Mutual conversion of iplimage and single byte char* in OpenCV
From Iplimage to char*: data = image->imagedata//aligned image, or data = image->imagedataorigin//unaligned raw images from char* to Iplim Age:image =cvcreateimageheader (Cvsize (width,height), depth, channels); Cvsetdata (image, data, step);
Step Specify Iplimage the number of bytes per line of the image. It is important to note that you cannot use cvreleaseimage directly while freeing up space, and you need to cvreleaseimageheader and then delete data, which is also the idea of "managing memory" inside OPENCV.
Iplimage There are two properties that are worth paying attention to, and a little inattention can lead to errors: One is the width property, and the other is the Widthstep property. The former is the number of pixels per line representing the image, and the latter refers to how many bytes are required to store a single row.
Iplimage *img=cvloadimage (m_strfile,0); int height,widht,step,channel;height=img->height;widht=img->width; step=img->widthstep;channel=img->nchannels;
It can be seen that Iplimage's widthstep is not equal to Width*channel, which is 4-byte aligned.
Example: Grayscale of images
width-image width//height-image high//pdata-input image data pointer (DIB)//linebytein-input image per line of bytes//pdataout-output image data pointer iplimage *img= Cvcreateimageheader (Cvsize (width,height), ipl_depth_8u,3); Cvsetdata (Img,pdata,linebytein); IplImage *des_gray= Cvcreateimage (Cvgetsize (IMG), img->depth,1); Cvcvtcolor (Img,des_gray,cv_rgb2gray); memcpy (pdataout,des_gray- >imagedata,des_gray->widthstep*des_gray->height); Cvreleaseimageheader (&img); CvReleaseImage (& Des_gray);
Conversion of Iplimage and char * in OpenCV, and very easy to overlook details