Recent

Author Topic: Opencv version 4.6 C++ API wrapper  (Read 4683 times)

gidesa

  • New Member
  • *
  • Posts: 44
Opencv version 4.6 C++ API wrapper
« on: April 01, 2023, 01:09:27 pm »
Hello,
is ready to download and test the wrapper around the Opencv version 4.6  C++ API .
This is the more recent Opencv stable version.
The old pure C API misses all the functions implemented only as C++ classes,
starting from the Machine Language module (ML), that was present since the Opencv v 1.0 .
Note: from v 2.4 onwards the old C API is always present, although no more officially supportoted.
This version of Opencv, among many other things, has a Deep Neural Network (DNN) module,
capable of load and use the most common neural net formats: Tensorflow, Torch, Caffe, Darknet, ONNX.
The wrapper supports this, of course, and has many improvements compared to my precedent wrapper for
Opencv v 2.4.31 .
All suggestions are welcome.
On Github:

https://github.com/gidesa/ocvWrapper46


Giando

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Opencv version 4.6 C++ API wrapper
« Reply #1 on: April 01, 2023, 11:55:38 pm »
Thanks!

Newer OpenCV wrapper has been missing for a long time, so this is most welcome.
Do you plan to release Linux Intel/ARM versions of the library?
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

gidesa

  • New Member
  • *
  • Posts: 44
Re: Opencv version 4.6 C++ API wrapper
« Reply #2 on: April 02, 2023, 11:57:30 am »
Thanks Avra!
I am not very much expert of Linux. But I have tried to compile the preceding version (OpencvWrapper24) with GCC on Windows, as preliminary step. The compilation was successful (attached the make file), of course the linking was not, because the Opencv distribution doesn't have binaries compatible with GCC. And recompiling entire Opencv with GCC is a very complex work, I imagine.
More, I tried in a Linux virtual machine, but the Opencv 2.4 is no more found by apt-get  in repositories, and direct compilation requires some other libraries that are also no more present. So I give away.
Maybe now the Opencv 4.6 is in repositories, I will try.
Of course, every suggestion and help is welcome by the great community of Lazarus!
 

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Opencv version 4.6 C++ API wrapper
« Reply #3 on: April 03, 2023, 05:32:37 am »
hello,
it is possible to use the wrapper under linux. I succeeded under ubuntu 20.04 using Lazarus 2.2.0 . test project featureTest using qt5 for GUI. See attachment. More informations later ...
Friendly, J.P
« Last Edit: April 03, 2023, 06:49:44 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Opencv version 4.6 C++ API wrapper
« Reply #4 on: April 03, 2023, 11:16:34 am »
it is possible to use the wrapper under linux. I succeeded under ubuntu 20.04 using Lazarus 2.2.0 . test project featureTest using qt5 for GUI. See attachment. More informations later ...
Nice!
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Opencv version 4.6 C++ API wrapper
« Reply #5 on: April 03, 2023, 12:37:48 pm »
Instructions to compile and use Opencv API Wrapper Lazarus on Linux :
1 - Install Opencv 4.6 from source :
Quote
# Install minimal prerequisites (Ubuntu 20.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip
# Download and unpack sources
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
unzip opencv.zip
# Create build directory in the opencv directory
mkdir -p build && cd build
# Configure
cmake  ../opencv-4.x
# Build
cmake --build .
# Install
sudo make install
2 - Download or git clone the Opencv version 4.6 C++ API wrapper project from github
create a new folder source-linux and put the content of source inside.
delete dllmain.cpp
modify wrapper.cpp :
#include     "opencv2/kdtree.hpp"
modify the HOME variable in the Makefile with your HOME
======================================
Modify the file precomp.hpp in the include files of opencv
precomp.hpp in opencv-4.6.0/modules/core/include:
//#include "opencv2/core/private.hpp"
======================================
in a terminal cd in the source-linux folder and try the command
make
if it is OK copy the so file in the /usr/lib. example :
sudo cp libocvCPPWrapper46.so  /usr/lib
sudo ldconfig
try the project in  the folder examples-Lazarus-Linux/featureTest
you need qt5 installed to compile and run this project
enjoy !
« Last Edit: April 03, 2023, 12:39:49 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

gidesa

  • New Member
  • *
  • Posts: 44
Re: Opencv version 4.6 C++ API wrapper
« Reply #6 on: April 03, 2023, 04:07:55 pm »
@JP really a very nice work! Can have your permission to add your tutorial in my repository?

About your correction:
   #include     "opencv2/kdtree.hpp"
Because in my Opencv on Windows the file is in  C:\opencv4.6\sources\modules\ms\src,  so
if I write, as now:
   #include     "kdtree.hpp"
compilation is ok.
Instead adding "opencv2/" generate error.
Perhaps the Opencv distribution for Linux has some differences.
I could add a conditional form in that line.
Windows is identificated by "WIN32", what is the constant for Linux?
By the way, at last I succeeded to compile Ocv 4.6 with Mingw on Windows, then compiled and linked
fine ocvWrapper46.dll.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Opencv version 4.6 C++ API wrapper
« Reply #7 on: April 03, 2023, 04:19:15 pm »
@JP really a very nice work! Can have your permission to add your tutorial in my repository?
Windows is identificated by "WIN32", what is the constant for Linux?
1 - yes you can add my instructions in your repository but wait for someone checks if this instructions are OK on a linux O.S.
2 - you can find the Platform Defines for Lazarus here : https://wiki.freepascal.org/Platform_defines#Operating_system_defines
Quote
WINDOWS: all Windows
   MSWINDOWS
     WIN16: 16-bit Windows 3.x
     WIN32: 32-bit Windows
     WIN64: 64-bit Windows
     WINCE, UNDER_CE, UNICODE: Windows CE, Windows mobile

UNIX
     LINUX

« Last Edit: April 03, 2023, 04:23:01 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

gidesa

  • New Member
  • *
  • Posts: 44
Re: Opencv version 4.6 C++ API wrapper
« Reply #8 on: April 03, 2023, 06:32:22 pm »
@JP Thank you. Indeed I refer to defines in C++ compiler.
But it's really simpler:
#ifdef WIN32
  #include     "kdtree.hpp"
#else
  #include     "opencv2/kdtree.hpp"
#endif
For now it suffices :)
 

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Opencv version 4.6 C++ API wrapper
« Reply #9 on: April 04, 2023, 11:27:35 am »
hello,
there is something wrong with kdtree.hpp. Normaly you don't need to add this header file in the include files for the wrapper shared library. But if you don't put it in the wrapper.cpp, the shared library doesn't compile. May be there is something wrong in your .h include files -> too many things inside.
friendly J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

gidesa

  • New Member
  • *
  • Posts: 44
Re: Opencv version 4.6 C++ API wrapper
« Reply #10 on: April 04, 2023, 12:01:39 pm »
Hellp JP, thanks for reply.
The wrapper exposes the KDTree C++ class. But this is defined just in kdtree.hpp header,
so this header has to be included in wrapper.cpp.
But, strange enough, the header is in modules\ml\src\ directory, and not in the usual \ml\include.
Nor it's included in other headers.
You can see directly on Opencv github repository (attached snapshot).



 

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Opencv version 4.6 C++ API wrapper
« Reply #11 on: April 04, 2023, 01:24:53 pm »
another thing :
in the file OPENCVWrapper.Pas  you use CopyMemory from windows Unit -> under Linux  when you compile there is an error with theCopyMemory --> Use Move to replace CopyMemory in the LCL code.
from post of Leledumbo :
Quote
Use the respective Pascal (platform independent ones):
CopyMemory -> Move(<source>,<dest>,<size>);
MoveMemory -> doesn't really exist, do you really need to?
ZeroMemory -> FillByte(<a variable>,<the variable size>,0);

and i have new instructions to install opencv and compile the wrapper under linux :
Quote
# Install minimal prerequisites (Ubuntu 20.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip
# Download and unpack sources in your home directory
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.6.0.zip
unzip opencv.zip
# Create build directory in the opencv directory
mkdir -p build && cd build
# copy build_min.sh in build folder and change mode
chmod a+x build_min.sh
#Configure for min compilation
./build_min.sh
# or Configure with standard options
# cmake  ..
# Build
#cmake --build .
# Install
sudo make install
======================================
modify the HOME variable in the Makefile with your HOME
in a terminal cd in the source-linux folder and try the command
make
if it is OK copy the so file in the /usr/lib. example :
sudo cp libocvCPPWrapper46.so  /usr/lib
sudo ldconfig

opencv is very long to compile, so i create a script to compile to reduce the time ( options to disable some parts) it is
build_min.sh in source-linux
i have put the files kdtree.hpp and precomp.hpp in source-linux
the Makefile has changed and wrapper.pas (kdtree.hpp) is in the directory -> #include "kdtree.hpp" works)
in Attachment source-linux

« Last Edit: April 04, 2023, 04:45:27 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

gidesa

  • New Member
  • *
  • Posts: 44
Re: Opencv version 4.6 C++ API wrapper
« Reply #12 on: April 04, 2023, 08:07:04 pm »
@JP thank you very much! Using your guide, I compiled Opencv 4.6 and ocvWrapper46 in Ubuntu 22.10 64bit (in Virtualbox VM).
The include folder structure of Opencv in Linux is very different from Windows. So you done a great work fixing also that!  :D

gidesa

  • New Member
  • *
  • Posts: 44
Re: Opencv version 4.6 C++ API wrapper
« Reply #13 on: April 05, 2023, 07:48:38 pm »
I am sorry, but the minimal Opencv build script causes a fault.
Some gui functions, as pCvimshow, don't work. The lib should be
linked also with graphic libraries, as Gtk.
See attached screenshot.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Opencv version 4.6 C++ API wrapper
« Reply #14 on: April 06, 2023, 01:15:12 am »
Hello,
i have no problem using qt5 as LCL WidgetSet and gtk3 also works.  See Attachment
gtk2 doesn't work.
[EDIT] gtk2 now works :
Add this in the build_min.sh script : -D WITH_GTK_2_X=ON then :
Quote
./build_min.sh
# or Configure with standard options
# cmake  ..
# Build
#cmake --build .
# Install
sudo make install
but if you do that, gtk3 doesn't work anymore (qt5 still works)

Friendly, J.P
« Last Edit: April 06, 2023, 07:39:23 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018