The function
Code: Select all
bool ConvertMatBitmapTowxBitmap(const cv::Mat& matBitmap, wxBitmap& bitmap)
All the code and additional information is available here:
https://github.com/PBfordev/wxopencvtest
UI of the test app
Code: Select all
bool ConvertMatBitmapTowxBitmap(const cv::Mat& matBitmap, wxBitmap& bitmap)
If you want to display an OpenCV image with wxWidgets, you need a wxBitmap, not a wxImage. If you just want to save an image, you can use OpenCV functions, without converting the image to wxImage.
Code: Select all
wxStopWatch stopWatch;
long time = 0;
stopWatch.Start();
cv::Size sz = img.size();
int imageWidth = sz.width;
int imageHeight = sz.height;
cv::cvtColor(img, img, cv::COLOR_RGB2BGR);
wxImage Image( imageWidth, imageHeight, img.data, TRUE );
wxBitmap bp= wxBitmap(Image, -1); // ...to get the corresponding bitmap
time = stopWatch.Time();
wxLogMessage("Convert Time=%d", time);
Code: Select all
-lgdi32
TBH, I do not understand how the old code works. I know little about OpenCV but I would say this
Code: Select all
cv::cvtColor(img, img, cv::COLOR_RGB2BGR);
This is odd, CMake should have added this library automatically when linking with wxCore. It did for me both with GCC and MSVC.ollydbg23 wrote: ↑Tue Dec 22, 2020 2:48 am By the way, I have to add a linker option, because you use the Win32 API function: SetDIBits in your library. Otherwise, you will get a linker error.Code: Select all
-lgdi32
wxBitmap can have an alpha channel (and a mask as well) but as I wrote before, I believe images with alpha are rarely coming from OpenCV.
You are right, the OpenCV cv::Mat default is the BGR. So, my code should be :cv::COLOR_BGR2RGB, because wxImage is RGB.PB wrote: ↑Tue Dec 22, 2020 4:25 pmTBH, I do not understand how the old code works. I know little about OpenCV but I would say thisconverts the mat from RGB to BGR colorspace. However, wxImage expects RGB (and OpenCV bitmaps are in BGR by default)?Code: Select all
cv::cvtColor(img, img, cv::COLOR_RGB2BGR);
Anyway, he old code was not very efficient if you wanted to convert cv::Mat to wxBitmap as it did:
1. Convert the mat bitmap from one colorspace to another.
2. Created a wxImage (this should be very fast).
3. Converted a wxImage to wxBitmap.
I'm directly adding your h/cpp file to my Codeblocks cbp file, so I have to add this linker option manually.This is odd, CMake should have added this library automatically when linking with wxCore. It did for me both with GCC and MSVC.ollydbg23 wrote: ↑Tue Dec 22, 2020 2:48 am By the way, I have to add a linker option, because you use the Win32 API function: SetDIBits in your library. Otherwise, you will get a linker error.Code: Select all
-lgdi32
I still do not understand how could your program link to wxCore but not to GDI32 and still build. But who knows...
TBH, converting from CV::Mat to wxBitmap is trivial. The only purpose I wrote an extra function like that was to use when possible the fast native function exploiting that CV_8UC3 is stored the same way in cv::Mat as in a Windows DIB.
But this is entirely within OpenCV, right? I have no experience with the OpenCV parallel framework.