简体   繁体   中英

Converting osg image to opencv mat for processing

Edit: The problem has since been solved, but the root of the problem lies elsewhere, the following is left as-is to help others who might need to do conversions OpenCV<->OSG

In OSG, I have an osg::ImageStream , and an osg::Image . The ImageStream basically stores and plays a pre-recorded video during runtime, while the Image acts as an overlay to present additional information.

The information in this case is processed video information using OpenCV algorithms, and thus I needed to allow OpenCV some form of access to manipulate the Image's data.

I need the Image to be in RGBA format so that unused pixels can be masked out, and this is how I declared the Image :

osg::ref_ptr<osg::Image> opencvOverlay = new osg::Image;
int width = 1920;
int height = 1200;
int size = width*height * 4; //RGBA, 4 channels
unsigned char* data = (unsigned char*)calloc(size, sizeof(unsigned char));
for (long i = 0; i < size; i += 4)
{
    data[i    ] = 0;    //red 
    data[i + 1] = 0;    //green 
    data[i + 2] = 0;    //blue 
    data[i + 3] = 0;    //alpha
}
opencvOverlay->setOrigin(osg::Image::BOTTOM_LEFT);
opencvOverlay->setImage(width, height, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, data, osg::Image::NO_DELETE);

While on the OpenCV side of things, I define a cv::Mat that can manipulate the internal data

IplImage* myIplImage = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 4);
cvSetData(myIplImage, opencvOverlay->data(), width * 4);
cv::Mat* overlay = new cv::Mat(myIplImage);

Such that the variable overlay can be used to output results of OpenCV algorithm.

However, before I proceeded to test any algorithm, i tried to doublecheck whether it is a valid cv::Mat, so I added the following:

cv::imshow("Mat", *overlay);

When the program compile and runs, it exits with the error

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow

Any idea what I'm doing wrong?

You may use cv::cvarrToMat. It is in core.hpp and in the cv:: namespace

cv::Mat overlay = cv::cvarrToMat(myIplImage);
cv::imshow("Mat", overlay);
waitKey(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM