Opencv version 4.6 C++ API wrapper lazarus / 39-Graphics ---------------------------------------------------------------------- gidesa · 2023-04-01 11:09 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 · 2023-04-01 21:55 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? ---------------------------------------------------------------------- gidesa · 2023-04-02 09:57 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 · 2023-04-03 03:32 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 ---------------------------------------------------------------------- avra · 2023-04-03 09:16 Quote from: Jurassic Pork on April 03, 2023, 05:32:37 amit 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! ---------------------------------------------------------------------- Jurassic Pork · 2023-04-03 10:37 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 install2 - 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 ! ---------------------------------------------------------------------- gidesa · 2023-04-03 14:07 @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 · 2023-04-03 14:19 Quote from: gidesa 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? 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 QuoteWINDOWS: 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 ---------------------------------------------------------------------- gidesa · 2023-04-03 16:32 @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 · 2023-04-04 09:27 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 ---------------------------------------------------------------------- gidesa · 2023-04-04 10:01 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 · 2023-04-04 11:24 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 : QuoteUse the respective Pascal (platform independent ones): CopyMemory -> Move(,,); MoveMemory -> doesn't really exist, do you really need to? ZeroMemory -> FillByte(,,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 ---------------------------------------------------------------------- gidesa · 2023-04-04 18:07 @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! ---------------------------------------------------------------------- gidesa · 2023-04-05 17:48 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 · 2023-04-05 23:15 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 installbut if you do that, gtk3 doesn't work anymore (qt5 still works) Friendly, J.P ---------------------------------------------------------------------- gidesa · 2023-04-06 07:58 @JP ah, ok! I compiled the Lazarus program with the default widgetset, maybe it's GTK2. Indeed I am using Xubuntu 22, perhaps GTK2 is the default here, so Opencv complains that it's not linked. I will try with GTK3 and qt5, and eventually recompile Opencv. Thank you ---------------------------------------------------------------------- Jurassic Pork · 2023-04-06 08:23 Quote from: gidesa on April 06, 2023, 09:58:15 amIndeed I am using Xubuntu 22, perhaps GTK2 is the default here, so Opencv complains that it's not linked. I will try with GTK3 and qt5, and eventually recompile Opencv. Xubuntu 22.04 : QuoteApplication Stack GNOME 42 GTK 3.24.33 MATE 1.26 Xfce 4.16 ---------------------------------------------------------------------- gidesa · 2023-04-06 16:36 Hello, at end success!  But rebuilding Opencv with GTK2. Building program with GTK3 widgetset causes same error that was previously, see image 1. Maybe xfce requires GTK2? I don't know. I also attached all building scripts, install.sh try to automate the various steps. ---------------------------------------------------------------------- gidesa · 2023-04-07 15:15 Hello, on github released updated version 1.0 of ocvWrapper46 library, with all fixes and scripts for Linux. Many thanks to JP! https://github.com/gidesa/ocvWrapper46 ---------------------------------------------------------------------- gidesa · 2023-05-05 15:08 Hello, the OpencvWrapper46 version 1.1 is released and ready to test. There are four new Lazarus components and other classes that great simplify the use of Opencv, especially for object recognition, and face recognition/detection. https://github.com/gidesa/ocvWrapper46 Thanks Giando ---------------------------------------------------------------------- fcu · 2023-05-05 17:31 thanks for the wrapper , very nice work please can you build the wrapper dll staticaly ? , it depend now on visual c++ runtime ---------------------------------------------------------------------- gidesa · 2023-05-06 10:30 @fcu, thanks appreciating my work. Statically link the C++ runtime, in my opinion, is not a good option, for these reasons: Opencv binaries depend themselves from C++ runtime Dll; so you should have to recompile also Opencv, and at end have two copies of same runtime inside the two Dll, Opencv and wrapper (indeed Opencv has at less 2 Dll). More, I don't know if linking Dll with static runtime is a good choice for multithreading. Remember that the application stack  with wrapper is somewhat complex: a Delphi/Lazarus exe (or maybe another Dll)  that calls the wrapper Dll that calls the Opencv Dll. ---------------------------------------------------------------------- gidesa · 2024-03-13 15:06 Hello, the Opencv 4.6 wrapper repository now has new examples, better Pascal classes, much better documentation. And instructions for compile on MacOS, too. Enjoy! Github repo    https://github.com/gidesa/ocvWrapper46/ ---------------------------------------------------------------------- fcu · 2024-03-14 06:51 Quote from: gidesa on March 13, 2024, 04:06:35 pmHello, the Opencv 4.6 wrapper repository now has new examples, better Pascal classes, much better documentation. And instructions for compile on MacOS, too. Enjoy! Github repo    https://github.com/gidesa/ocvWrapper46/ very nice work but i could not run it due to the missing visual c++ runtime , i have an old pc with windows7 32bits why would you not build it with mingw64 instead , mingw64 will generate dll's with 0 dependencies ---------------------------------------------------------------------- gidesa · 2024-03-14 11:29 @fcu thank you for kind words. Your suggestion is interesting. In Windows I tried to compile the wrapper with TDM-GCC-64, one of distribution of GCC compiler. The compilation is ok, but the linking failed, having only the Visual C++ DLL and static lib of Opencv. I will try to recompile the entire Opencv with GCC. Indeed you need the 32bit version of DLLs, is it correct? Have you suggestions on possible demo programs? ---------------------------------------------------------------------- Dzandaa · 2024-03-14 13:33 Hi, Just for info, Here are some notes I wrote one year ago for installing CUDA and OpenCV on Linux. Hope that help. And @gidesa, a great great thank you B-> ---------------------------------------------------------------------- fcu · 2024-03-14 16:28 Quote from: gidesa on March 14, 2024, 12:29:31 pm@fcu thank you for kind words. Your suggestion is interesting. In Windows I tried to compile the wrapper with TDM-GCC-64, one of distribution of GCC compiler. The compilation is ok, but the linking failed, having only the Visual C++ DLL and static lib of Opencv. I will try to recompile the entire Opencv with GCC. Indeed you need the 32bit version of DLLs, is it correct? Have you suggestions on possible demo programs? i had no issue building opencv4.6 using mingw64 i used gcc13 from (https://winlibs.com/#download-release)  and cmake(gui version) but you have to disable ADE framework ---------------------------------------------------------------------- gidesa · 2024-03-14 17:47 @Dzandaa thanks, you are welcome. @Fcu thank you. Eventually I have built 32bit version of Opencv and wrapper with TDC mingw. But reading jpeg files is not working. I will try again unchecking ADE. ---------------------------------------------------------------------- gidesa · 2024-03-15 17:11 @fcu I successfully compiled Opencv 4.6 and wrapper with  gcc version 10.3.0 (tdm64-1) on Windows 10. The 64 bit Opencv/wrapper Dll work fine on some quick tests. The 32 bit Opencv Dll is not working on reading Jpg images. To compile Opencv 32 bit I set these options in Cmake-gui: - in CMake group:    - set in CMAKE_CXX_FLAGS: -m32 -mwin32 -fpermissive    - set in CMAKE_C_FLAGS:   -m32 -mwin32 -fpermissive    - set  in CMAKE_RC_FLAGS:  -Fpe-i386 - in OPENCV group:    - check on OPENCV_FORCE_3RDPARTY_BUILD - in BUILD group:    - check on BUILD_opencv_world , to have a single DLL Attached the makefile for wrapper in 32bit and 64bit version, and a simple C++ program and Cmake file to test Jpg in Opencv. Maybe you can resolve the issue. ---------------------------------------------------------------------- fcu · 2024-03-16 14:44 many thanks gidesa , i'll test your makefile and see what could be done for jpg ---------------------------------------------------------------------- gidesa · 2024-11-14 15:11 Hello, for Opencv wrapper released many improvements and new examples: - classes that encapsulate OCV vectors and pointers. - a component to write movies from a video stream. - an example for movie writing, - an example that uses Machine Learning classifiers to recognize handwritten digits. - a detailed documentation See at  https://github.com/gidesa/ocvWrapper46 ---------------------------------------------------------------------- Mongkey · 2025-06-21 11:20 hello gidesa, i found error compiling on dnn2 sample, this following line was throwing error: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i netResult:=TOcvParamMat.Create(resdims, cvSingle, 1, dptr); frmDnnObjectDetection2.pas(227,58) Error: Incompatible type for arg no. 3: Got "ShortInt", expected "TOcvDataType" how to solve this error? Thank you. Lazarus 3.6 (rev lazarus_3_6) FPC 3.2.2 x86_64-win64-win32/win64 ---------------------------------------------------------------------- gidesa · 2025-06-21 16:55 Quote from: Mongkey on June 21, 2025, 01:20:34 pmhello gidesa, i found error compiling on dnn2 sample, this following line was throwing error: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i netResult:=TOcvParamMat.Create(resdims, cvSingle, 1, dptr); frmDnnObjectDetection2.pas(227,58) Error: Incompatible type for arg no. 3: Got "ShortInt", expected "TOcvDataType" how to solve this error? Thank you. Lazarus 3.6 (rev lazarus_3_6) FPC 3.2.2 x86_64-win64-win32/win64 Hello, add this in Var section: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   defaultArr: TArray; Then, in procedure add defaultArr as last parameter of constructor: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         SetLength(defaultArr, 0);{..............................................}    for j:=0 to  nrDetections-1 do    begin       // define a 1 x outW mat  over a block of the output matrix       netResult:=TOcvParamMat.Create(resdims, cvSingle, 1, dptr, defaultArr);      {..............................................} Now fixed in repository. ---------------------------------------------------------------------- Mongkey · 2025-06-22 05:07 Thank you gidesa, Working very well, seems very promising fast detection, from 1 to 10 score, our lovely pascal wrapper got how many score compared to python and c++ from your point of view? ---------------------------------------------------------------------- gidesa · 2025-06-22 16:00 Quote from: Mongkey on June 22, 2025, 07:07:01 amThank you gidesa, Working very well, seems very promising fast detection, from 1 to 10 score, our lovely pascal wrapper got how many score compared to python and c++ from your point of view? Thank you. Indeed note that the wrapper is just .... a wrapper on calculations done inside Opencv library, written in C++. So C++ remains faster, because interfacing from main program is more straighforward. And because heavy optimization work was done on some of its compilers (but not all C++ compilers are better than Pascal ones). And, for heavy processing with millions of detection cycles  (realtime detection from a camera, or processing thousands of images), Pascal remains much better than Python. I have done a little robot that just use this wrapper for continuous autonomous obstacle detection and object recognition. ---------------------------------------------------------------------- Mongkey · 2025-06-23 00:39 QuoteI have done a little robot that just use this wrapper for continuous autonomous obstacle detection and object recognition. Seems very interesting  .