Conversion Iplimage (OpenCV) vers GTKimage (GTK+)
Bonjour a tout le monde,
Je désire dans le cadre d'un stage faire de l'acquisition d'images a partir d'une camera avec OpenCV puis afficher cette image dans GTK. L’idée première est de faire tourner la camera et afficher le flux avec une fenêtre OpenCV et d’un clic de souris acquérir l’image.
Le problème que j'ai se fait au moment d'afficher une image dans la fenêtre GTK et bien la conversion Ipl image vers GTKimage bugge et l’affichage n’est pas très beau à voir.
Je m'excuse par avance du code quick and dirty avec les variables globales mais pour me rattraper j’ai commenté le code et remanié pour qu’il soit lisible a lire enfin je l’espère.
Voila en espérant que quelqu’un a déjà eu un problème similaire en utilisant conjointement OpenCV et GTK+ et pourrait m’aider.
Cordialement et merci par avance,
Damien

Le code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
 
// projetessai.cpp : Defines the entry point for the console application.
 
#include "stdafx.h"
#include <stdio.h>
//Libraries OpenCV
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
#include <cvcam.h>
#include <Windows.h>
// Libraries GTK+
#include <gtk/gtk.h>
#include <stdlib.h>
 
// Global variables used here for simplicity
 
  //Windows created
  GtkWidget *pWin;
  GtkWidget *pWin2;
 
// Callbacks widgets
  GtkWidget *pQuitBtn;
  GtkWidget *pGrabBtn;
 
// Table created for the main window
  GtkWidget *pTable;
 
  GtkWidget *pImage;
  GdkPixbuf *pGdkpix; 
 
// OpenCV 
  IplImage  *pIplimg;
  IplImage  *pIplimgnew;
 
  void transfoIplImageToGtkImage(void)
  {
// Creation and placement of the grabbed frame
   pWin2 = gtk_window_new(GTK_WINDOW_POPUP);
   gtk_window_set_title(GTK_WINDOW(pWin2), "picture grabbed");
   gtk_window_set_default_size(GTK_WINDOW(pWin2), 600, 400);
   gtk_window_move(GTK_WINDOW(pWin2), 400, 400);
 
// Information about the IPL Image
    int iHeightipl=      pIplimg->height; 	printf("iHeightipl  %d  ",iHeightipl);printf("\n\n");
    int iWidthipl=       pIplimg->width;  printf("iWidthipl  %d  ",iWidthipl);printf("\n\n");
    int iWidthStepipl=   pIplimg->widthStep; printf("iWidthStepipl  %d  ",iWidthStepipl);printf("\n\n");
 
	//je crois que le probleme vient d'ici, c'est bien la conversion qui cloche car si je sauve l'image et je la remet 
	// ce sont les fcts de sauvegarde bitmap en commentaire le prog fonctionne
 
   	pIplimgnew=pIplimg; //constructeur par recopie pour pas se fouler
 
	 //Create a pixbuf from the imageData of the OpenCv image in BGR order to an RGB one 
	cvCvtColor( pIplimg,pIplimgnew,  CV_BGR2RGB );
	pGdkpix=gdk_pixbuf_new_from_data((guchar*)pIplimg,GDK_COLORSPACE_RGB,FALSE, //We don't have alpha channel
	                               8,iWidthipl,iHeightipl,iWidthStepipl,NULL,NULL);
 
    pImage=gtk_image_new_from_pixbuf(pGdkpix);
	pImage=gtk_image_new_from_file("c:\\toto.bmp");
    gtk_container_add(GTK_CONTAINER(pWin2),pImage);
 
    gtk_widget_show_all(pWin2);
 
 
  }
 
 
//Callbacks functions
 
void cb_grab (GtkWidget *p_widget, gpointer user_data)
{
  transfoIplImageToGtkImage();
 
}
 
void cb_quit (GtkWidget *p_widget, gpointer user_data)
{
 gtk_main_quit();
}
 
 
int main (int argc, char *argv[])
{
 
  gtk_init(&argc, &argv);
 
 //Creation of the GUI enviromment
  pWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size(GTK_WINDOW(pWin), 200, 200);
  gtk_window_set_title(GTK_WINDOW(pWin), "GTK-Win");
  gtk_window_set_default_size(GTK_WINDOW(pWin), 200, 200);
  g_signal_connect(G_OBJECT(pWin), "destroy", G_CALLBACK(cb_quit), NULL);
 
  // Creation of the table here just for two buttons with two collumns
  pTable = gtk_table_new(2, 1, FALSE);
  gtk_container_add (GTK_CONTAINER(pWin),pTable);
 
  //Callbacks button and attached signal
  pQuitBtn = gtk_button_new_with_label("Quit");
  g_signal_connect(G_OBJECT(pQuitBtn),"clicked",G_CALLBACK(cb_quit), NULL);
  gtk_widget_set_size_request(pQuitBtn, 50, 40);
  gtk_table_attach(GTK_TABLE(pTable), pQuitBtn, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 50, 50);
 
  pGrabBtn = gtk_button_new_with_label("Grab");
  g_signal_connect(G_OBJECT(pGrabBtn),"clicked",G_CALLBACK(cb_grab), NULL);
  gtk_widget_set_size_request(pGrabBtn, 50, 40);
  gtk_table_attach(GTK_TABLE(pTable), pGrabBtn, 1, 2, 0, 1, GTK_FILL, GTK_FILL,50, 50);
 
  //OpenCV part of camera acquisition here just the first frame
   CvCapture* capture=cvCaptureFromCAM(0);
   cvNamedWindow("Capture Webcam", 0);
 
   pIplimg=cvQueryFrame(capture); 
   //Displaying and recording the first image
   cvShowImage("Capture Webcam",pIplimg);	
   cvSaveImage( "c:\\toto.bmp", pIplimg);
 
  gtk_widget_show_all(pWin);
  gtk_main ();
 
 
  return 0;
}