Octopus
Octopus

Reputation: 8325

I'm trying to convert pixel data to an OpenCV Mat object

I have raw pixel data that I want to output via the opencv cvShowImage() function.

I have the following code:

#include <opencv2/highgui/highgui.hpp>

// pdata is the raw pixel data as 3 uchars per pixel
static char bitmap[640*480*3];
memcpy(bitmap,pdata,640*480*3);
cv::Mat mat(480,640,CV_8UC3,bitmap);

std::cout << mat.flags << ", "
          << mat.dims  << ", "
          << mat.rows  << ", "
          << mat.cols  << std::endl;

cvShowImage("result",&mat);

Which outputs:

1124024336, 2, 480, 640

to the console, but fails to output the image with cvShowImage(). Instead throwing an exception with the message:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat

I suspect the problem is in the way I create the mat object, but I am having a very hard time finding any more specific information on how I am supposed to do that.

I don't think CV_8UC3 is enough of a description for it to render the array of data. Doesn't it have to know whether the data is RGB or YUY2, etc.? How do I set that?

Upvotes: 1

Views: 2274

Answers (1)

Roger Rowland
Roger Rowland

Reputation: 26259

Try cv::imshow("result", mat) instead of mixing the old C and new C++ APIs. I expect casting a Mat to a CvArr* is the source of the problem.

So, something like this:

#include <opencv2/highgui/highgui.hpp>

// pdata is the raw pixel data as 3 uchars per pixel
static char bitmap[640*480*3];
memcpy(bitmap,pdata,640*480*3);
cv::Mat mat(480,640,CV_8UC3,bitmap);

std::cout << mat.flags << ", "
          << mat.dims  << ", "
          << mat.rows  << ", "
          << mat.cols  << std::endl;

cv::imshow("result", mat);

Upvotes: 1

Related Questions