Search code examples
c++opencvwxwidgets

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

Solution

  • 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);
    ...