Writing to IplImage imageData

I want to write data directly to the imageData array from IplImage, but I can not find much information about how it is formatted. What bothers me most is that despite creating an image with three channels, each pixel has four bytes.

The function I use to create the image:

IplImage *frame = cvCreateImage(cvSize(1, 1), IPL_DEPTH_8U, 3);

According to all indications, this should create a three-channel RGB image, but this does not seem to be the case.

How could I, for example, write one red pixel to this image?

Thanks for any help, it puzzles me.

+5
source share
3 answers

If you are watching frame->imageSize, keep in mind that it frame->height * frame->widthStepis not frame->height * frame->width.

BGR - OpenCV, RGB.

, , ++ ( Mat IplImage), , .

, :

int main (int argc, const char * argv[]) {

    IplImage *frame = cvCreateImage(cvSize(41, 41), IPL_DEPTH_8U, 3);

    for( int y=0; y<frame->height; y++ ) { 
        uchar* ptr = (uchar*) ( frame->imageData + y * frame->widthStep ); 
        for( int x=0; x<frame->width; x++ ) { 
            ptr[3*x+2] = 255; //Set red to max (BGR format)
        }
    }

    cvNamedWindow("window", CV_WINDOW_AUTOSIZE);
    cvShowImage("window", frame);
    cvWaitKey(0);
    cvReleaseImage(&frame);
    cvDestroyWindow("window");
    return 0;
}
+8

unsigned char * imageData = [r1, g1, b1, r2, g2, b2,..., rN, bn, gn];//n = * frame- > imageData = imageData.

, N M - N * M. unsigned char * IPL_DEPTH_8U.

0

, :

IplImage *frame = cvCreateImage(cvSize(1, 1), IPL_DEPTH_8U, 3);
int y,x;
x=0;y=0; //Pixel coordinates. Use this for bigger images than a single pixel.
int C=2; //0 for blue, 1 for green and 2 for red (BGR is the default format).
frame->imageData[y*frame->widthStep+3*x+C]=(uchar)255;
0

All Articles