LivingMachine/Code/KirbyTrack.c

252 lines
5.8 KiB
C
Raw Normal View History

2017-05-23 17:15:23 +02:00
/**
* \file KirbyTrack.c
* \author Jacques / Antoine
* \date avril - mai 2017
* \brief Figure Imposé : Suivi d'un objet coloré.
*
* \details Suivie d'un Kirby (Rose) ou d'une étoile (Jaune) par une caméra avec mode interface utilisateur ou configuration
2017-05-23 18:00:19 +02:00
* \bug SFML ne supporte qu'un nombre limité de sprite en fonction du PC
* \bug Affichage OpenCV incompatible avec SFML
* \todo Optimisation du chargement de la frame en image SFML
2017-05-23 17:15:23 +02:00
*/
#include "fonction.h"
2017-05-23 17:15:23 +02:00
2017-05-23 17:15:23 +02:00
/**
* \fn int main(int argc, char* argv[])
* \brief Entrée du programme
* \author Antoine / Jacques
* \return EXIT_SUCCESS : Arrêt normal du programme, EXIT_FAILURE : Le programme a rencontrée une erreur au cours de son execution
*/
2017-05-01 20:45:20 +02:00
int main(int argc, char* argv[])
{
2017-05-09 16:29:35 +02:00
//Initialisations
2017-05-16 17:36:09 +02:00
int height,width; //parameters of the image we are working on
2017-05-01 20:45:20 +02:00
int posX, posY; //Position objet
int boucle;
2017-05-09 18:09:07 +02:00
double angle[2] = {100,100};
int tracking; //0 = tracking OFF
2017-05-09 16:29:35 +02:00
#ifdef SFML
//Initialisation SFML
2017-05-16 17:36:09 +02:00
sf::Texture txFlux;
sf::Sprite spFlux;
sf::Image imFlux;
2017-05-09 18:01:10 +02:00
sf::Event event;
tracking = 0; //Pas de tracking de base en mode SFML
#endif
2017-05-09 16:29:35 +02:00
//Ouverture flux camera
CvCapture* capture = cvCaptureFromCAM( 0 );
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
if( !capture ){
printf("ERROR: capture is NULL \n" );
exit(EXIT_FAILURE);
}
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
// grab an image from the capture
IplImage* frame = cvQueryFrame( capture );
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
// get the image data
height = frame->height;
width = frame->width;
2017-05-01 20:45:20 +02:00
// capture size -
CvSize size = cvSize(width,height);
#ifdef SFML
//Création de la fenetre principale
2017-05-09 18:01:10 +02:00
sf::RenderWindow window(sf::VideoMode(width+300, height), "KirbyTrack");
#endif
2017-05-01 20:45:20 +02:00
// Initialize different images that are going to be used in the program
IplImage* hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); // image converted to HSV plane
IplImage* threshold = cvCreateImage(size, IPL_DEPTH_8U, 1);
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
//Controle couleur
#ifdef KIRBY
2017-05-01 20:45:20 +02:00
//Setup Kirby
2017-05-09 18:09:07 +02:00
int iLowH = 152;
2017-05-09 16:29:35 +02:00
int iHighH = 179;
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
int iLowS = 48;
int iHighS = 255;
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
int iLowV = 101;
int iHighV = 255;
#endif
#ifdef ETOILE
//Setup Etoile
int iLowH = 20;
int iHighH = 30;
2017-05-01 20:45:20 +02:00
int iLowS = 100;
int iHighS = 255;
2017-05-01 20:45:20 +02:00
int iLowV = 100;
int iHighV = 255;
#endif
2017-05-01 20:45:20 +02:00
#ifdef CONFIG
//Affichage du panneau de config
config(&iLowH, &iHighH, &iLowS, &iHighS, &iLowV, &iHighV);
2017-05-01 20:45:20 +02:00
boucle = 1;
tracking = 1; //Tracking de base en mode CONFIG
#endif
while(boucle)
{
#ifdef SFML
2017-05-09 16:29:35 +02:00
boucle = window.isOpen();
// on inspecte tous les évènements de la fenêtre qui ont été émis depuis la précédente itération
while (window.pollEvent(event))
{
// évènement "fermeture demandée" : on ferme la fenêtre
if (event.type == sf::Event::Closed)
window.close();
}
#endif
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
// Get one frame
frame = cvQueryFrame( capture );
if( !frame ){
perror("ERROR: frame is null...");
break;
}
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
//Binarisation du flux vidéo
traitement(frame, hsv_frame, threshold, iLowH, iHighH, iLowS, iHighS, iLowV, iHighV);
// Calculate the moments to estimate the position of the ball
Position_moy(threshold, &posX, &posY);
//Dessine les informations de tracking sur frame
Affichage_Tracking(frame, posX, posY, width, height);
#ifdef SFML
//Affichage SFML
/* Clear the screen */
2017-05-09 18:01:10 +02:00
window.clear(sf::Color::White);
//Affichage de la frame
//Le chargement pourrait etre plus optimisé en créant nous me l'image SFML en parcourant l'IplImage
//Enregistrement de la frame openCV
2017-05-09 18:01:10 +02:00
cvSaveImage("Stock SFML/temp.jpg", frame);
//Chargement de la frame en texture SFML
2017-05-09 18:01:10 +02:00
if (!txFlux.loadFromFile("Stock SFML/temp.jpg")){
printf("Erreur chargement image SFML\n" );
break;
}
spFlux.setTexture(txFlux);
2017-05-09 18:01:10 +02:00
window.draw(spFlux);
sf::Vector2i PosMouse = sf::Mouse::getPosition(window);
//Detection du bouton tracking
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)&&(PosMouse.x>640)&&(PosMouse.x<760)&&(PosMouse.y>0)&&(PosMouse.y<120)){
//printf("\n\n\n OK \n\n\n");
if (tracking){ tracking = 0;}
else tracking = 1;
2017-05-16 17:36:09 +02:00
cvWaitKey(100);
2017-05-09 18:01:10 +02:00
}
//printf("Pos Mouse : %d %d \n", PosMouse.x, PosMouse.y);
//Dessin du bouton de tracking
2017-05-16 17:36:09 +02:00
sf::Texture txBut;
sf::Sprite button_tracking;
if (!txBut.loadFromFile("Stock SFML/button.png")){
printf("Erreur chargement image SFML\n" );
break;
}
2017-05-16 17:36:09 +02:00
button_tracking.setTexture(txBut);
button_tracking.setScale(0.5,0.5);
button_tracking.setPosition(sf::Vector2f(width+20, 20));
2017-05-09 18:01:10 +02:00
2017-05-16 17:36:09 +02:00
if(tracking){ button_tracking.setColor(sf::Color::Green); }
else{ button_tracking.setColor(sf::Color::Red); }
2017-05-09 18:01:10 +02:00
2017-05-16 17:36:09 +02:00
window.draw(button_tracking);
2017-05-09 18:01:10 +02:00
//Ajout du texte
sf::Font font;
if (!font.loadFromFile("Stock SFML/arial.ttf")){
printf("Erreur chargement police SFML\n" );
break;
2017-05-09 18:01:10 +02:00
}
sf::Text text;
// choix de la police à utiliser
text.setFont(font); // font est un sf::Font
// choix de la chaîne de caractères à afficher
2017-05-16 17:36:09 +02:00
text.setString("Tracking Moteur");
// choix de la taille des caractères
text.setCharacterSize(24); // exprimée en pixels, pas en points !
//text.setFillColor(sf::Color::Black);
text.setColor(sf::Color::Black);
2017-05-16 17:36:09 +02:00
text.setPosition(sf::Vector2f(width+100, 35));
window.draw(text);
2017-05-01 20:45:20 +02:00
/* Update the window */
window.display();
2017-05-01 20:45:20 +02:00
#endif
//Envoie données moteurs
if(tracking){
2017-05-09 16:29:35 +02:00
//Mouvements moteurs
2017-05-16 17:36:09 +02:00
//printf("-PREMAJ_ANGLE...: %d %d\n",width,height);
2017-05-09 16:56:07 +02:00
2017-05-16 17:36:09 +02:00
maj_angle(ajust_pos(posX-width/2,width), ajust_pos(posY-height/2,height), height*JEU, angle);
2017-05-09 16:29:35 +02:00
controle_moteur(angle);
2017-05-09 18:12:04 +02:00
2017-05-16 17:36:09 +02:00
cvWaitKey(50);
}
2017-05-16 17:36:09 +02:00
#ifdef CONFIG
2017-05-09 16:29:35 +02:00
affichage_config(frame, hsv_frame, threshold); //Affichage du flux vidéo et de ses traitements
2017-05-09 16:29:35 +02:00
if( (cvWaitKey(10) ) >= 0 ) break; //Arret capture
#endif
2017-05-09 16:29:35 +02:00
}
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
// Release the capture device housekeeping
cvReleaseCapture( &capture );
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
cvReleaseImage(&threshold);
cvReleaseImage(&hsv_frame);
cvReleaseImage(&frame);
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00
return EXIT_SUCCESS;
}
2017-05-01 20:45:20 +02:00
2017-05-09 16:29:35 +02:00