GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 23, 2014 12:39 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Loading an OpenCv into a GtkWindow
PostPosted: Wed Mar 24, 2010 4:57 pm 
Offline
Familiar Face

Joined: Thu Jan 21, 2010 10:23 am
Posts: 8
Hi there.

I have been wondering how to load an OpenCV image into a GtkWindow. There are some tips in the GTK forums which have help a lot. Anyway I want to share this code with all.

I hope it helps to somebody.

Code:
/**
* @file GUI.cpp
* @brief OpenCV Demo.
* @date Created on: 15/03/2010
* @author tArKi
* @version 1.0
*/

#include <gtk/gtk.h>
#include <glib.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <stdio.h>

gint t = 0;
IplImage* opencvImage;
GdkPixbuf* pix;

gboolean
expose_event_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{

  pix = gdk_pixbuf_new_from_data((guchar*) opencvImage->imageData,
      GDK_COLORSPACE_RGB, FALSE, opencvImage->depth, opencvImage->width,
      opencvImage->height, (opencvImage->widthStep), NULL, NULL);

  gdk_draw_pixbuf(widget->window,
      widget->style->fg_gc[GTK_WIDGET_STATE (widget)], pix, 0, 0, 0, 0,
      opencvImage->width, opencvImage->height, GDK_RGB_DITHER_NONE, 0, 0); /* Other possible values are  GDK_RGB_DITHER_MAX,  GDK_RGB_DITHER_NORMAL */

  return TRUE;
}

int
main(int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *drawing_area;
  gtk_init (&argc, &argv);

//Change this path in order to load any other image.
  opencvImage = cvLoadImage("./System/Blank.jpg", 1);
  cvCvtColor(opencvImage, opencvImage, CV_BGR2RGB);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_window_set_title(GTK_WINDOW (window), "RESET DEMO");
  g_signal_connect (G_OBJECT (window), "destroy",
      G_CALLBACK (gtk_main_quit), NULL);

  /* Now add a child widget to the aspect frame */
  drawing_area = gtk_drawing_area_new();

  /* Ask for a 200x200 window, but the AspectFrame will give us a 200x100
   * window since we are forcing a 2x1 aspect ratio */
  gtk_widget_set_size_request(drawing_area, opencvImage->width, opencvImage->height);
  gtk_container_add(GTK_CONTAINER (window), drawing_area);
  gtk_widget_show(drawing_area);

  g_signal_connect (G_OBJECT (drawing_area), "expose_event",
      G_CALLBACK (expose_event_callback), NULL);

  gtk_widget_show(window);
  gtk_main();
  return 0;
}


Top
 Profile  
 
 Post subject: Re: Loading an OpenCv into a GtkWindow
PostPosted: Sat Mar 26, 2011 7:39 am 
Offline
Familiar Face

Joined: Fri Mar 25, 2011 7:55 am
Posts: 35
Location: romania
djtarki wrote:
Hi there.

I have been wondering how to load an OpenCV image into a GtkWindow. There are some tips in the GTK forums which have help a lot. Anyway I want to share this code with all.

I hope it helps to somebody.



Hi !

thanks for example, here is my webcam streaming

Code:
#include <gtk/gtk.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <stdio.h>

gint t = 0;
IplImage* opencvImage;
GdkPixbuf* pix;
CvCapture* capture;

static gboolean time_handler( GtkWidget *widget ) {
  return TRUE;
}

gboolean expose_event_callback( GtkWidget      *widget,
                                GdkEventExpose *event,
                                gpointer        data) {
   gtk_widget_queue_draw( GTK_WIDGET( widget ));
   opencvImage = cvQueryFrame( capture );
   cvCvtColor( opencvImage, opencvImage, CV_BGR2RGB );
   pix = gdk_pixbuf_new_from_data(( guchar* )opencvImage->imageData,
                                             GDK_COLORSPACE_RGB,
                                             FALSE,
                                             opencvImage->depth,
                                             opencvImage->width,
                                             opencvImage->height,
                                             ( opencvImage->widthStep ),
                                             NULL, NULL);
   gdk_draw_pixbuf( widget->window,
                    widget->style->fg_gc[ GTK_WIDGET_STATE( widget )],
                    pix,
                    0, 0, 0, 0,
                    opencvImage->width,
                    opencvImage->height,
                    GDK_RGB_DITHER_MAX,
                    0, 0);
   return TRUE;
}

int main( int argc, char *argv[] ) {
   GtkWidget *window;
   GtkWidget *drawing_area;

   gtk_init (&argc, &argv);
   capture = cvCaptureFromCAM( CV_CAP_ANY );
   opencvImage = cvQueryFrame( capture );
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title( GTK_WINDOW( window ), "WEBCAM");
   g_signal_connect( G_OBJECT( window ), "destroy", G_CALLBACK (gtk_main_quit), NULL);
   drawing_area = gtk_drawing_area_new();
   gtk_widget_set_size_request( drawing_area, opencvImage->width, opencvImage->height );
   gtk_container_add( GTK_CONTAINER( window ), drawing_area );
   gtk_widget_show( drawing_area );
   g_signal_connect( G_OBJECT( drawing_area), "expose_event", G_CALLBACK (expose_event_callback), NULL);
   g_timeout_add( 100, ( GSourceFunc )time_handler, ( gpointer )drawing_area );
   gtk_widget_show(window);
   gtk_main();
   cvReleaseCapture( &capture );
   return 0;
}

_________________
c/c++ for ever


Top
 Profile  
 
 Post subject: Re: Loading an OpenCv into a GtkWindow
PostPosted: Mon Mar 26, 2012 11:35 am 
Offline

Joined: Fri Mar 23, 2012 5:33 am
Posts: 3
Thanks a lot for the code.... trust me you are helped me a lot..... thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group