17 #include <SFML/Audio.hpp>
18 #include <SFML/Graphics.hpp>
19 #include <SFML/Window.hpp>
57 void maj_angle(
int vecX,
int vecY,
int rayon,
double* angle);
103 void config(
int* LowH,
int* HighH,
int* LowS,
int* HighS,
int* LowV,
int* HighV);
137 void Position_moy(IplImage* Binaire,
int* posX,
int * posY);
154 void traitement(IplImage* frame, IplImage* HSV, IplImage* Binaire,
int LowH,
int HighH,
int LowS,
int HighS,
int LowV,
int HighV);
162 int main(
int argc,
char* argv[])
169 double angle[2] = {100,100};
188 CvCapture* capture = cvCaptureFromCAM( 0 );
191 printf(
"ERROR: capture is NULL \n" );
197 IplImage* frame = cvQueryFrame( capture );
200 height = frame->height;
201 width = frame->width;
204 CvSize size = cvSize(width,height);
209 sf::RenderWindow window(sf::VideoMode(width+300, height),
"KirbyTrack");
213 IplImage* hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3);
214 IplImage* threshold = cvCreateImage(size, IPL_DEPTH_8U, 1);
243 config(&iLowH, &iHighH, &iLowS, &iHighS, &iLowV, &iHighV);
253 boucle = window.isOpen();
256 while (window.pollEvent(event))
259 if (event.type == sf::Event::Closed)
265 frame = cvQueryFrame( capture );
268 perror(
"ERROR: frame is null...");
273 traitement(frame, hsv_frame, threshold, iLowH, iHighH, iLowS, iHighS, iLowV, iHighV);
285 window.clear(sf::Color::White);
292 cvSaveImage(
"Stock SFML/temp.jpg", frame);
295 if (!txFlux.loadFromFile(
"Stock SFML/temp.jpg")){
296 printf(
"Erreur chargement image SFML\n" );
300 spFlux.setTexture(txFlux);
304 sf::Vector2i PosMouse = sf::Mouse::getPosition(window);
306 if (sf::Mouse::isButtonPressed(sf::Mouse::Left)&&(PosMouse.x>640)&&(PosMouse.x<760)&&(PosMouse.y>0)&&(PosMouse.y<120)){
308 if (tracking){ tracking = 0;}
316 sf::Sprite button_tracking;
318 if (!txBut.loadFromFile(
"Stock SFML/button.png")){
319 printf(
"Erreur chargement image SFML\n" );
323 button_tracking.setTexture(txBut);
324 button_tracking.setScale(0.5,0.5);
325 button_tracking.setPosition(sf::Vector2f(width+20, 20));
327 if(tracking){ button_tracking.setColor(sf::Color::Green); }
328 else{ button_tracking.setColor(sf::Color::Red); }
330 window.draw(button_tracking);
334 if (!font.loadFromFile(
"Stock SFML/arial.ttf")){
335 printf(
"Erreur chargement police SFML\n" );
344 text.setString(
"Tracking Moteur");
347 text.setCharacterSize(24);
350 text.setColor(sf::Color::Black);
352 text.setPosition(sf::Vector2f(width+100, 35));
375 if( (cvWaitKey(10) ) >= 0 )
break;
381 cvReleaseCapture( &capture );
383 cvReleaseImage(&threshold);
384 cvReleaseImage(&hsv_frame);
385 cvReleaseImage(&frame);
391 void maj_angle(
int vecX,
int vecY,
int rayon,
double* angle){
394 double coeffx, coeffy;
401 coeffx = -0.2*vecX/rayon;
402 coeffy = 0.2*vecY/rayon;
409 if (l0 != 0) angle[0] = l0;
410 if (l1 != 0) angle[1] = l1;
416 if (pos > ref)
return 0;
421 int MAX_PWM = 130, MIN_PWM = 30;
422 if (val_pwm > MAX_PWM){
425 else if (val_pwm < MIN_PWM){
436 FILE* fichier = NULL;
437 fichier = fopen(
"/dev/ttyACM0",
"w");
439 printf(
"Erreur ouverture fichier\n");
440 perror(
"fopen failed for /dev/ttyACM0" );
441 exit( EXIT_FAILURE );
445 fprintf(fichier,
"%d\n",(
int)angle[0]);
446 fprintf(fichier,
"%d\n",(
int)angle[1]);
453 void traitement(IplImage* frame, IplImage* HSV, IplImage* Binaire,
int LowH,
int HighH,
int LowS,
int HighS,
int LowV,
int HighV){
456 cvCvtColor(frame, HSV, CV_BGR2HSV);
459 cvSmooth( HSV, HSV, CV_GAUSSIAN, 15, 0,0,0);
463 CvScalar valinf={(double)LowH,(
double)LowS,(double)LowV};
464 CvScalar valsup={(double)HighH,(
double)HighS,(double)HighV};
466 cvInRangeS(HSV, valinf,valsup, Binaire);
476 CvMoments *moments = (CvMoments*)malloc(
sizeof(CvMoments));
478 cvMoments(Binaire, moments, 1);
480 double moment10 = cvGetSpatialMoment(moments, 1, 0);
481 double moment01 = cvGetSpatialMoment(moments, 0, 1);
482 double area = cvGetCentralMoment(moments, 0, 0);
484 *posX = moment10/area;
485 *posY = moment01/area;
490 void config(
int* LowH,
int* HighH,
int* LowS,
int* HighS,
int* LowV,
int* HighV){
492 cvNamedWindow(
"Control", CV_WINDOW_AUTOSIZE);
495 cvCreateTrackbar(
"LowH",
"Control", LowH, 179,NULL);
496 cvCreateTrackbar(
"HighH",
"Control", HighH, 179,NULL);
498 cvCreateTrackbar(
"LowS",
"Control", LowS, 255,NULL);
499 cvCreateTrackbar(
"HighS",
"Control", HighS, 255,NULL);
501 cvCreateTrackbar(
"LowV",
"Control", LowV, 255,NULL);
502 cvCreateTrackbar(
"HighV",
"Control", HighV, 255,NULL);
508 cvNamedWindow(
"HSV", CV_WINDOW_AUTOSIZE );
509 cvNamedWindow(
"Binaire", CV_WINDOW_AUTOSIZE );
510 cvNamedWindow(
"Camera", CV_WINDOW_AUTOSIZE );
512 cvShowImage(
"HSV", HSV);
513 cvShowImage(
"Binaire", Binaire);
514 cvShowImage(
"Camera", frame );
520 cvCircle(frame, cvPoint(width/2,height/2), height*
JEU, CV_RGB(0, 255, 0), 4, 8, 0 );
525 cvLine(frame, cvPoint(posX-20,posY), cvPoint(posX+20,posY), CV_RGB(255, 0, 0), 4, 8, 0 );
526 cvLine(frame, cvPoint(posX,posY-20), cvPoint(posX,posY+20), CV_RGB(255, 0, 0), 4, 8, 0 );
530 cvLine(frame, cvPoint(posX-20,posY), cvPoint(posX+20,posY), CV_RGB(0, 0, 255), 4, 8, 0 );
531 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.