Recent

Author Topic: OpenCV 4.5 using DynLibs  (Read 1273 times)

del

  • Full Member
  • ***
  • Posts: 214
OpenCV 4.5 using DynLibs
« on: June 12, 2021, 10:01:56 pm »
I am now able to use modern (C++ interface) OpenCV with Lazarus. It turned out to be very simple once I converted my image class from dynamic arrays to pointers. I compile a C++ library (linux .so) and call it using DynLibs and the image data pointers. The old Delphi wrapper was based on a long deprecated C interface. The C++ stuff does its own memory management, and Pascal provides the memory and pointers for input and output data. Here's the C++ demo:

Code: C  [Select][+][-]
  1. #include "opencv2/xphoto/oilpainting.hpp"
  2. //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
  3.  
  4. extern "C" void SimpleSO(float* img, float* img2, int rows, int cols, int szSng)
  5. {
  6.   int sz = sizeof(float);
  7.  
  8.   if (sz == szSng)
  9.   {
  10.     cv::Mat inImg(rows, cols, CV_32FC3);
  11.  
  12.     for (int i = 0; i < rows; i++)
  13.     {
  14.       cv::Vec3f* r = inImg.ptr<cv::Vec3f>(i);
  15.       int off = i * cols * 3;
  16.  
  17.       for (int j = 0; j < cols; j++)
  18.       {
  19.         for (int k = 0; k < 3; k++)
  20.         {
  21.           r[j][k] = img[off + k];
  22.         }
  23.  
  24.         off += 3;
  25.       }
  26.     }
  27.  
  28.     inImg.convertTo(inImg, CV_8UC3);
  29.     cv::Mat outImg(rows, cols, CV_8UC3);
  30.     cv::xphoto::oilPainting(inImg, outImg, 10, 1, cv::COLOR_BGR2Lab);
  31.     outImg.convertTo(outImg, CV_32FC3);
  32.  
  33.     for (int i = 0; i < rows; i++)
  34.     {
  35.       cv::Vec3f* r = outImg.ptr<cv::Vec3f>(i);
  36.       int off = i * cols * 3;
  37.  
  38.       for (int j = 0; j < cols; j++)
  39.       {
  40.         for (int k = 0; k < 3; k++)
  41.         {
  42.           img2[off + k] = r[j][k];
  43.         }
  44.  
  45.         off += 3;
  46.       }
  47.     }
  48.   }
  49. }

Very similar to something I did in C for Python a few years ago. All the memory management for img and img2 is handled by Pascal. The szSng variable comes from SizeOf(single) so it can be compared to sizeof(float). The Pascal pointers are pointing to "single".

« Last Edit: June 13, 2021, 01:46:39 am by del »

 

TinyPortal © 2005-2018