————————–pro——————————————-
INCLUDEPATH += /usr/include/gtk-3.0 \
/usr/include/at-spi2-atk/2.0 \
/usr/include/at-spi-2.0 \
/usr/include/dbus-1.0 \
/usr/lib/x86_64-linux-gnu/dbus-1.0/include \
/usr/include/gtk-3.0 /usr/include/gio-unix-2.0 \
/usr/include/cairo /usr/include/pango-1.0 \
/usr/include/harfbuzz \
/usr/include/pango-1.0 \
/usr/include/fribidi \
/usr/include/harfbuzz \
/usr/include/atk-1.0 \
/usr/include/cairo \
/usr/include/pixman-1 \
/usr/include/uuid \
/usr/include/freetype2 \
/usr/include/gdk-pixbuf-2.0 \
/usr/include/libpng16 \
/usr/include/x86_64-linux-gnu \
/usr/include/libmount \
/usr/include/blkid \
/usr/include/glib-2.0 \
/usr/lib/x86_64-linux-gnu/glib-2.0/include\
/usr/include/
LIBS += -pthread \
-pthread \
-lgtk-3 \
-lgdk-3 \
-lpangocairo-1.0 \
-lpango-1.0 \
-lharfbuzz \
-latk-1.0 \
-lcairo-gobject \
-lcairo \
-lgdk_pixbuf-2.0 \
-lgio-2.0 \
-lgobject-2.0 \
-lglib-2.0 \
-lserialport
INCLUDEPATH += /home/yue/openCV/install/include\
/home/yue/openCV/install/include/opencv4 \
/home/yue/openCV/install/include/opencv2
LIBS += /home/yue/openCV/install/lib/libopencv_highgui.so \
/home/yue/openCV/install/lib/libopencv_core.so \
/home/yue/openCV/install/lib/libopencv_imgproc.so \
/home/yue/openCV/install/lib/libopencv_imgcodecs.so
SOURCES += \
main.cpp
—————————main.cpp————————————
#include
#include
#include
// Function to convert OpenCV Mat to GdkPixbuf
GdkPixbuf* mat_to_pixbuf(const cv::Mat& mat) {
int width = mat.cols;
int height = mat.rows;
int stride = width * 3; // Assuming 3 channels (RGB)
// Create a GdkPixbuf with the same size as the OpenCV Mat
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data(
mat.data,
GDK_COLORSPACE_RGB,
false,
8,
width,
height,
stride,
NULL,
NULL
);
return pixbuf;
}
// Callback function to load and display an image
void on_load_image(GtkButton *button, gpointer user_data) {
GtkWidget *image_widget = GTK_WIDGET(user_data);
// Load image using OpenCV
cv::Mat img = cv::imread(“/home/yue/monkey.png”); // Change to your image file
if (img.empty()) {
g_print(“Failed to load image.\n”);
return;
}
// Convert OpenCV Mat to GdkPixbuf
GdkPixbuf *pixbuf = mat_to_pixbuf(img);
// Set the image in the GTK image widget
gtk_image_set_from_pixbuf(GTK_IMAGE(image_widget), pixbuf);
// Unreference the GdkPixbuf when done
g_object_unref(pixbuf);
}
// Main function
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *image;
GtkWidget *button;
// Initialize GTK
gtk_init(&argc, &argv);
// Create a new GTK window
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), “GTK + OpenCV Example”);
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
// Set up a signal handler to exit the program when the window is closed
g_signal_connect(window, “destroy”, G_CALLBACK(gtk_main_quit), NULL);
// Create a vertical box to hold widgets
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_container_add(GTK_CONTAINER(window), vbox);
// Create an image widget
image = gtk_image_new();
gtk_box_pack_start(GTK_BOX(vbox), image, TRUE, TRUE, 0);
// Create a button to load the image
button = gtk_button_new_with_label(“Load Image”);
gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
// Connect the button click signal to the callback function
g_signal_connect(button, “clicked”, G_CALLBACK(on_load_image), image);
// Show all widgets in the window
gtk_widget_show_all(window);
// Enter the GTK main loop
gtk_main();
return 0;
}