1

如何正确地将 OpenCV IplImage 转换为 OpenSceneGraph 的 osg::Image?

这是我目前的方法。但我得到的颜色数据不正确。

// 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);
4

1 回答 1

1

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!

于 2012-06-04T14:38:12.203 回答