1
votes

How can I correctly convert an OpenCV IplImage to OpenSceneGraph's osg::Image?

This my current method. But I'm getting incorrect color data.

// IplImage* cvImg is a webcam output image captured using cvQueryFrame(capture)
osg::ref_ptr<osg::Image> osgImage = new osg::Image;
osgImage->setImage(cvImg->width,cvImg->height, 3,
                           GL_RGB, GL_RGB, GL_UNSIGNED_BYTE,
                           (BYTE*)(cvImg->imageData),
                           osg::Image::AllocationMode::NO_DELETE,1);
1

1 Answers

1
votes

This is likely an issue involving OpenCV's native BGR color space. You don't mention which version of OpenCV you are using, but modern versions define CV_BGR2RGB for use with cvCvtColor. Possibly doing the conversion like this

IplImage* pImg = cvLoadImage("lines.jpg", CV_LOAD_IMAGE_COLOR);
cvCvtColor(pImg, pImg, CV_BGR2RGB);
cvReleaseImage(&pImg);

If you can't use that another option would be use cvSplit to separate and reorder the channels and then combine them with cvMerge.

On a side note, I would definitely recommend using the C++ interface as it is much easier memory management, and has more features than the C interface.

Hope that helps!