Sonic
Sonic

Reputation: 324

converting images between opencv and wxwidgets

I have got a big problem. After searching through the internet, I didn't find a good solution. I read in images from files via opencv (2.3) and manipulate them. Afterwards I want to present the result in my application written in wxwidgets (2.9.3). The main problem is, that my images are grayscale and so I just have got a single data pointer, but wxwidgets just use RGB. just a small example:

cv::imread(filename,CV_LOAD_IMAGE_GRAYSCALE).convertTo(pictureMatrix,CV_32F,(float)(1/2.0f),0);
// here are some more floating point calculations
cv::Mat output;
pictureMatrix.convertTo(output,CV_8U);
wxImage test(output.rows, output.cols, output.data, true);
wxInitAllImageHandlers();
// saving the picture is just for testing, if it works
test.SaveFile("test.png", wxBITMAP_TYPE_PNG);

Upvotes: 3

Views: 4010

Answers (2)

Sam
Sam

Reputation: 20058

All you need to to do is to convert from grayscale to RGB (actually, to BGR, if you're on Windows).

cv::Mat grayOutput, rgbOutput;
pictureMatrix.convertTo(grayOutput,CV_8U);
cvtColor(grayOutput, rgbOutput, CV_GRAY2BGR); // note the BGR here. 
//If on Linux, set as RGB
wxImage test(rgbOutput.cols, rgbOutput.rows,  rgbOutput.data, true);
...

Upvotes: 4

Vlad
Vlad

Reputation: 18633

You can always set R=G=B=<your grayscale value> for every pixel. If the format of the image pixels doesn't match up, you can allocate a new array in the format expected by wxImage and fill it with those RGB values.

You can also take a look at this link. It looks similar to what you need to do.

Upvotes: 1

Related Questions