Converting pixels to IplImage*

I am trying to convert pixels (retreived from pixels = ofVideoPlayer.getPixels() ) to IplImage type. But for some reasons, executing the code below returns a video with color channels being imbalanced. How do I copy all the channels (R, G, B) from pixels to IplImage?

IplImage *image;
unsigned char* pixels;

pixels = ofVideoPlayer.getPixels();

if( image->width == image->widthStep )
{
memcpy( image->imageData, pixels, image->width*image->height*3);
}
else
{
for( int i=0; i < image->height; i++ )
{
memcpy( image->imageData +(i*image->widthStep), pixels + (i*image->width*3),image->width*3 );
}
}

http://codepad.org/Ho7GzoO4 <–in case the code above becomes unreadable

Use ofxCvColorImage::setFromPixels to create an image from pixel data, and ofxCvColorImage::getCvImage() to get a pointer to IplImage.

Right, that is a possible work around. But I need to use openCV 2.0 and that’s why I couldn’t use the ofxOpenCV functions.

But it should be possible to get the R, G, B channels from ofVideoPlayer.getPixels().

Any ideas, anyone?

i’m pretty sure OpenCV 2 uses the same IplImage* internally. in any case you could have a look inside the ofxOpenCv code to see how setFromPixels does it, then copy/paste and adapt the code for your case. you’re probably better sticking of ofxCv*Image if you can, because managing IplImages by hand can be a bit of a pain. you should be able to use just the ofxCv*Image classes to your project and use them, leaving behind the other code.

damian: the code that pasted in my first message is from setFromPixels function. In other words, coping the same exact code is not working out.

Once again the link is: http://codepad.org/Ho7GzoO4

Hopefully, one of the openframework developers can look at it and point to a solution.