Ask Your Question

cv::Mat and OpenCV version

asked 2013-01-08 01:16:58 -0600

thomas11 gravatar image

Dear All, What is the opencv version with which we can start use cv::Mat? My opencv version is 2.4.1 and I am trying to convert from IplImage* to cv::Mat. I always have the error of unavailable synchronous data. Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-01-08 09:39:50 -0600

Michael Burdinov gravatar image

updated 2013-01-09 07:06:20 -0600

If I remember correctly Mat was introduced in OpenCV 2.0. The conversion can be done in the most straightforward way. What is sounds surprising to me is the error about 'synchronous data' that seems completely irrelevant. Can you please explain how you perform the conversion, and for what purpose?


I run your code and stored resulting image on the disk. Created image was OK. How do you look into the data? Maybe you are accessing it in some wrong way.

Next points are not related to question but are worth noting. There pretty much mess in the code. Lets try to sort out the problems. First lets deal with your first approach:

1) You are initializing Mat from constructor that returns IplImage. This will compile and run, but it will leak memory. cvCreateImage() is creating IplImage object, but there no IplImage object that may relesae it later. Your 'original' image is Mat object that is just another header that points to the same memory, and so it won't release it either.

2) Are you sure that w=col_size and h=row_size?

3) It is sort of pointless to cast 20 to char. Unlike IplImage the 'data' in Mat is array of unsigned char not array of char. Besides this cast will happen anyway even if you won't write it.

4) There no point in double for loop in order to set all values. Use setTo() if you work with Mat or cvSet() if you work with IplImage. Or just provide the value to constructor.

Now about second approach:

1) First you create IplImage object. So far its memory buffer is allocated but not initialized (i.e. can contain anything). Then you are creating Mat object with same parameters as IplImage, and forcing it to copy this memory buffer. What for? It is exactly the same as creating completely unrelated Mat.

2) Then you are taking image from ImageQueue without releasing previous one. This is again memory leak. B.t.w., what is this ImageQueue?

3) Then you are setting your 'original' image to point to the same place as current opcvImg. What was the point of initializing it before? After your OR statement you actually made copy of data, not just another pointer to existing data but the question remains.

Are you completelly sure you need IplImage at all? There almost nothing you can do with IplImage that you can't do with Mat.

edit flag offensive delete link more


Yeah it should be straightforward. I tried two approaches. (1)Mat original = cvCreateImage(cvSize(col_size, row_size), IPL_DEPTH_8U,1); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) {[y * original.step + x] = (char)20;

} }

Then I look at the, it is unavailable synchronous data.

IplImage *opcvImg = cvCreateImage(cvSize(col_size, row_size), IPL_DEPTH_8U,1);

Mat original = Mat(opcvImg, true);

opcvImg = ImageQueue::getQueue();//get the IplImage from the queue, the data is perfect

Mat original = cvarrToMat(opcvImg);


Mat original = Mat(opcvImg, true);

Then original shows unavailable synchronous data. Thanks

thomas11 gravatar imagethomas11 ( 2013-01-08 20:25:39 -0600 )edit

Question Tools


Asked: 2013-01-08 01:16:58 -0600

Seen: 593 times

Last updated: Jan 09 '13