Recent

Author Topic: OpenCV with Lazarus  (Read 26222 times)

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
OpenCV with Lazarus
« on: March 07, 2016, 11:40:04 am »
hello,
here is a starting point to use opencv with Lazarus.
Not sure that works with all the functions of Opencv, but works with the lkdemo  ( video motion tracking)
OpenCV.pas needs to be checked because it is a version for 2.1 version of Opencv and i use it with version 3.0 of Opencv ( I have done some changes for that works).
OpenCV.pas needs some changes to work with Linux.
Lkdemo Lazarus project :
In attachment lkdemo project for Lazarus
 Download opencv 3.0 for windows from here
Extract the  dlls  \opencv-3.0.0.exe\opencv\build\x86\vc12\bin\*.dll (choose the right folder for your O.S ex : x86 -> 32 bits , vc12 -> visual c++ dlls  version 12)  from opencv-3.0.0.exe with 7z  in your project folder.
 you must change this line in unit1.pas -> choose a video file or capture from CAM
 
Code: Pascal  [Select][+][-]
  1.  //      capture := cvCaptureFromCAM(0);
  2.         capture := cvCaptureFromFile(PChar('f:\temp\videos\test.mp4'));

tested with Lazarus 1.6 Windows 32 bits and Opencv 3.0.

Thanks to Giandomenico   
Enjoy !
« Last Edit: June 08, 2018, 01:09:28 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

zbyna

  • Jr. Member
  • **
  • Posts: 63
Re: OpenCV with Lazarus
« Reply #1 on: April 18, 2016, 09:12:33 pm »

six1

  • Full Member
  • ***
  • Posts: 117
Re: OpenCV with Lazarus
« Reply #2 on: January 09, 2018, 02:19:42 pm »
My small contribution (Face detect, working Sample!):
http://www.mediafire.com/file/3t2a024nacfwx0k/Laz_OpenCV_Face_Detect.zip

You have to download OpenCV 2.4.13.5 https://opencv.org/releases.html
Extract following Libs from opencv\build\x86\vc14\bin\ into Programs dir:

opencv_calib3d2413.dll
opencv_contrib2413.dll
opencv_core2413.dll
opencv_features2d2413.dll
opencv_highgui2413.dll
opencv_imgproc2413.dll
opencv_legacy2413.dll
opencv_nonfree2413.dll
opencv_objdetect2413.dll
opencv_photo2413.dll
opencv_video2413.dll

Have fun, Michael

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: OpenCV with Lazarus
« Reply #3 on: January 19, 2018, 04:10:58 am »
Ooh.  OpenCV has been on my TODO list for a while.  Many thanks for posting this...
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

six1

  • Full Member
  • ***
  • Posts: 117
Re: OpenCV with Lazarus
« Reply #4 on: January 22, 2018, 02:05:23 pm »
Hi Mike,
it seems to be a long way...  ::)

Meanwhile i've modified Delphi components to work with Lazarus.

After many fixes and few improvements, most of it is working, but there are a few problems left to do...
(ocvView is now working after many hours of debuging, but videosource must be set at runtime! see LazSample in Samle folder on github)

look at github, for latest changes of my OpenCV Fork

https://github.com/six1/Laz_OpenCV_2


best, Michael
« Last Edit: January 22, 2018, 03:36:57 pm by six1 »

Lotiara

  • Newbie
  • Posts: 3
Re: OpenCV with Lazarus
« Reply #5 on: February 08, 2018, 06:53:25 am »
Thank you six1

Just installed your fork.
Impossible to make it work so I've installed Lazarus x86.
Now it's moment to start playing with the lib, I want to detect a car position in a
parking, I need to know the exact location (don't know if it's possible as I need to convert
the pitcure/video position to a real position), any clue on how to do this would be appreciated.
Good Job
Bye

six1

  • Full Member
  • ***
  • Posts: 117
Re: OpenCV with Lazarus
« Reply #6 on: February 08, 2018, 01:34:00 pm »
Hi Lotiara,
with my experiences on OpenCV, that would be not that easy...
First you have to make a Haar Detection File. Therfore you need Pictures from Cars in the View you want to recognize later on..

I would give it a try with Motion Detection... this is working stable an accurate.
If you got my Sample working, select "Motion Detection" in Combobox and see, what's going on.  :)

Best, Michael

m4u_hoahoctro

  • Full Member
  • ***
  • Posts: 160
Re: OpenCV with Lazarus
« Reply #7 on: May 17, 2018, 06:14:05 pm »
hello,
here is a starting point to use opencv with Lazarus.
Not sure that works with all the functions of Opencv, but works with the lkdemo  ( video motion tracking)
OpenCV.pas needs to be checked because it is a version for 2.1 version of Opencv and i use it with version 3.0 of Opencv ( I have done some changes for that works).
OpenCV.pas needs some changes to work with Linux.
Lkdemo Lazarus project :
In attachment lkdemo project for Lazarus
 Download opencv 3.0 for windows from here
Extract the  dlls  \opencv-3.0.0.exe\opencv\build\x86\vc12\bin\*.dll (choose the right folder for your O.S ex : x86 -> 32 bits , vc12 -> visual c++ dlls  version 12)  from opencv-3.0.0.exe with 7z  in your project folder.
 you must change this line in unit1.pas -> choose a video file or capture from CAM
 
Code: Pascal  [Select][+][-]
  1.  //      capture := cvCaptureFromCAM(0);
  2.         capture := cvCaptureFromFile(PChar('f:\temp\videos\test.mp4'));

tested with Lazarus 1.6 Windows 32 bits and Opencv 3.0.

Thanks to Giandomenico   
Enjoy !

I followed your instruction (using CAM), but on my DELL Inspirion 14 series 3000, I just see the flash light, I dont see anything in app window

dimonsky

  • Newbie
  • Posts: 3
Re: OpenCV with Lazarus
« Reply #8 on: July 08, 2018, 10:34:34 am »
Could you please help:

CPP code reads picture correctly:
Code: Pascal  [Select][+][-]
  1. cv::Mat img;
  2. img = imread("/home/pi/MyPrograms/cxx/Where.png", IMREAD_UNCHANGED);

but Lazarus reads wrong number of rows (just toooo big value). Cols number is OK:
Code: Pascal  [Select][+][-]
  1. var
  2. img:PCvMat;
  3. ...
  4. img:=cv_imread(pchar('/home/pi/MyPrograms/cxx/Where.png'),CV_LOAD_IMAGE_UNCHANGED);

what is the reason??

I run code on Raspbery pi with the attached interface header.
« Last Edit: July 08, 2018, 10:56:43 am by dimonsky »

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: OpenCV with Lazarus
« Reply #9 on: July 14, 2018, 01:04:52 am »
Just to note all the codes for "OpenCV.pas" I've seen here are using the directives:

Code: Pascal  [Select][+][-]
  1. {$ifndef V3 AND $ifndef V2}

But I've found this is not working the proper way.

I've replaced it with:

Code: Pascal  [Select][+][-]
  1. {$if not Defined(V3) AND Not Defined(V2)}

and it works OK.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: OpenCV with Lazarus
« Reply #10 on: July 29, 2018, 08:21:11 am »
but Lazarus reads wrong number of rows (just toooo big value). Cols number is OK:
Code: Pascal  [Select][+][-]
  1. var
  2. img:PCvMat;
  3. ...
  4. img:=cv_imread(pchar('/home/pi/MyPrograms/cxx/Where.png'),CV_LOAD_IMAGE_UNCHANGED);

what is the reason??

I run code on Raspbery pi with the attached interface header.

It's probably your structure TIplImage has not the correct size.  Check the size of the fields of the records. I've tested your code in Ubuntu and it works OK.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: OpenCV with Lazarus
« Reply #11 on: July 29, 2018, 07:03:25 pm »
My small collaboration: https://github.com/t-edson/MiOpenCV
Fixed to work in Lazarus 1.8 for Windows and Linux.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: OpenCV with Lazarus
« Reply #12 on: August 03, 2018, 11:04:09 pm »
Other contribution for Linux/Windows: https://github.com/t-edson/LazarusOpenCV
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: OpenCV with Lazarus
« Reply #13 on: September 08, 2021, 07:14:21 am »
hello,
Another possibility -->  Python4Lazarus  opencv-python
requirements :
Python4Lazarus package
A python embedded distribution  with same "version" of your lazarus project ( 32 bits , 64 bits)
pip python utility to get modules, libraries from the pypi repository
knowlege of python language.
pros :
fast
use the last version of opencv  (embedded in opencv-python library)
can use existing python scripts with small changes (to communicate with lazarus).
in attachment example of demo project to use opencv to detect stop panels in an image using  Haar feature-based cascade classifier.
the result of python script is displayed in a Timage component.
Lazarus 2.0.12  64 bits - Windows  10 - python 3.9 - opencv 4.5.3   
Time to detect the panels : 0.062 s

Friendly, J.P
« Last Edit: September 08, 2021, 07:34:20 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 with Lazarus
« Reply #14 on: September 09, 2021, 01:22:31 am »
Nice. However native usage of OpenCV 4.xx without Python would require C bindings around C++ as found here:
https://github.com/aferust/opencvd
https://github.com/hybridgroup/gocv
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018