Recent

Author Topic: A Way To Use Modern OpenCV?  (Read 346 times)

del

  • Jr. Member
  • **
  • Posts: 89
A Way To Use Modern OpenCV?
« on: September 19, 2019, 12:52:23 am »
I've asked about OpenCV before. Specifically the latest C++ versions. Since then I've spent about 18 months programming with OpenCV in C++. However, Lazarus is head and shoulders above any other non-commercial IDE for Linux. So I'm always pulled back to Lazarus (and Pascal!) by something like a gravitational force.

So yesterday, I was thinking ...

  • A .dll or .so is basically a blob of machine code - it really does not matter in what language the source files were written.
  • An executable is also basically a blob of machine code - it also doesn't matter in what language the source files were written.
  • So at runtime it's all machine code, and the secret is getting the executable's machine code to interact with the .dll or .so machine code.
  • There are no OpenCV "bindings" for Free Pascal (as there are for Python and Java), so it's difficult / impossible to pass sophisticated C++ objects back and forth between the base executable section and the loaded .dll or .so section.
  • However, simple objects can be passed back and forth as pointers, and the basic element of the "cv::Mat" class is its data buffer.
  • So if I can pass this buffer back and forth ("flattened") along with some other simple parameters, then inside the .dll or .so section (which I write in C++) I can construct full blown cv::Mats and we're off to the races.
  • And when the OpenCV processing is done, the results are passed back through that same data pointer.
If I'm understanding this correctly, I don't have to wait for somebody to develop OpenCV bindings for Free Pascal. I just write my own .dll's and / or .so's in C++ and manage the interfaces. And I still get to write the main app in Lazarus / FPC !!! It seems almost too good to be true! What am I missing? I've never done anything like this with Lazarus, but I've done something similar with Python "ctypes" and a .dll written in C.

I don't want you to spend a lot of time coaching the specifics, I should learn those on my own time. But I do want to know if it's as feasible as I seem to believe it is. Thanks in advance !! (I was gonna post this in "Graphics" but it's generic enough conceptually for general programming)


tr_escape

  • Sr. Member
  • ****
  • Posts: 393
  • sector name toys | respect to spectre
    • Github:
Re: A Way To Use Modern OpenCV?
« Reply #1 on: September 19, 2019, 07:02:12 am »
You already explained your problem and your solving method :) so also there some workarounds on github:

https://github.com/zbyna/opencv-lazarus last commit 2016

and

https://github.com/Laex/Delphi-OpenCV  last commit 2 days ago

If you wish you can create another one too.

If you need more about how to use and create a dll / so file lazarus/fpc's wikies will be help

https://wiki.freepascal.org/shared_library


PascalDragon

  • Hero Member
  • *****
  • Posts: 639
  • Compiler Developer
Re: A Way To Use Modern OpenCV?
« Reply #2 on: September 19, 2019, 09:09:07 am »
If I'm understanding this correctly, I don't have to wait for somebody to develop OpenCV bindings for Free Pascal. I just write my own .dll's and / or .so's in C++ and manage the interfaces. And I still get to write the main app in Lazarus / FPC !!! It seems almost too good to be true! What am I missing? I've never done anything like this with Lazarus, but I've done something similar with Python "ctypes" and a .dll written in C.

I don't want you to spend a lot of time coaching the specifics, I should learn those on my own time. But I do want to know if it's as feasible as I seem to believe it is. Thanks in advance !! (I was gonna post this in "Graphics" but it's generic enough conceptually for general programming)
That is indeed how bindings for C++ libraries are usually done: by flattening the interface. Qt4Pas works this way as well for example.

del

  • Jr. Member
  • **
  • Posts: 89
Re: A Way To Use Modern OpenCV?
« Reply #3 on: September 19, 2019, 06:42:42 pm »
Cool - thanks for the replies. That's encouraging. I'll start poking around with it this weekend!   :)