I want to display a cv::Mat in a Gui written by gtkmm. So I have done a test.
I have a widget Gtk::Image image
, and I want to set the image with the following two methods:
// first method, display from file
void displayImage1()
{
Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_file("gtk.png");
image.set(pixbuf);
}
// second method, display from cv::Mat
void displayImage2()
{
cv::Mat outImage = cv::imread("gtk.png");
cv::cvtColor(outImage, outImage, CV_BGR2RGB);
Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_data(outImage.data, Gdk::COLORSPACE_RGB,false, 8, outImage.cols, outImage.rows, outImage.step);
image.set(pixbuf);
}
The first method works well.
However, the second method doesn't work well, I got a destroyed image on the screen as shown in the picture.
If I set the has_alpha parameter to true, the result is also strange (see pic. below).
Similar tests were done by using Gtk::DrawingArea. Different IDEs are used (but all g++ compiler under linux). All same results.
Update:
I tested lots of images. Sometimes the images are broken, sometimes the programs crashed with
The program has unexpectedly finished.