GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 22, 2014 5:58 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Solved]Problem OpenCV and continuous image frames(video) gtk
PostPosted: Wed Feb 03, 2010 9:43 pm 
Offline
Familiar Face

Joined: Wed Feb 03, 2010 9:23 pm
Posts: 22
I am working with GIGE camera in opencv 1.0.0 and GTK+2.0 using gcc. I am interested in streaming live video grabbing individual image frames. I have been able to grab just single frame. My problem is how do I update image on the gtkwidget. or repaint it. I know that my pixbuf is being updated and I cannot put "image" widget inside loop or insertion will fail. How do I tell imy "image" widget to get the new value of pix buffer which is being updated by my capture subroutine? I tried using timer and "expose_event" so far no luck :cry: Any help would be appreciated.

[code=]


//===================gtk timer and expose event==============================================
static gboolean
on_expose_event(GtkWidget *widget,
GdkEventExpose *event,
gpointer data)
{

capture_image();
//OpenCV clean up
cvReleaseImage(&bayerImage);
cvReleaseImage(&rgbImage);
return FALSE;
}


static gboolean
time_handler(GtkWidget *widget)
{
//capture_image();
gtk_widget_queue_draw(widget);
cvWaitKey(10);
printf("hello!\n");
return TRUE;
}

//===================end of gtk timer and expose event========================================
.
.
.
int main (int argc, char *argv[]) {

//int key = 0;

capture_init();
printf("Camera ID \t\t= %ld\n", myCamera.UID);
gtk_init(&argc, &argv);


capture_image();
image = gtk_image_new_from_pixbuf (pix);

.
.
.
g_signal_connect(G_OBJECT(window), "expose-event",
G_CALLBACK(on_expose_event), NULL);

.
.
.g_timeout_add(10, (GSourceFunc) time_handler, (gpointer) window);
.
.
.
}//end main

[/code]


Last edited by virgoptrex@yahoo.com on Fri Mar 26, 2010 4:09 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: ok found the solution and got it working :)
PostPosted: Fri Feb 05, 2010 4:11 pm 
Offline
Familiar Face

Joined: Wed Feb 03, 2010 9:23 pm
Posts: 22
Solved this after 4 days x 15 hours of struggle as every newbie to GTK would have to go! Try this and simply replace gdk_draw_arc() function!
Code:
/* Copyright(c) 2010 Virgoptrex. Feel free to use. But leave credits intact :). */
/* My little gift :D   to the Open Source Community */
#include <gtk/gtk.h>
   
gint t=0;
 
     gboolean
     expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
     {

     gtk_widget_queue_draw(GTK_WIDGET(widget));

     //g_object_ref_sink (widget->window);
     
     // gdk_drawable_ref (widget->window);

       gdk_draw_arc (widget->window,
                     widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
                     TRUE,
                     0, 0, widget->allocation.width, widget->allocation.height,
                     0, 64 * 18*t);
     
      //gdk_drawable_unref (widget->window);
     


       return TRUE;
     }



static gboolean
time_handler(GtkWidget *widget)
{   

 

  if (t<20)
  { t++; }
  else if (t >=20)
  { t=0; }

printf("hello %d\n",t);
   return TRUE;


}

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


    gtk_window_set_title (GTK_WINDOW (window), "Aspect Frame");
    g_signal_connect (G_OBJECT (window), "destroy",
                 G_CALLBACK (gtk_main_quit), NULL);
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
   
    /* Create an aspect_frame and add it to our toplevel window */
   
    aspect_frame = gtk_aspect_frame_new ("2x1", /* label */
                                         0.5, /* center x */
                                         0.5, /* center y */
                                         1.33, /* xsize/ysize = 640/480 = 1.33 */
                                         FALSE /* ignore child's aspect */);
   
    gtk_container_add (GTK_CONTAINER (window), aspect_frame);
    gtk_widget_show (aspect_frame);
   
    /* 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, 200, 200);
    gtk_container_add (GTK_CONTAINER (aspect_frame), 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 ();
    return 0;
}


then one may replace the gdk_draw_arc() with some function like...


Last edited by virgoptrex@yahoo.com on Thu Jun 24, 2010 4:08 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2010 4:07 am 
Offline
Familiar Face

Joined: Wed Feb 03, 2010 9:23 pm
Posts: 22
Here is the improved code.

Code:
#include <gtk/gtk.h>
   
gint t=0;
gint time1;



    GtkWidget *window;
    GtkWidget *aspect_frame;
    GtkWidget *drawing_area;
    GtkWidget *vbox1, *toggle1;
 
     gboolean
     expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
     {

     

     //g_object_ref_sink (widget->window);
     
     // gdk_drawable_ref (widget->window);
       
       gdk_draw_arc (widget->window,
                     widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
                     TRUE,
                     0, 0, widget->allocation.width, widget->allocation.height,
                     0, 64 * 18*t);
   
      //gdk_drawable_unref (widget->window);
       return TRUE;
     }



static gboolean
time_handler(GtkWidget *widget)
{   

  gtk_widget_queue_draw(GTK_WIDGET(widget));

  if (t<20)
  { t++; }
  else if (t >=20)
  { t=0; }

printf("hello %d\n",t);
   return TRUE;


}

void toggle_button_callback (GtkWidget *widget, gpointer data)
{
    if (GTK_TOGGLE_BUTTON (widget)->active)
    {
         g_print("The button %d pressed\n", GPOINTER_TO_INT( g_object_get_data( G_OBJECT( widget),
                                                    "id" ) ) ); 
         
        /* If control reaches here, the toggle button is down */

          g_source_remove(time1);
   
    } else {
   
          g_print("The button %d released\n", GPOINTER_TO_INT( g_object_get_data( G_OBJECT( widget),
                                                    "id" ) ) );
         
          time1 = g_timeout_add(100, (GSourceFunc) time_handler, (gpointer)drawing_area);
         
        /* If control reaches here, the toggle button is up */
           
    }
}


int main( int argc,
          char *argv[] )
{
    gtk_init (&argc, &argv);
   
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   


    gtk_window_set_title (GTK_WINDOW (window), "Aspect Frame");
    g_signal_connect (G_OBJECT (window), "destroy",
                 G_CALLBACK (gtk_main_quit), NULL);
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
   
     vbox1 = gtk_vbox_new(TRUE,0);


    /* Create an aspect_frame and add it to our toplevel window */
   
    aspect_frame = gtk_aspect_frame_new ("2x1", /* label */
                                         0.5, /* center x */
                                         0.5, /* center y */
                                         2, /* xsize/ysize = 2 */
                                         FALSE /* ignore child's aspect */);
   
   
    gtk_container_add( GTK_CONTAINER( window ), vbox1 );

       toggle1 = gtk_toggle_button_new_with_label ("Hold");
      g_signal_connect( toggle1, "toggled", G_CALLBACK( toggle_button_callback ), NULL );
      gtk_box_pack_start(GTK_BOX(vbox1), toggle1, TRUE, TRUE,0);
   
      gtk_box_pack_start(GTK_BOX(vbox1), aspect_frame, TRUE, TRUE,0);   
      gtk_widget_show (aspect_frame);
      gtk_widget_show (vbox1);
   
    /* 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, 200, 200);
    gtk_container_add (GTK_CONTAINER (aspect_frame), drawing_area);
    gtk_widget_show (drawing_area);

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

    time1 = g_timeout_add(100, (GSourceFunc) time_handler, (gpointer)drawing_area);


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


Top
 Profile  
 
 Post subject: Re: Solved]Problem OpenCV and continuous image frames(video)
PostPosted: Fri Mar 23, 2012 5:51 am 
Offline

Joined: Fri Mar 23, 2012 5:33 am
Posts: 3
Hi,

Sir, I am doing a project which captures an frame wrom the webcam and then analyses it for its rgb values. I wanted to develop a gui for the application. I am using OpencCV for capturing the image and was thinking of developing the gui using Gtk+. I am currently having problems in displaying the image captured in gtk. Can you please show me a way for displaying the openCV image in Gtk. I am fairly new to this so i am not able to understand gdk_new_pix_buff(). Can you please help me with it.
Thanks and Regards,
Prateek Srivastava


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

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 3 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