Ask Your Question
0

Iplimage to Mat memory acces violation

asked 2013-04-19 12:03:02 -0600

I´m having some problems with a code I'm writing, i'm trying to make the bwlabel operation in c++. And I'm having some memory deallocation problems, I don't know why, because I tried to follow the documentation in the OpenCV tutorials. It seems to be a problem with the variable refcount proper of the variable Mat.

Here is my code:

void VideoSeg::bwlabel(IplImage *srce, IplImage *out) {

namedWindow( "wndNameOut", CV_GUI_NORMAL );
cvConvertScale(srce,srce,255.);
Ptr<IplImage> srcx = srce; 

Mat src(srcx);

imshow( "wndNameOut", src);            //The image is succesfully plotted
SimpleBlobDetector blobDetector( params );
blobDetector.create("SimpleBlob");

blobDetector.detect(src, keyPoints );  // The problem appears in this line

for(int i=0; i<keyPoints.size(); i++ )
{
    cv::floodFill(src,keyPoints[i].pt, Scalar::all((i+1)*255/(keyPoints.size()+1)));
}

IplImage outx = src;
//http://docs.opencv.org/doc/tutorials/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.html
(*out) = outx;

cout << "Keypoints " << keyPoints.size() << endl;

}

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2013-04-19 12:32:53 -0600

unxnut gravatar image

The problem is not in the line you suggested but in the line IplImage outx = src; Replace it with: IplImage outx = src.operator IplImage(); This is the recommended way to convert from a cv::Mat to an IplImage.

edit flag offensive delete link more

Comments

Thanks for the answer, but the problem i'm having is in that line, when i debug it the error appears in that line. I tried to run the code like this and it worked, but when i try to use it as a function of the class it doesn´t work.

int main() { const char
* wndNameOut = "Out", * name = "../1.bmp";

vector&lt;KeyPoint&gt; keyPoints;

namedWindow( wndNameOut, CV_GUI_NORMAL );

Ptr &lt; IplImage &gt; IplI = cvLoadImage(name);

Mat src(IplI);

SimpleBlobDetector blobDetector( params );
blobDetector.create("SimpleBlob");
blobDetector.detect( src, keyPoints );

for(int i=0; i&lt;keyPoints.size(); i++ )
{
    cv::floodFill(src,keyPoints[i].pt, Scalar::all((i+1)*255/(keyPoints.size()+1)));
}

imshow( wndNameOut, src );
waitKey();

}

Santiago-Molina gravatar imageSantiago-Molina ( 2013-04-19 14:41:14 -0600 )edit
1

answered 2013-04-19 12:48:51 -0600

berak gravatar image

aww, try Not to mix the old 1.0, IplImage and the new 2.0 cv::Mat api.

do yourself a favour and stick to cv::Mat , whenever possible.

void VideoSeg::bwlabel( const cv::Mat & src, cv::Mat & out ) 
{

    namedWindow( "wndNameOut", CV_GUI_NORMAL );
    src.convertTo(out, src.type(), 255.);  // or:  out = src * 255.0;
    imshow( "wndNameOut", out);            //The image is succesfully plotted
    SimpleBlobDetector blobDetector( params );
    blobDetector.create("SimpleBlob");

    blobDetector.detect(out, keyPoints );  // The problem appears in this line

    for(int i=0; i<keyPoints.size(); i++ )
    {
        cv::floodFill(out,keyPoints[i].pt, Scalar::all((i+1)*255/(keyPoints.size()+1)));
    }

    cout << "Keypoints " << keyPoints.size() << endl;
}
edit flag offensive delete link more

Comments

Thank you for your answer, right now i can´t pass all the code to the second version, that's why i need to do this operation.

Santiago-Molina gravatar imageSantiago-Molina ( 2013-04-19 14:58:15 -0600 )edit

Question Tools

Stats

Asked: 2013-04-19 12:03:02 -0600

Seen: 699 times

Last updated: Apr 19 '13