Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Mat to IplImage* conversion

Tags:

opencv

I have a pointer to image:

IplImage *img;

which has been converted to Mat

Mat mt(img);

Then, the Mat is sent to a function that gets a reference to Mat as input void f(Mat &m);

f(mt);

Now I want to copy back the Mat data to the original image.

Do you have any suggestion?

Best Ali

like image 845
Edi Avatar asked May 24 '11 16:05

Edi


2 Answers

Your answer can be found in the documentation here: http://opencv.willowgarage.com/documentation/cpp/c++_cheatsheet.html

Edit:

The first half of the first code area indeed talks about the copy constructor which you already have.

The second half of the first code area answers your question. Reproduced below for clarity.

//Convert to IplImage or CvMat, no data copying
IplImage ipl_img = img;
CvMat cvmat = img; // convert cv::Mat -> CvMat
like image 158
peakxu Avatar answered Oct 26 '22 17:10

peakxu


For the following case:

double algorithm(IplImage* imgin)
{
    //blabla
    return erg;
}

I use the following way to call the function:

cv::Mat image = cv::imread("image.bmp");
double erg = algorithm(&image.operator IplImage());

I have made some tests and how it looks the image object will manage the memory. The operator IplImage() will only construct the header for IplImage. Maybe this could be useful?

like image 33
jamk Avatar answered Oct 26 '22 17:10

jamk