Recent

Author Topic: OpenCV Linux?  (Read 5670 times)

del

  • Sr. Member
  • ****
  • Posts: 258
OpenCV Linux?
« on: February 06, 2018, 08:26:34 pm »
I put a "?" in the title cuz I don't have any news - just questions. I do a lot of hobby coding for images - particularly video frames. I want to take advantage of OpenCV functionality in my Lazarus / FPC processing on Linux. How do I get started? I've run some Python OpenCV demos and would like to bring that goodness into my Lazarus / FPC world.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 793
    • Blog personal
Re: OpenCV Linux?
« Reply #1 on: February 06, 2018, 09:49:19 pm »
Hello, I don't have any experience, but here are some information.
I think it's a starting point
https://forum.lazarus.freepascal.org/index.php?topic=31851.0
/BlueIcaro

del

  • Sr. Member
  • ****
  • Posts: 258
Re: OpenCV Linux?
« Reply #2 on: February 06, 2018, 10:54:20 pm »
Hello, I don't have any experience, but here are some information.
I think it's a starting point
https://forum.lazarus.freepascal.org/index.php?topic=31851.0
/BlueIcaro
Thanks - I saw that earlier and it seems like people are working the problem / opportunity. He says "OpenCV.pas needs some changes to work with Linux." I guess I could add "OpenCV.pas" to a project and try to fix compile / link errors.

del

  • Sr. Member
  • ****
  • Posts: 258
Re: OpenCV Linux?
« Reply #3 on: February 09, 2018, 08:53:29 pm »
Progress.  8-) Using Giandomenico's OpenCV.pas http://gidesa.altervista.org/camshiftdemo.php (lkdemo-fp.zip) I made some minor changes and got it to compile and link. I'm using linux Mint (like Ubuntu) and Lazarus 1.8.0 from the deb files at sourceforge. It was just a matter of getting the right library files:

Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. uses
  4.   Graphics, FPImage, IntfGraphics, Sysutils, Math, IPL;
  5.  
  6. const
  7.  
  8.  // cvDLL       = 'libcv.so.2.1';
  9.  cvDLL       = 'libopencv_imgproc.so.2.4.9';
  10.  // videoDLL    = 'libcv.so.2.1';
  11.  videoDLL    = 'libopencv_video.so.2.4.9';
  12.  // calibDLL    = 'libcv.so.2.1';
  13.  calibDLL    = 'libopencv_calib3d.so.2.4.9';
  14.  // HighGUI_DLL = 'libhighgui.so.2';
  15.  HighGUI_DLL = 'libopencv_highgui.so.2.4.9';
  16.  // cxCore      = 'libcxcore.so.2.1';
  17.  cxCore      = 'libopencv_core.so.2.4.9';
  18.  
  19.  // New !!!
  20.  legacyDLL = 'libopencv_legacy.so.2.4.9';

The original names are commented out - three of the functions are now deprecated so I had to introduce the "legacy" library. And that showed up here:

Code: Pascal  [Select][+][-]
  1.  procedure  cvCalcOpticalFlowLK;          external legacyDLL name 'cvCalcOpticalFlowLK';
  2.  procedure  cvCalcOpticalFlowBM;          external legacyDLL name 'cvCalcOpticalFlowBM';
  3.  procedure  cvCalcOpticalFlowHS;          external legacyDLL name 'cvCalcOpticalFlowHS';
  4.  procedure  cvCalcOpticalFlowPyrLK;       external videoDLL name 'cvCalcOpticalFlowPyrLK';
  5.  function   cvCamShift;                   external videoDLL name 'cvCamShift';

So the way I solved this. The IDE was just telling me that it failed to compile cuz of an unidentified "generic" linking error. So I launched Lazarus from a terminal and during compilation the full fpc command was echoed in the terminal. So I closed the IDE and copied that command with all its bazillion arguments and ran it from the command line. Bingo. Detailed error messages that led me directly to the problem.

The next linux issue will be using TBGRABitmap instead of TBitmap. Which I do already for other stuff but so far not with this new OpenCV stuff. Good times.

del

  • Sr. Member
  • ****
  • Posts: 258
Re: OpenCV Linux?
« Reply #4 on: February 11, 2018, 01:51:04 am »
Hmm ... I can get the cvLoadImage and cvSaveImage functions to work. And the image structure IplImage seems to be getting populated correctly. But there's something wrong with the memory access of the IMAGEDATA element. It's non-null but I get "<error: Cannot access memory at address 0x109325....". And IMAGEDATAORIGIN is $0 (which doesn't seem right to me).

And everybody out there is disparaging the "deprecated" IplImage approach and pushing the modern C++ interface. So now I'm gonna punt and build an external library that basically passes simple unsigned char pointers back and forth and handles all the fancy C / C++ gymnastics internally.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: OpenCV Linux?
« Reply #5 on: February 11, 2018, 08:41:08 am »
So now I'm gonna punt and build an external library that basically passes simple unsigned char pointers back and forth and handles all the fancy C / C++ gymnastics internally.
If you use trunk you can use the system OpaquePointer type for that instead of PChar. It was introduced exactly for such a purpose: it is a strongly typed pointer to a structure of unknown - in Pascal -  size (in the case of this library known and used in the C++ part).
This is more descriptive and type safe towards its actual use: a Pchar is not the correct type, it is weakly typed, although it works.
In 3.0.4 you can introduce it yourself:
Code: Pascal  [Select][+][-]
  1. type
  2.   OpaquePointer =^TEmptyRecord; // strongly typed pointer to a structure which content itself is only of interest to the external library.
  3.   TEmptyRecord = record // it is not really empty but not handled in Pascal
  4.  end;

Note this pointer type can also serve other purposes for things in pure pascal code. But its main use is for code like the above.
« Last Edit: February 11, 2018, 09:14:20 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018