Ask Your Question
0

How to convert cv::Mat to Gdk::Pixbuf?

asked 2020-04-11 22:55:56 -0600

Deng gravatar image

updated 2020-04-20 03:18:21 -0600

Hello, everyone

Recently, I am trying to write a GUI for my image processing application(commandline), using Gtkmm. After processing a image using opencv, I want to display the result in a Gtkmm' s Image widget. The following is the code I used to setup Gtk::Image:

cv::Mat temp;
const int width = img_window.get_allocation().get_width();
const int height = img_window.get_allocation().get_height();
cv::resize(fusion_img, temp, cv::Size(width, height));
cv::cvtColor(temp, temp, cv::COLOR_BGR2RGB);

img.set(Gdk::Pixbuf::create_from_data(temp.data, Gdk::COLORSPACE_RGB, false, 8, width, height, temp.step[0]));

but it display a damaged image. I need some advice to make it right. Thank you.

edit retag flag offensive close merge delete

Comments

You should post a screenshot of the displayed image.

Also, you should print the value of temp.step[0], width and height.

Eduardo gravatar imageEduardo ( 2020-04-20 05:27:05 -0600 )edit
1

@Eduardo Same question asked on the stack overflow with no answer:

link text

The key issue is how to determine rowstride parameter. Prototype of:

Gdk::Pixbuf::create_from_data() :

Gdk::Pixbuf::create_from_data(const guint8 * data,
                                                  Colorspace    colorspace,
                                                             bool   has_alpha,
                                                               int  bits_per_sample,
                                                               int  width,
                                                               int  height,
                                                               int  rowstride)
Deng gravatar imageDeng ( 2020-05-06 20:50:28 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
-1

answered 2020-05-24 19:47:59 -0600

Deng gravatar image

updated 2020-05-24 20:51:47 -0600

supra56 gravatar image

1. convert cv::Mat to CvMat:

cv::Mat img = ...;

 CvMat des_img(img);

2. convert CvMat to Gdk::Pixbuf:

auto img_buf = Gdk::Pixbuf::create_from_data(des_img.data.ptr, Gdk::COLORSPACE_RGB, false, 8, des_img.rows, des_img.cols, des_img.step)

NOTE: CvMat is defined in opencv2/core/types_c.h header file. It may work...

edit flag offensive delete link more

Comments

CvMat is from the deprecated (and now removed !) opencv1.0 C-api.

so -- terrible answer !

berak gravatar imageberak ( 2020-05-25 01:45:04 -0600 )edit
1

types_c.h can be found in newest OpenCV version 4.3, CvMat is not removed. Check out the source code!

Deng gravatar imageDeng ( 2020-05-25 20:04:18 -0600 )edit

Why do you want to use CvMat? This is obsolete.

Eduardo gravatar imageEduardo ( 2020-05-26 14:04:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-11 22:55:56 -0600

Seen: 1,175 times

Last updated: May 24 '20

Related questions