10 #include <SFML/Audio.hpp>
11 #include <SFML/Graphics.hpp>
12 #include <SFML/Window.hpp>
13 #include <SFML/System.hpp>
27 void maj_angle(
int vecX,
int vecY,
int rayon,
double* angle);
32 void config(
int* LowH,
int* HighH,
int* LowS,
int* HighS,
int* LowV,
int* HighV);
36 void Position_moy(IplImage* Binaire,
int* posX,
int * posY);
37 void traitement(IplImage* frame, IplImage* HSV, IplImage* Binaire,
int LowH,
int HighH,
int LowS,
int HighS,
int LowV,
int HighV);
39 int image_CV2SFML(IplImage* imcv, sf::Image imFlux);
42 int main(
int argc,
char* argv[])
49 double angle[2] = {100,100};
67 CvCapture* capture = cvCaptureFromCAM( 0 );
70 printf(
"ERROR: capture is NULL \n" );
76 IplImage* frame = cvQueryFrame( capture );
79 height = frame->height;
83 CvSize size = cvSize(width,height);
88 sf::RenderWindow window(sf::VideoMode(width+300, height),
"KirbyTrack");
92 IplImage* hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3);
93 IplImage* threshold = cvCreateImage(size, IPL_DEPTH_8U, 1);
122 config(&iLowH, &iHighH, &iLowS, &iHighS, &iLowV, &iHighV);
132 boucle = window.isOpen();
135 while (window.pollEvent(event))
138 if (event.type == sf::Event::Closed)
144 frame = cvQueryFrame( capture );
147 perror(
"ERROR: frame is null...");
152 traitement(frame, hsv_frame, threshold, iLowH, iHighH, iLowS, iHighS, iLowV, iHighV);
164 window.clear(sf::Color::White);
174 cvSaveImage(
"Stock SFML/temp.jpg", frame);
177 if (!txFlux.loadFromFile(
"Stock SFML/temp.jpg")){
178 printf(
"Erreur chargement image SFML\n" );
182 spFlux.setTexture(txFlux);
187 sf::Vector2i PosMouse = sf::Mouse::getPosition(window);
189 if (sf::Mouse::isButtonPressed(sf::Mouse::Left)&&(PosMouse.x>640)&&(PosMouse.x<760)&&(PosMouse.y>0)&&(PosMouse.y<120)){
191 if (tracking){ tracking = 0;}
199 sf::Sprite button_tracking;
201 if (!txBut.loadFromFile(
"Stock SFML/button.png")){
202 printf(
"Erreur chargement image SFML\n" );
206 button_tracking.setTexture(txBut);
207 button_tracking.setScale(0.5,0.5);
208 button_tracking.setPosition(sf::Vector2f(width+20, 20));
210 if(tracking){ button_tracking.setColor(sf::Color::Green); }
211 else{ button_tracking.setColor(sf::Color::Red); }
213 window.draw(button_tracking);
217 sf::Sprite button_reset;
219 if (!txBut2.loadFromFile(
"Stock SFML/button.png")){
220 printf(
"Erreur chargement image SFML\n" );
224 button_reset.setTexture(txBut2);
225 button_reset.setScale(0.5,0.5);
226 button_reset.setPosition(sf::Vector2f(width+20, 100));
228 window.draw(button_reset);
289 if( (cvWaitKey(10) ) >= 0 )
break;
297 cvReleaseCapture( &capture );
299 cvReleaseImage(&threshold);
300 cvReleaseImage(&hsv_frame);
301 cvReleaseImage(&frame);
307 void maj_angle(
int vecX,
int vecY,
int rayon,
double* angle){
310 double coeffx, coeffy;
317 coeffx = -0.2*vecX/rayon;
318 coeffy = 0.2*vecY/rayon;
325 if (l0 != 0) angle[0] = l0;
326 if (l1 != 0) angle[1] = l1;
332 if (pos > ref)
return 0;
337 int MAX_PWM = 130, MIN_PWM = 30;
338 if (val_pwm > MAX_PWM){
341 else if (val_pwm < MIN_PWM){
352 FILE* fichier = NULL;
353 fichier = fopen(
"/dev/ttyACM0",
"w");
355 printf(
"Erreur ouverture fichier\n");
356 perror(
"fopen failed for /dev/ttyACM0" );
357 exit( EXIT_FAILURE );
361 fprintf(fichier,
"%d\n",(
int)angle[0]);
362 fprintf(fichier,
"%d\n",(
int)angle[1]);
369 int image_CV2SFML(IplImage* imcv, sf::Image imFlux){
372 int w = imcv->widthStep;
373 char* ptr = imcv->imageData;
375 imFlux.create(imcv->width,imcv->height, NULL);
377 for(
int y=0; y<imcv->height; y++ ) {
379 for(
int x=0; x<imcv->width; x++ ) {
382 G = ptr[y*w + 3*x + 1];
383 R = ptr[y*w + 3*x + 2];
386 imFlux.setPixel(x,y,sf::Color(R,G,B,1));
394 void traitement(IplImage* frame, IplImage* HSV, IplImage* Binaire,
int LowH,
int HighH,
int LowS,
int HighS,
int LowV,
int HighV){
397 cvCvtColor(frame, HSV, CV_BGR2HSV);
400 cvSmooth( HSV, HSV, CV_GAUSSIAN, 15, 0,0,0);
404 CvScalar valinf={(double)LowH,(
double)LowS,(double)LowV};
405 CvScalar valsup={(double)HighH,(
double)HighS,(double)HighV};
407 cvInRangeS(HSV, valinf,valsup, Binaire);
415 void Position_moy(IplImage* Binaire,
int* posX,
int * posY){
417 CvMoments *moments = (CvMoments*)malloc(
sizeof(CvMoments));
419 cvMoments(Binaire, moments, 1);
421 double moment10 = cvGetSpatialMoment(moments, 1, 0);
422 double moment01 = cvGetSpatialMoment(moments, 0, 1);
423 double area = cvGetCentralMoment(moments, 0, 0);
425 *posX = moment10/area;
426 *posY = moment01/area;
431 void config(
int* LowH,
int* HighH,
int* LowS,
int* HighS,
int* LowV,
int* HighV){
433 cvNamedWindow(
"Control", CV_WINDOW_AUTOSIZE);
436 cvCreateTrackbar(
"LowH",
"Control", LowH, 179,NULL);
437 cvCreateTrackbar(
"HighH",
"Control", HighH, 179,NULL);
439 cvCreateTrackbar(
"LowS",
"Control", LowS, 255,NULL);
440 cvCreateTrackbar(
"HighS",
"Control", HighS, 255,NULL);
442 cvCreateTrackbar(
"LowV",
"Control", LowV, 255,NULL);
443 cvCreateTrackbar(
"HighV",
"Control", HighV, 255,NULL);
449 cvNamedWindow(
"HSV", CV_WINDOW_AUTOSIZE );
450 cvNamedWindow(
"Binaire", CV_WINDOW_AUTOSIZE );
451 cvNamedWindow(
"Camera", CV_WINDOW_AUTOSIZE );
453 cvShowImage(
"HSV", HSV);
454 cvShowImage(
"Binaire", Binaire);
455 cvShowImage(
"Camera", frame );
461 cvCircle(frame, cvPoint(width/2,height/2), height*
JEU, CV_RGB(0, 255, 0), 4, 8, 0 );
466 cvLine(frame, cvPoint(posX-20,posY), cvPoint(posX+20,posY), CV_RGB(255, 0, 0), 4, 8, 0 );
467 cvLine(frame, cvPoint(posX,posY-20), cvPoint(posX,posY+20), CV_RGB(255, 0, 0), 4, 8, 0 );
471 cvLine(frame, cvPoint(posX-20,posY), cvPoint(posX+20,posY), CV_RGB(0, 0, 255), 4, 8, 0 );
472 cvLine(frame, cvPoint(posX,posY-20), cvPoint(posX,posY+20), CV_RGB(0, 0, 255), 4, 8, 0 );
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.
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.
#define JEU
Coefficient de tolérance pour le suivi d'objet.
int ajust_pos(int pos, int ref)
permet d'éviter des positions supérieures à ref considérées comme aberrantes.
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...
int limite_moteur(int val_pwm)
Fonction qui vérifie que les valeurs envoyees aux moteurs sont correctes.
void Affichage_Tracking(IplImage *frame, int posX, int posY, int width, int height)
Fonction d'affichage des informations de suivi.
int main(int argc, char *argv[])
Entrée du programme.
void controle_moteur(double *angle)
Fonction d'envoie des angles aux moteurs.
void affichage_config(IplImage *frame, IplImage *HSV, IplImage *Binaire)
Fonction d'affichage du flux vidéo, du flux en HSV et de sa binarisation.
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.