Conversion of Iplimage and char * in OpenCV, and very easy to overlook details

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.