The standard (recommended) way to store image data in the disk is putting the pixels in an image container (jpg/png/bmp/tiff/...) and save that to a file. OpenCV handles this process very easily, since you already have img
as IplImage*
all you need to do is call cvSaveImage()
and give the name of the file:
if (!cvSaveImage("output.png", img))
{
// print cvSaveImage failed and deal with error
}
then, to read this file from the disk use can use cvLoadImage()
, which you have probably used already.
If, for some mysteryous reason you are actually storing just the pixels in a file it would be perfectly understandable that you want to read them back to an IplImage
structure. For this purpose, the function you are looking for is cvCreateImage()
:
IplImage* new_img = cvCreateImage(cvSize(width, height), /*img depth*/, /* nChannels*/);
after creating new_image
you need to mempcy()
the pixels read to new_image->imageData
.
However, you need to know beforehand what's the actual size of the image to be able to use this approach. If you are just storing the pixels of the image inside the file then I suggest you store the size of the image as a part of the filename.
One benefit from using OpenCV functions to deal with images is that your system will be able to recognize and display them (which is so important for debugging purposes).