1

I am facing a problem while writing image from cvMat.

This is what I have done.

IplImage* low_threshold_mask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
CvMat* labelMat = cvCreateMat(low_threshold_mask->height,low_threshold_mask->width,CV_32F);

/* I populate elements of labelMat inside a function. Its done like this: cvmSet(labelMat,r,c,label); // where label is of type long */

To check the values I dump each pixel value in a text file and also dump the image.

IplImage* labelImg;
IplImage imageHeader;
labelImg = cvGetImage(labelMat, &imageHeader);

Now when I cross-check pixel intensity with corresponding value in dumped text file, I find mis-match. I feel I have got correct values in text file but wrong ones in image.

Can anyone help in figuring out the mistake?


---------------------New addition-------------------

I am still facing the problem. I have uploaded my programs. I will explain where exactly I am facing the error.

Libraries used: Along with openCV, I am using disjoint_sets of boost library.

Basically I am doing connected component labeling. For debugging purpose, for 20th frame, I have dumped the label info of each pixel both in a)text file as well b) an image with intensity levels same as the final label of the pixels. So i am expecting the values same in both text and image. But that's not happening. And I am unable to figure out why. The text files shows the correct values but not the image. I am checking pixel values of image in Matlab(i had taken care of indices in matlab starts with 1 not 0).

My text files a) (frame20final.txt) gets populated in GrimsonGMM.cpp/ConCompLabeling(). b) (frame20image.txt) gets populated in main.cpp My dumped image(frame-ccs.jpg) gets populated in main.cpp.

Both the text files get same values. So there must be some mistake in writing the image from CvMat.

Test Video: person15_walking_d1_uncomp.avi You can try with any other video also.

Thanks in advance, Kaushik

Kaushik Acharya
  • 1,520
  • 2
  • 16
  • 25

2 Answers2

1

I understood why I was getting the error. I was dumping in .jpg image which was doing compression. This was resolved when I used .png

Kaushik Acharya
  • 1,520
  • 2
  • 16
  • 25
0

Your question its so easy.

You want to work with CvMat and after do operations with CvMat you want to plot your CvMat like it was an image.

You must create imageHeader, something like this.

CvMat* mat = cvCreateMatHeader(rows, cols, type);
mat->step = 4 * (mat->cols * CV_ELEM_SIZE1(mat->type) * CV_MAT_CN(mat->type) / 4 + 1);//critical
cvCreateData(mat);

In OpenCV 2.0 and below C++ interface it's not really necessary to change from Mat to IplImage.

You can plot your image using cvShowImage and if you want to convert intoIplImage just do a easy cast IplImage *img = labelMat;

Jorge Vega Sánchez
  • 7,430
  • 14
  • 55
  • 77
  • But shouldn't cvGetImage also work as explained by rics in http://stackoverflow.com/questions/2468307/how-to-convert-a-mat-variable-type-in-an-iplimage-variable-type-in-opencv-2-0/2475735#2475735 – Kaushik Acharya Mar 06 '11 at 01:12
  • Plus in OpenCV 2.0 it gives the error: cannot convert from 'CvMat *' to 'IplImage *' – Kaushik Acharya Mar 06 '11 at 01:21