6
votes

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

3
The conversion Mat mt(img) does not do any copying of the imagedata, so you don't have to do anything, as long as you don't do an operation which reallocates (eg. assign mt to an independent other Mat).Barney Szabolcs

3 Answers

2
votes

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
1
votes

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?

0
votes

You can use this form:

Your Code:

plImage *img;

Mat mt(img);

f(mt);

Now copy back the Mat data to the original image.

img->imageData = (char *) mt.data;

You can also copy the data instead of pointer:

memcpy(mt.data, img->imageData, (mt.rows*mt.cols));

(mt.rows*mt.cols) is the size that you should use for copy all data the mt to img.

Hope I helped