开发者

writing to IplImage imageData

I want to write data directly into the imageData array of an IplImage, but I can't find a lot of information on how it's formatted. One thing that's particularly troubling me is that, despite creating an image with three channels, there are four bytes to each pixel.

The function I'm using to create the imag开发者_StackOverflow中文版e is:

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

By all indications, this should create a three channel RGB image, but that doesn't seem to be the case.

How would I, for example, write a single red pixel to that image?

Thanks for any help, it's get me stumped.


If you are looking at frame->imageSize keep in mind that it is frame->height * frame->widthStep, not frame->height * frame->width.

BGR is the native format of OpenCV, not RGB.

Also, if you're just getting started, you should consider using the C++ interface (where Mat replaces IplImage) since that is the future direction and it's a lot easier to work with.

Here's some sample code that accesses pixel data directly:

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;
}


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

Take Your image that is a dimensional array of height N and width M and arrange it into a row-wise vector of length N*M. Make this of type unsigned char* for IPL_DEPTH_8U images.


Straight to your answer, painting the pixel red:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜