The Camera of Zelda
 All Classes Files Functions Variables Macros Pages
KirbyTrack.c
Go to the documentation of this file.
1 
13 #include "fonction.h"
14 #include "fonction.c" //Pour le problème de référence indéfinie à la compilation
15 
16 
23 int main(int argc, char* argv[])
24 {
25  //Initialisations
26  int height,width; //parameters of the image we are working on
27  int posX, posY; //Position objet
28  int boucle = 1; //On effectuera la boucle principale au moins une fois
29 
30  double angle[2] = {100,100};
31 
32  int tracking; //0 = tracking OFF
33 
34 
35 #ifdef SFML
36  //Initialisation SFML
37 
38  sf::Texture txFlux;
39  sf::Sprite spFlux;
40  sf::Image imFlux;
41  sf::Event event;
42 
43  tracking = 0; //Pas de tracking de base en mode SFML
44 
45  //Chargement boutons
46  sf::Texture txBut;
47  sf::Sprite button_tracking;
48  sf::Sprite button_reset;
49 
50  if (!txBut.loadFromFile("Stock SFML/button.png")){
51  printf("Erreur chargement image SFML\n" );
52  return EXIT_FAILURE;
53  }
54 
55  //Chargement texte
56  sf::Font font;
57  if (!font.loadFromFile("Stock SFML/arial.ttf")){
58  printf("Erreur chargement police SFML\n" );
59  return EXIT_FAILURE;
60  }
61 
62  sf::Text text_track;
63  sf::Text text_reset;
64 
65  //Chargement son
66  sf::Music aye;
67  if (!aye.openFromFile("Stock SFML/Aye Sir.ogg"))
68  return EXIT_FAILURE;
69 
70 #endif
71 
72  //Ouverture flux camera
73  CvCapture* capture = cvCaptureFromCAM(1);
74 
75  if( !capture ){
76  printf("ERROR: capture is NULL \n" );
77  exit(EXIT_FAILURE);
78  }
79 
80 
81  // grab an image from the capture
82  IplImage* frame = cvQueryFrame( capture );
83 
84  // get the image data
85  height = frame->height;
86  width = frame->width;
87 
88  // capture size -
89  CvSize size = cvSize(width,height);
90 
91 
92 #ifdef SFML
93  //Création de la fenetre principale
94  sf::RenderWindow window(sf::VideoMode(width+300, height), "KirbyTrack");
95 #endif
96 
97  // Initialize different images that are going to be used in the program
98  IplImage* hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); // image converted to HSV plane
99  IplImage* threshold = cvCreateImage(size, IPL_DEPTH_8U, 1);
100 
101 
102  //Controle couleur
103 #ifdef KIRBY
104  //Setup Kirby
105  int iLowH = 152;
106  int iHighH = 179;
107 
108  int iLowS = 48;
109  int iHighS = 255;
110 
111  int iLowV = 101;
112  int iHighV = 255;
113 #endif
114 #ifdef ETOILE
115  //Setup Etoile
116  int iLowH = 20;
117  int iHighH = 30;
118 
119  int iLowS = 100;
120  int iHighS = 255;
121 
122  int iLowV = 100;
123  int iHighV = 255;
124 #endif
125 
126 #ifdef CONFIG
127  //Affichage du panneau de config
128  config(&iLowH, &iHighH, &iLowS, &iHighS, &iLowV, &iHighV);
129 
130  tracking = 1; //Tracking de base en mode CONFIG
131 #endif
132 
133  while(boucle)
134  {
135 
136 #ifdef SFML
137  boucle = window.isOpen();
138 
139  // on inspecte tous les évènements de la fenêtre qui ont été émis depuis la précédente itération
140  while (window.pollEvent(event))
141  {
142  // évènement "fermeture demandée" : on ferme la fenêtre
143  if (event.type == sf::Event::Closed)
144  window.close();
145  }
146 #endif
147  // Get one frame
148  frame = cvQueryFrame( capture );
149 
150  if( !frame ){
151  perror("ERROR: frame is null...");
152  break;
153  }
154 
155  //Binarisation du flux vidéo
156  traitement(frame, hsv_frame, threshold, iLowH, iHighH, iLowS, iHighS, iLowV, iHighV);
157 
158  // Calculate the moments to estimate the position of the ball
159  Position_moy(threshold, &posX, &posY);
160 
161  //Dessine les informations de tracking sur frame
162  Affichage_Tracking(frame, posX, posY, width, height);
163 
164 #ifdef SFML
165 //Affichage SFML
166  /* Clear the screen */
167  window.clear(sf::Color::White);
168 
169 //Affichage de la frame
170 
171  //Le chargement pourrait etre plus optimisé en créant nous me l'image SFML en parcourant l'IplImage
172 
173  //Enregistrement de la frame openCV
174  cvSaveImage("Stock SFML/temp.jpg", frame);
175 
176  //Chargement de la frame en texture SFML
177  if (!txFlux.loadFromFile("Stock SFML/temp.jpg")){
178  printf("Erreur chargement image SFML\n" );
179  break;
180  }
181 
182  spFlux.setTexture(txFlux);
183  window.draw(spFlux);
184 
185 //Dessin des boutons
186  button_tracking.setTexture(txBut);
187  button_tracking.setScale(0.5,0.5);
188  button_tracking.setPosition(sf::Vector2f(width+20, 20));
189 
190  button_reset.setTexture(txBut);
191  button_reset.setScale(0.5,0.5);
192  button_reset.setPosition(sf::Vector2f(width+20, 100));
193 
194  if(tracking){ button_tracking.setColor(sf::Color::Green); }
195  else{ button_tracking.setColor(sf::Color::Red); }
196 
197 //Ajout du texte
198  // choix de la police à utiliser
199  text_track.setFont(font); // font est un sf::Font
200  text_reset.setFont(font);
201 
202  // choix de la chaîne de caractères à afficher
203  text_track.setString("Tracking Moteurs");
204  text_reset.setString("Reset Moteurs");
205 
206  // choix de la taille des caractères
207  text_track.setCharacterSize(24); // exprimée en pixels, pas en points !
208  text_reset.setCharacterSize(24);
209 
210  //text.setFillColor(sf::Color::Black);
211  text_track.setColor(sf::Color::Black);
212  text_reset.setColor(sf::Color::Black);
213 
214  text_track.setPosition(sf::Vector2f(width+100, 35));
215  text_reset.setPosition(sf::Vector2f(width+100, 115));
216 
217 
218 //Detection du bouton tracking
219 sf::Vector2i PosMouse = sf::Mouse::getPosition(window);
220  if (sf::Mouse::isButtonPressed(sf::Mouse::Left)&&(PosMouse.x>640)&&(PosMouse.x<760)&&(PosMouse.y>0)&&(PosMouse.y<110)){
221  //printf("\n\n\n OK \n\n\n");
222  if (tracking){ tracking = 0;}
223  else tracking = 1;
224 
225  aye.play();
226 
227  cvWaitKey(100);
228  }
229 
230 //Detection du bouton reset
231  if (sf::Mouse::isButtonPressed(sf::Mouse::Left)&&(PosMouse.x>640)&&(PosMouse.x<760)&&(PosMouse.y>110)&&(PosMouse.y<160)){
232 
233  tracking = 0;
234  //Reset Position moteur
235  angle[0]=60; //ANGLES A VERIFIER
236  angle[1]=100;
237  controle_moteur(angle);
238 
239  aye.play();
240 
241  cvWaitKey(100);
242  }
243  //printf("Pos Mouse : %d %d \n", PosMouse.x, PosMouse.y);
244 
245 
246  /* Update the window */
247  window.draw(button_tracking);
248  window.draw(button_reset);
249  window.draw(text_track);
250  window.draw(text_reset);
251 
252  window.display();
253 
254 #endif
255 //Envoie données moteurs
256  if(tracking){
257  //Mouvements moteurs
258  //printf("-PREMAJ_ANGLE...: %d %d\n",width,height);
259 
260  maj_angle(ajust_pos(posX-width/2,width), ajust_pos(posY-height/2,height), height*JEU, angle);
261  controle_moteur(angle);
262 
263  cvWaitKey(50);
264  }
265 
266 
267 #ifdef CONFIG
268  affichage_config(frame, hsv_frame, threshold); //Affichage du flux vidéo et de ses traitements
269 
270  if( (cvWaitKey(10) ) >= 0 ) break; //Arret capture
271 #endif
272 
273  }
274 
275  // Release the capture device housekeeping
276  cvReleaseCapture( &capture );
277 
278  cvReleaseImage(&threshold);
279  cvReleaseImage(&hsv_frame);
280  cvReleaseImage(&frame);
281 
282  return EXIT_SUCCESS;
283 }
284 
285 /*
286  //musique
287  if(aye.getStatus()==sf::Sound::Stopped){
288  aye.setPitch(0.5);
289  aye.setVolume(150);
290  aye.setLoop(true);
291  aye.play();
292  }
293 */
294 
295 /*
296  //Link
297  sf::Texture txLink;
298  sf::Sprite Link;
299 
300  if (!txLink.loadFromFile("Stock SFML/link.png")){
301  printf("Erreur chargement image SFML\n" );
302  break;
303  }
304 
305  Link.setTexture(txLink);
306  Link.setPosition(sf::Vector2f(posX-75, posY-75));
307 
308  window.draw(Link);
309 */
void config(int *LowH, int *HighH, int *LowS, int *HighS, int *LowV, int *HighV)
Fonction d'affichage du panneau de configuration de la couleur à suivre.
Definition: fonction.c:136
void Position_moy(IplImage *Binaire, int *posX, int *posY)
Effectue le baricentre des pixels d'une image binaire pour obtenir la postion de l'objet.
Definition: fonction.c:120
int ajust_pos(int pos, int ref)
permet d'éviter des positions supérieures à ref considérées comme aberrantes.
Definition: fonction.c:36
void traitement(IplImage *frame, IplImage *HSV, IplImage *Binaire, int LowH, int HighH, int LowS, int HighS, int LowV, int HighV)
Effectue une binarisation de frame en fonction des bornes HSV.
Definition: fonction.c:99
void maj_angle(int vecX, int vecY, int rayon, double *angle)
Met à jour angle selon la distance entre le centre de la caméra et la cible, avec un tolérance circul...
Definition: fonction.c:12
#define JEU
Coefficient de tolérance pour le suivi d'objet.
Bibliothèque, Headers et Documentation des fonctions.
void affichage_config(IplImage *frame, IplImage *HSV, IplImage *Binaire)
Fonction d'affichage du flux vidéo, du flux en HSV et de sa binarisation.
Definition: fonction.c:151
Fonctions utilisés dans les programmes.
int main(int argc, char *argv[])
Entrée du programme.
Definition: KirbyTrack.c:23
void controle_moteur(double *angle)
Fonction d'envoie des angles aux moteurs.
Definition: fonction.c:54
void Affichage_Tracking(IplImage *frame, int posX, int posY, int width, int height)
Fonction d'affichage des informations de suivi.
Definition: fonction.c:163