title: Using opencv and gtk+2 together
body: <p>so i am currently trying to achieve the following: Create a gui for the user were the user can see a livefeed of the webcam and also have two buttons left of the livefeed to start and stop the recording. all this has to run on a raspberry pi.</p>
  
  <p>I am using a basler camera so i can not use the videocapture object but using the pylon sdk (sdk for basler cameras) i managed to grab an image and convert it into a opencv::mat.</p>
  
  <p>however im stuck on two points:</p>
  
  <p>first i do not know how to display an opencv::mat on a gtk widget at all.</p>
  
  <p>the second problem is that i do not know how to get a live stream. I tried to use the g_timeout_add function but then the ui gets unresponsive. it calls the function in background but since i call it every 100ms it seems to be stuck forever in the function and therefore i can not click on any buttons.</p>
  
  <p>i have included some of my code below.</p>
  
  <pre><code>#include &lt;gtk/gtk.h&gt;
  
  #include &lt;opencv2/core/core.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/video/video.hpp&gt;
  
  #include &lt;pylon/PylonIncludes.h&gt;
  
  using namespace Pylon;
  using namespace cv;
  using namespace std;
  
  void callback(GtkWidget *widget, gpointer labelstatus) {}
  
  gint delete_event(GtkWidget *widget, GdkEvent  *event) {
      gtk_main_quit ();
  }
  
  static gboolean livefeed_func(gpointer data) {
  
      Pylon::PylonAutoInitTerm autoInitTerm;
      CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
      camera.Open();
      GenApi::INodeMap&amp; nodemap = camera.GetNodeMap();
      .
      . Some other pylon related stuff
      .
      CImageFormatConverter formatConverter;
      formatConverter.OutputPixelFormat= PixelType_BGR8packed;
      CPylonImage pylonImage;
  
      Mat openCvImage;
      cv::Size frameSize = Size((int)width-&gt;GetValue(), (int)height-&gt;GetValue());
      .
      . Some other pylon related stuff
      .
      formatConverter.Convert(pylonImage, ptrGrabResult);
      openCvImage = cv::Mat(ptrGrabResult-&gt;GetHeight(), ptrGrabResult-&gt;GetWidth(), CV_8UC3, (uint8_t *) pylonImage.GetBuffer());
      imwrite("tmp_name.jpg", openCvImage);
  }
  
  int main(int argc, char *argv[]) {
  
      //GTK
  
      GtkWidget *window;
      GtkWidget *table;
      GtkWidget *labelstatus;
      GtkWidget *labellog;
      GtkWidget *buttonexit;
      GtkWidget *buttonstart;
      GtkWidget *buttonstop;
      GtkWidget *livefeed;
  
      gtk_init (&amp;argc, &amp;argv);
  
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (window), "Table");
      gtk_container_set_border_width (GTK_CONTAINER (window), 20);
  
      gtk_signal_connect (GTK_OBJECT (window), "delete_event", GTK_SIGNAL_FUNC (delete_event), NULL);
  
      table = gtk_table_new (6, 6, TRUE);
      gtk_container_add (GTK_CONTAINER (window), table);
  
      labelstatus = gtk_label_new ("Status: Idle");
      gtk_table_attach_defaults (GTK_TABLE(table), labelstatus, 0, 2, 0, 1);
      gtk_widget_show (labelstatus);
  
      buttonstart = gtk_button_new_with_label ("Start");
      gtk_signal_connect (GTK_OBJECT (buttonstart), "clicked", GTK_SIGNAL_FUNC (callback), (gpointer) labelstatus);
      gtk_table_attach_defaults (GTK_TABLE(table), buttonstart, 0, 1, 1, 2);
      gtk_widget_show (buttonstart);
  
      buttonstop = gtk_button_new_with_label ("Stop");
      gtk_signal_connect (GTK_OBJECT (buttonstop), "clicked", GTK_SIGNAL_FUNC (callback), NULL);
      gtk_table_attach_defaults (GTK_TABLE(table), buttonstop, 1, 2, 1, 2);
      gtk_widget_show (buttonstop);
  
      labellog = gtk_label_new ("11:31:54: Started Recording\n11:36:54: Stopped Recording");
      gtk_table_attach_defaults (GTK_TABLE(table), labellog, 0, 2, 2, 3);
      gtk_widget_show (labellog);
  
      buttonexit = gtk_button_new_with_label ("Quit");
      gtk_signal_connect (GTK_OBJECT (buttonexit), "clicked",GTK_SIGNAL_FUNC (delete_event), NULL);
      gtk_table_attach_defaults (GTK_TABLE(table), buttonexit, 0, 2, 3, 4);
      gtk_widget_show (buttonexit);
  
      livefeed = gtk_drawing_area_new();
      gtk_table_attach_defaults (GTK_TABLE(table), livefeed, 2, 5, 0, 4);
      gtk_widget_show (livefeed);
  
      gtk_widget_show (table);
      gtk_widget_show (window);
  
      g_timeout_add(100, livefeed_func, livefeed);
  
      gtk_main ();
  
  }
  </code></pre>
  
  <p>so if i run this code it will display the gtk window and then be unresponsive and write images until i cancel the process.</p>
  
  <p>So to sum it up:</p>
  
  <ol>
  <li><p>is there a way to execute background tasks in gtk without the ui becoming unresponsive </p></li>
  <li><p>how can i display an opencv::Mat in a gtk window/widget?</p></li>
  </ol>