How to save IplImage?

When we have IplImage , how can we save it so that we can use it later or view it as an image outside our code (for example, via png or jpeg)?

As an example of code, I have the following:

void SaveImage() { CvSize size; IplImage *rgb_img; int i = 0; size.height = HEIGHT; size.width = WIDTH; rgb_img = cvCreateImageHeader(size, IPL_DEPTH_8U, 3); rgb_img->imageData = my_device.ColorBuffer; rgb_img->imageDataOrigin = rgb_img->imageData; /*for (i = 2; i < rgb_img->imageSize; i+= 3) { // confirming all values print correctly printf("%d, ", rgb_img->imageData[i]); }*/ cvSaveImage("foo.png",rgb_img); } 

I printed all the values ​​in the loop of commented out loops, and it seems that the data is in the buffer correctly. Using cvShowImage to display an image also works correctly, so the image structure seems to be in order.

+7
source share
2 answers
 void SaveImage() { CvSize size; IplImage *rgb_img; int i = 0; size.height = HEIGHT; size.width = WIDTH; rgb_img = cvCreateImageHeader(size, IPL_DEPTH_8U, 3); rgb_img->imageData = my_device.ColorBuffer; // You should NOT have the line below or OpenCV will try to deallocate your data //rgb_img->imageDataOrigin = rgb_img->imageData; for (i = 0; i < size.height; i++) { for (j = 0;j < size.width; j++) { // confirming all values print correctly printf("%c, ", rgb_img->imageData[i*width + j]); } } cvSaveImage("foo.png",rgb_img); } 

Running this should not crash.

Some problems with your code

  • You use% f to print, but IPL_DEPTH_8U is a 1 byte uchar
+5
source

To save:

 cvSaveImage(outFileName,img) 

If you want to verify that it has been saved, you can do the following:

  if(!cvSaveImage(outFileName,img)) printf("Could not save: %s\n",outFileName); 

Taken from http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00052000000000000000 - the best result on Google for "opencv write iplimage".

+1
source

All Articles