Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV pointer to bitmap processing

I've created a shared library for contour detection that is loaded from a Delphi/Lazarus application. The main app, passes a pointer to a bitmap to be processed by a function inside the library.

Here's the function inside the library. The parameter "img" is the pointer to my bitmap.

extern "C" {

  void detect_contour(int imgWidth, int imgHeight, unsigned char * img, int &x, int &y, int &w, int &h)
  {
    Mat threshold_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Mat src_gray;
    int thresh = 100;
        int max_thresh = 255;
    RNG rng(12345);

    /// Load source image and convert it to gray
    Mat src(imgHeight, imgWidth, CV_8UC4);
    int idx;

    src.data = img;

    /// Convert image to gray and blur it
    cvtColor( src, src_gray, CV_BGRA2GRAY );

    blur( src_gray, src_gray, Size(10,10) );

    /// Detect edges using Threshold
    threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
    /// Find contours
    findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    /// Approximate contours to polygons + get bounding rects and circles
    vector<vector<Point> > contours_poly( contours.size() );
    vector<Rect> boundRect( contours.size() );
    vector<Point2f>center( contours.size() );
    vector<float>radius( contours.size() );

    int lArea = 0;
    int lBigger = -1;

    for( int i = 0; i < contours.size(); i++ )
       { 
         approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
         boundRect[i] = boundingRect( Mat(contours_poly[i]) );
         if(lArea < boundRect[i].width * boundRect[i].height)
         {
           lArea = boundRect[i].width * boundRect[i].height;
           lBigger = i;
         }
       }

    if(lBigger > -1)
    {
       x = boundRect[lBigger].x;
       y = boundRect[lBigger].y;
       w = boundRect[lBigger].width;
       h = boundRect[lBigger].height;
    }
  }
}

From the Delphi side, I'm passing a pointer to an array of this structure:

TBGRAPixel = packed record
  blue, green, red, alpha: byte;
end; 

I need to process the bitmap in-memory, that's why I'm not loading the file from inside the library.

The question is: Is this the right way to assign a bitmap to a cv::Mat ?

I ask this because the code works without problems in Linux, but fails on Windows compiled with Mingw.

Note: it fails with a SIGSEGV on this line:

blur( src_gray, src_gray, Size(10,10) );

EDIT: The SIGSEGV is raised only if I compile OpenCV in Release mode, in Debug mode it works ok.

Thanks in advance, Leonardo.

like image 726
leonardorame Avatar asked Nov 04 '11 20:11

leonardorame


1 Answers

So you are creating an image this way:

Mat src(imgHeight, imgWidth, CV_8UC4);
int idx;

src.data = img;

The first declaration and instantiation Mat src(imgHeight, imgWidth, CV_8UC4) will allocate memory for a new image and a reference counter that automatically keeps track of the number of references to the allocated memory. Then you mutate an instance variable through

src.data = img;

When the the instance src goes out of scope, the destructor is called and most likely tries to deallocate the memory at src.data, which you assigned and this might cause a segmentation fault. The right way to do it is to not change instance variable of an object, but to simply use the right constructor when you instantiate src:

Mat src(imgHeight, imgWidth, CV_8UC4, img);

This way, you just create a matrix header and no reference counter or deallocation will be performed by the destructor of src.

Good luck!

EDIT: I am not sure that the segfault is actually caused by an attempt to deallocate memory incorrectly, but it is a good practice not to break data abstraction by assigning directly to instance variables.

like image 68
Rulle Avatar answered Oct 10 '22 08:10

Rulle