hello all
hello all

Reputation: 252

wxWidgets + OpenCV: wxBitmap does not draw wxImage correctly

I'm trying to make a wxImage out of a cv::Mat raw data, and have a wxBitmap draw it out on a wxPanel-derived class (this bmImage class).

Here's the code for loading the cv::Mat data into the wxImage.

cv::Mat asCvMat = cv::imread("something.png", 1);
cv::imshow("preview", asCvMat);
cv::Mat cvtResult;
aswxImage = wxImage(asCvMat.cols, asCvMat.rows, asCvMat.data, true);
wxLogMessage(wxT("%s"), std::string((char*)asCvMat.data));

And here's the code for drawing the wxBitmap out.

void bmImage::render(wxDC& dc) {
    wxBitmap bitmap = wxBitmap(aswxImage);
    dc.DrawBitmap(bitmap, wxPoint(0, 0), false);
}

enter image description here

The little image on the left is the image I try to load and showed with cv::imshow, and the one on the right is the resulting wxBitmap. It doesn't show the image correctly. How should I fix this to show the image properly?

Plus, what's confusing me is the (null) value of asCvMat.data. Does this mean that wxImage() has finished loading all the pixel data from it so it has reached the end (\0)?

EDIT

cv::Mat asCvMat = cv::imread("something.png", 1);
cv::imshow("preview", asCvMat);
void *mdata = malloc((size_t)(asCvMat.dataend - asCvMat.datastart));
std::memcpy(mdata, asCvMat.data, (size_t)(asCvMat.dataend - asCvMat.datastart));

cv::Mat cvtResult;
aswxImage = wxImage(asCvMat.cols, asCvMat.rows, (unsigned char*)mdata, false);

wxLogMessage(wxT("%s"), std::string((char*)mdata));

yet it's the same thing... sometimes it throws an unhandled memory exception if I load a large image.

Upvotes: 0

Views: 1931

Answers (2)

hello all
hello all

Reputation: 252

Solved by:

sizer_to_contain_image->Add(image, 1, wxEXPAND);

and:

sizer_containing_image->SetSizeHints(frame_containing_sizer);

Upvotes: 0

VZ.
VZ.

Reputation: 22753

Are you sure cv::Mat data is in the format expected by wxImage, i.e. simple RGB without any interleave?

Also, how exactly do you call render()? You can't/shouldn't call it directly, is it called from your wxEVT_PAINT handler?

Upvotes: 1

Related Questions