Getting webcam feed with opencv to GTK+2 container.

asked 2016-02-19 00:56:58 -0600

inckka gravatar image

I'm developing an C, GTK 2 based app. I'm about to get live webcam video display inside a gtk widget called "drawing area" However I'm new with opencv and gtk. I successfully manged to get only a single frame from webcam using opencv. Look below the working code which only grab first single frame. However not sure how to get live video frame. Could someone help?

/*
 *  compile with:
 *  gcc -o weby3 att3.c `pkg-config --libs --cflags gtk+-2.0 opencv`
 */

#include "highgui.h"
#include <gtk/gtk.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>

GdkPixbuf* pix;
IplImage* frame;
CvCapture* capture;


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

  printf("Exposed\n");


  //while(1){
    frame = cvQueryFrame( capture );

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


    gdk_draw_pixbuf(widget->window,
     widget->style->fg_gc[GTK_WIDGET_STATE (widget)], pix, 0, 0, 0, 0,
     -1, -1, 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 is the storage type for widgets */
   GtkWidget *window;
   GtkWidget *drawing_area;

   gtk_init (&argc, &argv);

   capture = cvCreateCameraCapture(0);

   /* create a new window */
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title (GTK_WINDOW (window), "Hello WebCam and OpenCV!");
   g_signal_connect (G_OBJECT (window), "destroy",
    G_CALLBACK (gtk_main_quit), NULL);

   /* Sets the border width of the window. */
   gtk_container_set_border_width (GTK_CONTAINER (window), 10);

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


   /* window since we are forcing a aspect ratio */
   gtk_widget_set_size_request(drawing_area, 600, 400);
   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);

   /* and the window */
   gtk_widget_show (window);

   /* All GTK applications must have a gtk_main(). Control ends here
    * and waits for an event to occur (like a key press or
    * mouse event). */
   gtk_main ();
}
edit retag flag offensive close merge delete

Comments

"I'm developing an C, GTK 2 based app" -- if you're really interested in opencv, using the discontinued c-api is already a bad idea. do you really need a computer-vision api ? it's probably better to interface with libv4l or similar directly.

berak gravatar imageberak ( 2016-02-19 01:46:56 -0600 )edit

@berak This was a ongoing project which hold for long time so it is gtk2 I'd need opencv to grab some frames and do edge detection. Is there any tutorial about using libv4l with gtk?

inckka gravatar imageinckka ( 2016-02-19 01:56:55 -0600 )edit

please try to use the c++ version of both gtk and opencv, like here

berak gravatar imageberak ( 2016-02-19 02:01:16 -0600 )edit

@berak Thanks but I'm looking for something to correct above code.

inckka gravatar imageinckka ( 2016-02-19 02:05:44 -0600 )edit

again, your widget only draws once, callled on the "expose-event". you probably need some timer / thread, whatever, which draws continuously.

berak gravatar imageberak ( 2016-02-19 02:07:24 -0600 )edit

@berak OK, Can you let me know using threads or timer how to get above code work? sorry I'm new to GTK and C programming. code pasted: http://pastebin.com/VYbwTvgP

inckka gravatar imageinckka ( 2016-02-19 02:12:53 -0600 )edit

sorry, not using gtk at all, can't help.

and this gets more and more off-topic, no ?

again, please * do not use opencv's c-api* they moved away from using code like yours half a decade ago.

berak gravatar imageberak ( 2016-02-19 02:16:45 -0600 )edit

Ok thanks a lot for the info and guidence. Above code is just for testing purpose. Main project is a bit bigger code base. So its not feasible to migrate to C++, GTK3. That would be for future phase.

inckka gravatar imageinckka ( 2016-02-19 02:20:32 -0600 )edit