mirror of
https://github.com/AntoineHX/LivingMachine.git
synced 2025-05-04 05:40:46 +02:00
separation main et fonctions
This commit is contained in:
parent
5ede5056a8
commit
28165352bc
8 changed files with 26642 additions and 280 deletions
|
@ -41,7 +41,6 @@ int main(int argc, char* argv[])
|
||||||
double angle[2] = {100,100};
|
double angle[2] = {100,100};
|
||||||
|
|
||||||
int tracking; //0 = tracking OFF
|
int tracking; //0 = tracking OFF
|
||||||
, 1 = ON
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef SFML
|
#ifdef SFML
|
||||||
|
|
|
@ -1,15 +1,24 @@
|
||||||
EXEC= KirbyTrack
|
EXEC= KirbyTrack Navy
|
||||||
|
|
||||||
LSFML = -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
|
LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio `pkg-config opencv --libs`
|
||||||
CFLAGS = -Wall -g3
|
CFLAGS = -Wall -g3 -pg `pkg-config opencv --cflags`
|
||||||
|
|
||||||
all: $(EXEC)
|
all: $(EXEC)
|
||||||
|
|
||||||
KirbyTrack : KirbyTrack.o
|
KirbyTrack : KirbyTrack.o
|
||||||
g++ -o $@ $< `pkg-config opencv --libs` $(LSFML) $(CFLAGS)
|
g++ -o $@ $< $(LDFLAGS) $(CFLAGS)
|
||||||
|
|
||||||
KirbyTrack.o : KirbyTrack.c
|
KirbyTrack.o : KirbyTrack.c
|
||||||
g++ -o $@ -c $< `pkg-config opencv --cflags` $(CFLAGS)
|
g++ -o $@ -c $< $(CFLAGS)
|
||||||
|
|
||||||
|
Navy : Navy.o fonction.o
|
||||||
|
g++ -o $@ $^ $(LDFLAGS) $(CFLAGS)
|
||||||
|
|
||||||
|
Navy.o : Navy.c fonction.h
|
||||||
|
g++ -o $@ -c $< $(CFLAGS)
|
||||||
|
|
||||||
|
fonction.o : fonction.c fonction.h
|
||||||
|
g++ -o $@ -c $< $(CFLAGS)
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
rm -f *.o $(EXEC)
|
rm -f *.o $(EXEC)
|
||||||
|
|
303
Code/Navy.c
303
Code/Navy.c
|
@ -1,128 +1,61 @@
|
||||||
#include <stdio.h>
|
#include "fonction.h"
|
||||||
#include <stdlib.h>
|
|
||||||
#include <cv.h>
|
|
||||||
#include <highgui.h>
|
|
||||||
//#include <opencv2/highgui.hpp> //Pour le cvRound
|
|
||||||
//#include "opencv2/videoio/videoio_c.h" //Pour le CvCapture*
|
|
||||||
|
|
||||||
//#include <cxcore.h>
|
|
||||||
//#include <SFML/Window.h>
|
|
||||||
#include <SFML/Audio.hpp>
|
|
||||||
#include <SFML/Graphics.hpp>
|
|
||||||
#include <SFML/Window.hpp>
|
|
||||||
#include <SFML/System.hpp> //inutilisé pour le moment
|
|
||||||
|
|
||||||
//#define CONFIG
|
|
||||||
#define SFML
|
|
||||||
|
|
||||||
#define KIRBY
|
|
||||||
//#define ETOILE
|
|
||||||
|
|
||||||
#define JEU 0.15
|
|
||||||
|
|
||||||
//ATTENTION AFFICHAGE OPENCV INCOMPATIBLE AVEC AFFICHAGE SFML
|
|
||||||
//ATTENTION SFML SUPPORTE UN NOMBRE LIMITE DE SPRITE EN FCT DU PC
|
|
||||||
|
|
||||||
/*Headers*/
|
|
||||||
void maj_angle(int vecX, int vecY, int rayon, double* angle); //Met à jour l'angle selon la distance CentreCamera - Cible
|
|
||||||
int ajust_pos(int pos, int ref);
|
|
||||||
void controle_moteur(double* angle);//Envoie les angles au moteur
|
|
||||||
int limite_moteur(int val_pwm);//Verifie que les valeurs envoyees aux moteurs sont correctes
|
|
||||||
|
|
||||||
void config(int* LowH, int* HighH, int* LowS, int* HighS, int* LowV, int* HighV); //Affiche le panneau de configuration de tracking avec les arguments comme valeur de base
|
|
||||||
void affichage_config(IplImage* frame, IplImage* HSV, IplImage* Binaire); //Affiche le flux vidéos et ses différent traitements
|
|
||||||
void Affichage_Tracking(IplImage* frame, int posX, int posY, int width, int height); //Dessine les informations de tracking sur frame
|
|
||||||
|
|
||||||
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 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
|
|
||||||
|
|
||||||
int image_CV2SFML(IplImage* imcv, sf::Image imFlux); //Construction de imsf (RGBA) à partir de imcv (BGR), avec alpha constant (=1)
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
//Initialisations
|
//Initialisations
|
||||||
int height,width; //parameters of the image we are working on
|
int height,width; //parameters of the image we are working on
|
||||||
int posX, posY; //Position objet
|
int posX, posY; //Position objet
|
||||||
int boucle;
|
int boucle;
|
||||||
|
|
||||||
double angle[2] = {100,100};
|
double angle[2] = {100,100};
|
||||||
|
|
||||||
int tracking;
|
int tracking;
|
||||||
|
|
||||||
|
|
||||||
#ifdef SFML
|
|
||||||
//Initialisation SFML
|
|
||||||
|
|
||||||
sf::Texture txFlux;
|
|
||||||
sf::Sprite spFlux;
|
|
||||||
sf::Image imFlux;
|
|
||||||
sf::Event event;
|
|
||||||
|
|
||||||
tracking = 0; //Pas de tracking de base en mode SFML
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//Ouverture flux camera
|
//Ouverture flux camera
|
||||||
CvCapture* capture = cvCaptureFromCAM( 0 );
|
CvCapture* capture = cvCaptureFromCAM( 0 );
|
||||||
|
|
||||||
if( !capture ){
|
if( !capture ){
|
||||||
printf("ERROR: capture is NULL \n" );
|
printf("ERROR: capture is NULL \n" );
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Initialisation frame
|
||||||
// grab an image from the capture
|
|
||||||
IplImage* frame = cvQueryFrame( capture );
|
IplImage* frame = cvQueryFrame( capture );
|
||||||
|
height = frame->height;
|
||||||
// get the image data
|
width = frame->width;
|
||||||
height = frame->height;
|
CvSize size = cvSize(width,height);
|
||||||
width = frame->width;
|
IplImage* hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); // image converted to HSV plane
|
||||||
|
IplImage* threshold = cvCreateImage(size, IPL_DEPTH_8U, 1);
|
||||||
// capture size -
|
|
||||||
CvSize size = cvSize(width,height);
|
//Initialisation detection visage
|
||||||
|
CvHaarClassifierCascade* cascade = init_cascade();
|
||||||
|
|
||||||
#ifdef SFML
|
#ifdef SFML
|
||||||
|
//Initialisation SFML
|
||||||
|
sf::Texture txFlux;
|
||||||
|
sf::Sprite spFlux;
|
||||||
|
sf::Image imFlux;
|
||||||
|
sf::Event event;
|
||||||
|
|
||||||
|
tracking = 0; //Pas de tracking de base en mode SFML
|
||||||
//Création de la fenetre principale
|
//Création de la fenetre principale
|
||||||
sf::RenderWindow window(sf::VideoMode(width+300, height), "KirbyTrack");
|
sf::RenderWindow window(sf::VideoMode(width+300, height), "KirbyTrack");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
|
|
||||||
//Controle couleur
|
//Controle couleur
|
||||||
#ifdef KIRBY
|
#ifdef KIRBY
|
||||||
//Setup Kirby
|
int tab_HSV[6] = {152,179,48,255,101,255};
|
||||||
int iLowH = 152;
|
|
||||||
int iHighH = 179;
|
|
||||||
|
|
||||||
int iLowS = 48;
|
|
||||||
int iHighS = 255;
|
|
||||||
|
|
||||||
int iLowV = 101;
|
|
||||||
int iHighV = 255;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef ETOILE
|
#ifdef ETOILE
|
||||||
//Setup Etoile
|
int tab_HSV[6] = {20,30,100,255,100,255};
|
||||||
int iLowH = 20;
|
|
||||||
int iHighH = 30;
|
|
||||||
|
|
||||||
int iLowS = 100;
|
|
||||||
int iHighS = 255;
|
|
||||||
|
|
||||||
int iLowV = 100;
|
|
||||||
int iHighV = 255;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG
|
#ifdef CONFIG
|
||||||
//Affichage du panneau de config
|
//Affichage du panneau de config
|
||||||
config(&iLowH, &iHighH, &iLowS, &iHighS, &iLowV, &iHighV);
|
config(tab_HSV,tab_HSV+1,tab_HSV+2,tab_HSV+3,tab_HSV+4,tab_HSV+5);
|
||||||
|
|
||||||
boucle = 1;
|
boucle = 1;
|
||||||
tracking = 1; //Tracking de base en mode CONFIG
|
tracking = 0; //Tracking de base en mode CONFIG
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while(boucle)
|
while(boucle)
|
||||||
|
@ -148,15 +81,7 @@ int main(int argc, char* argv[])
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Binarisation du flux vidéo
|
detect_and_draw(frame,cascade);
|
||||||
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
|
#ifdef SFML
|
||||||
//Affichage SFML
|
//Affichage SFML
|
||||||
|
@ -183,7 +108,7 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
window.draw(spFlux);
|
window.draw(spFlux);
|
||||||
|
|
||||||
//TEST SFML
|
//TEST SFML
|
||||||
sf::Vector2i PosMouse = sf::Mouse::getPosition(window);
|
sf::Vector2i PosMouse = sf::Mouse::getPosition(window);
|
||||||
//Detection du bouton tracking
|
//Detection du bouton tracking
|
||||||
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)&&(PosMouse.x>640)&&(PosMouse.x<760)&&(PosMouse.y>0)&&(PosMouse.y<120)){
|
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)&&(PosMouse.x>640)&&(PosMouse.x<760)&&(PosMouse.y>0)&&(PosMouse.y<120)){
|
||||||
|
@ -268,9 +193,9 @@ int main(int argc, char* argv[])
|
||||||
*/
|
*/
|
||||||
/* Update the window */
|
/* Update the window */
|
||||||
window.display();
|
window.display();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
if(tracking){
|
if(tracking){
|
||||||
//Mouvements moteurs
|
//Mouvements moteurs
|
||||||
//printf("-PREMAJ_ANGLE...: %d %d\n",width,height);
|
//printf("-PREMAJ_ANGLE...: %d %d\n",width,height);
|
||||||
|
@ -283,8 +208,9 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
|
|
||||||
#ifdef CONFIG
|
#ifdef CONFIG
|
||||||
|
/*
|
||||||
affichage_config(frame, hsv_frame, threshold); //Affichage du flux vidéo et de ses traitements
|
affichage_config(frame, hsv_frame, threshold); //Affichage du flux vidéo et de ses traitements
|
||||||
|
*/
|
||||||
if( (cvWaitKey(10) ) >= 0 ) break; //Arret capture
|
if( (cvWaitKey(10) ) >= 0 ) break; //Arret capture
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -294,180 +220,9 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
// Release the capture device housekeeping
|
// Release the capture device housekeeping
|
||||||
cvReleaseCapture( &capture );
|
cvReleaseCapture( &capture );
|
||||||
|
|
||||||
cvReleaseImage(&threshold);
|
cvReleaseImage(&threshold);
|
||||||
cvReleaseImage(&hsv_frame);
|
cvReleaseImage(&hsv_frame);
|
||||||
cvReleaseImage(&frame);
|
cvReleaseImage(&frame);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void maj_angle(int vecX, int vecY, int rayon, double* angle){
|
|
||||||
//On ajustera coeff fonction du rayon. Si la cible est à une distance 5*r, il sera 5x plus rapide que s'il était à 1*r
|
|
||||||
|
|
||||||
double coeffx, coeffy;
|
|
||||||
int l0, l1;
|
|
||||||
|
|
||||||
//printf("-MAJ_ANGLE...Valeur maj_angle arguments : %d %d %d\n\tAnciens angles : %d %d\n\t",vecX,vecY,rayon,(int)angle[0],(int)angle[1]);
|
|
||||||
|
|
||||||
|
|
||||||
//Ajout d'un angle moteur pondéré par la distance
|
|
||||||
coeffx = -0.2*vecX/rayon;
|
|
||||||
coeffy = 0.2*vecY/rayon;
|
|
||||||
angle[0] += coeffx;
|
|
||||||
angle[1] += coeffy;
|
|
||||||
|
|
||||||
//Majoration - minoration des angles moteurs
|
|
||||||
l0 = limite_moteur(angle[0]);
|
|
||||||
l1 = limite_moteur(angle[1]);
|
|
||||||
if (l0 != 0) angle[0] = l0;
|
|
||||||
if (l1 != 0) angle[1] = l1;
|
|
||||||
|
|
||||||
//printf("Nouveaux angles : %lf %lf %d %d\n",angle[0],angle[1],(int)angle[0],(int)angle[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
int ajust_pos(int pos, int ref){
|
|
||||||
if (pos > ref) return 0;
|
|
||||||
else return pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
int limite_moteur(int val_pwm){
|
|
||||||
int MAX_PWM = 130, MIN_PWM = 30;
|
|
||||||
if (val_pwm > MAX_PWM){
|
|
||||||
return MAX_PWM;
|
|
||||||
}
|
|
||||||
else if (val_pwm < MIN_PWM){
|
|
||||||
return MIN_PWM;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void controle_moteur(double* angle){
|
|
||||||
|
|
||||||
//Ouverture port serie
|
|
||||||
FILE* fichier = NULL;
|
|
||||||
fichier = fopen("/dev/ttyACM0","w");
|
|
||||||
if(fichier==NULL){
|
|
||||||
printf("Erreur ouverture fichier\n");
|
|
||||||
perror("fopen failed for /dev/ttyACM0" );
|
|
||||||
exit( EXIT_FAILURE );
|
|
||||||
}
|
|
||||||
|
|
||||||
//Ecriture angles
|
|
||||||
fprintf(fichier,"%d\n",(int)angle[0]);
|
|
||||||
fprintf(fichier,"%d\n",(int)angle[1]);
|
|
||||||
|
|
||||||
//Fermeture
|
|
||||||
fclose(fichier);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int image_CV2SFML(IplImage* imcv, sf::Image imFlux){
|
|
||||||
|
|
||||||
int R, G, B;
|
|
||||||
int w = imcv->widthStep;
|
|
||||||
char* ptr = imcv->imageData;
|
|
||||||
|
|
||||||
imFlux.create(imcv->width,imcv->height, NULL); //Initialise une image vide
|
|
||||||
|
|
||||||
for( int y=0; y<imcv->height; y++ ) {
|
|
||||||
//uchar* ptr = (uchar*) ( imcv->imageData + y * imcv->widthStep );
|
|
||||||
for( int x=0; x<imcv->width; x++ ) {
|
|
||||||
//Recupération du pixel
|
|
||||||
B = ptr[y*w + 3*x];
|
|
||||||
G = ptr[y*w + 3*x + 1];
|
|
||||||
R = ptr[y*w + 3*x + 2];
|
|
||||||
|
|
||||||
//Ecriture du pixel associé
|
|
||||||
imFlux.setPixel(x,y,sf::Color(R,G,B,1)); //Alpha channel = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
// Covert color space to HSV as it is much easier to filter colors in the HSV color-space.
|
|
||||||
cvCvtColor(frame, HSV, CV_BGR2HSV);
|
|
||||||
|
|
||||||
//Blur
|
|
||||||
cvSmooth( HSV, HSV, CV_GAUSSIAN, 15, 0,0,0); //suppression des parasites par flou gaussien
|
|
||||||
|
|
||||||
//Binarisation
|
|
||||||
|
|
||||||
CvScalar valinf={(double)LowH,(double)LowS,(double)LowV};
|
|
||||||
CvScalar valsup={(double)HighH,(double)HighS,(double)HighV};
|
|
||||||
|
|
||||||
cvInRangeS(HSV, valinf,valsup, Binaire);
|
|
||||||
|
|
||||||
//En cas d'erreur sur les trois ligne précédentes
|
|
||||||
//cvInRangeS(HSV, CvScalar(LowH,LowS,LowV),CvScalar(HighH,HighS,HighV), Binaire);
|
|
||||||
|
|
||||||
//cvSmooth( Binaire, Binaire, CV_GAUSSIAN, 9, 9 ); //Legère suppression des parasites
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
|
|
||||||
|
|
||||||
cvMoments(Binaire, moments, 1);
|
|
||||||
// The actual moment values
|
|
||||||
double moment10 = cvGetSpatialMoment(moments, 1, 0);
|
|
||||||
double moment01 = cvGetSpatialMoment(moments, 0, 1);
|
|
||||||
double area = cvGetCentralMoment(moments, 0, 0);
|
|
||||||
|
|
||||||
*posX = moment10/area;
|
|
||||||
*posY = moment01/area;
|
|
||||||
|
|
||||||
free(moments);
|
|
||||||
}
|
|
||||||
|
|
||||||
void config(int* LowH, int* HighH, int* LowS, int* HighS, int* LowV, int* HighV){ //Affiche le panneau de configuration de tracking avec les arguments comme valeur de base
|
|
||||||
|
|
||||||
cvNamedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"
|
|
||||||
|
|
||||||
//Create trackbars in "Control" window
|
|
||||||
cvCreateTrackbar("LowH", "Control", LowH, 179,NULL); //Hue (0 - 179)
|
|
||||||
cvCreateTrackbar("HighH", "Control", HighH, 179,NULL);
|
|
||||||
|
|
||||||
cvCreateTrackbar("LowS", "Control", LowS, 255,NULL); //Saturation (0 - 255)
|
|
||||||
cvCreateTrackbar("HighS", "Control", HighS, 255,NULL);
|
|
||||||
|
|
||||||
cvCreateTrackbar("LowV", "Control", LowV, 255,NULL); //Value (0 - 255)
|
|
||||||
cvCreateTrackbar("HighV", "Control", HighV, 255,NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void affichage_config(IplImage* frame, IplImage* HSV, IplImage* Binaire){ //Affiche le flux vidéos et ses différent traitements
|
|
||||||
|
|
||||||
// Create a window in which the captured images will be presented
|
|
||||||
cvNamedWindow( "HSV", CV_WINDOW_AUTOSIZE );
|
|
||||||
cvNamedWindow( "Binaire", CV_WINDOW_AUTOSIZE );
|
|
||||||
cvNamedWindow( "Camera", CV_WINDOW_AUTOSIZE );
|
|
||||||
|
|
||||||
cvShowImage( "HSV", HSV); // Original stream in the HSV color space
|
|
||||||
cvShowImage( "Binaire", Binaire); // The stream after color filtering
|
|
||||||
cvShowImage( "Camera", frame ); // Flux caméra avec tracking objet
|
|
||||||
}
|
|
||||||
|
|
||||||
void Affichage_Tracking(IplImage* frame, int posX, int posY, int width, int height){ //Dessine les informations de tracking sur frame
|
|
||||||
|
|
||||||
//Affichage zone suivie objet
|
|
||||||
cvCircle(frame, cvPoint(width/2,height/2), height*JEU, CV_RGB(0, 255, 0), 4, 8, 0 );
|
|
||||||
|
|
||||||
if(posX<5&&posY<5){ //Si aucun objet spotted, pointeur rouge au centre
|
|
||||||
posX=width/2;
|
|
||||||
posY=height/2;
|
|
||||||
cvLine(frame, cvPoint(posX-20,posY), cvPoint(posX+20,posY), CV_RGB(255, 0, 0), 4, 8, 0 );
|
|
||||||
cvLine(frame, cvPoint(posX,posY-20), cvPoint(posX,posY+20), CV_RGB(255, 0, 0), 4, 8, 0 );
|
|
||||||
}
|
|
||||||
else{ //Objet spotted
|
|
||||||
//Affichage position de l'objet
|
|
||||||
cvLine(frame, cvPoint(posX-20,posY), cvPoint(posX+20,posY), CV_RGB(0, 0, 255), 4, 8, 0 );
|
|
||||||
cvLine(frame, cvPoint(posX,posY-20), cvPoint(posX,posY+20), CV_RGB(0, 0, 255), 4, 8, 0 );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
125
Code/facedetect.c
Normal file
125
Code/facedetect.c
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
// Sample Application to demonstrate how Face detection can be done as a part of your source code.
|
||||||
|
|
||||||
|
// Include header files
|
||||||
|
#include "cv.h"
|
||||||
|
#include "highgui.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <float.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
|
||||||
|
// Create a string that contains the exact cascade name
|
||||||
|
const char* cascade_name =
|
||||||
|
"C:/Program Files/OpenCV/data/haarcascades/haarcascade_frontalface_alt.xml";
|
||||||
|
/* "haarcascade_profileface.xml";*/
|
||||||
|
|
||||||
|
|
||||||
|
// Function prototype for detecting and drawing an object from an image
|
||||||
|
void detect_and_draw( IplImage* image );
|
||||||
|
|
||||||
|
// Main function, defines the entry point for the program.
|
||||||
|
int main( int argc, char** argv )
|
||||||
|
{
|
||||||
|
|
||||||
|
//Ouverture flux camera
|
||||||
|
CvCapture* capture = cvCaptureFromCAM( 0 );
|
||||||
|
|
||||||
|
if( !capture ){
|
||||||
|
printf("ERROR: capture is NULL \n" );
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the function to detect and draw the face positions
|
||||||
|
detect_and_draw(img);
|
||||||
|
|
||||||
|
// Wait for user input before quitting the program
|
||||||
|
cvWaitKey();
|
||||||
|
|
||||||
|
// Release the image
|
||||||
|
cvReleaseImage(&img);
|
||||||
|
|
||||||
|
// Destroy the window previously created with filename: "result"
|
||||||
|
cvDestroyWindow("result");
|
||||||
|
|
||||||
|
// return 0 to indicate successfull execution of the program
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to detect and draw any faces that is present in an image
|
||||||
|
void detect_and_draw( IplImage* img )
|
||||||
|
{
|
||||||
|
|
||||||
|
// Create memory for calculations
|
||||||
|
static CvMemStorage* storage = 0;
|
||||||
|
|
||||||
|
// Create a new Haar classifier
|
||||||
|
static CvHaarClassifierCascade* cascade = 0;
|
||||||
|
|
||||||
|
int scale = 1;
|
||||||
|
|
||||||
|
// Create a new image based on the input image
|
||||||
|
IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
|
||||||
|
|
||||||
|
// Create two points to represent the face locations
|
||||||
|
CvPoint pt1, pt2;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
// Load the [[HaarClassifierCascade]]
|
||||||
|
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
|
||||||
|
|
||||||
|
// Check whether the cascade has loaded successfully. Else report and error and quit
|
||||||
|
if( !cascade )
|
||||||
|
{
|
||||||
|
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate the memory storage
|
||||||
|
storage = cvCreateMemStorage(0);
|
||||||
|
|
||||||
|
// Create a new named window with title: result
|
||||||
|
cvNamedWindow( "result", 1 );
|
||||||
|
|
||||||
|
// Clear the memory storage which was used before
|
||||||
|
cvClearMemStorage( storage );
|
||||||
|
|
||||||
|
// Find whether the cascade is loaded, to find the faces. If yes, then:
|
||||||
|
if( cascade )
|
||||||
|
{
|
||||||
|
|
||||||
|
// There can be more than one face in an image. So create a growable sequence of faces.
|
||||||
|
// Detect the objects and store them in the sequence
|
||||||
|
CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
|
||||||
|
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
|
||||||
|
cvSize(40, 40) );
|
||||||
|
|
||||||
|
// Loop the number of faces found.
|
||||||
|
for( i = 0; i < (faces ? faces->total : 0); i++ )
|
||||||
|
{
|
||||||
|
// Create a new rectangle for drawing the face
|
||||||
|
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
|
||||||
|
|
||||||
|
// Find the dimensions of the face,and scale it if necessary
|
||||||
|
pt1.x = r->x*scale;
|
||||||
|
pt2.x = (r->x+r->width)*scale;
|
||||||
|
pt1.y = r->y*scale;
|
||||||
|
pt2.y = (r->y+r->height)*scale;
|
||||||
|
|
||||||
|
// Draw the rectangle in the input image
|
||||||
|
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show the image in the window named "result"
|
||||||
|
cvShowImage( "result", img );
|
||||||
|
|
||||||
|
// Release the temp image created.
|
||||||
|
cvReleaseImage( &temp );
|
||||||
|
}
|
253
Code/fonction.c
Normal file
253
Code/fonction.c
Normal file
|
@ -0,0 +1,253 @@
|
||||||
|
#include "fonction.h"
|
||||||
|
|
||||||
|
|
||||||
|
void maj_angle(int vecX, int vecY, int rayon, double* angle){
|
||||||
|
//On ajustera coeff fonction du rayon. Si la cible est à une distance 5*r, il sera 5x plus rapide que s'il était à 1*r
|
||||||
|
|
||||||
|
double coeffx, coeffy;
|
||||||
|
int l0, l1;
|
||||||
|
|
||||||
|
//printf("-MAJ_ANGLE...Valeur maj_angle arguments : %d %d %d\n\tAnciens angles : %d %d\n\t",vecX,vecY,rayon,(int)angle[0],(int)angle[1]);
|
||||||
|
|
||||||
|
|
||||||
|
//Ajout d'un angle moteur pondéré par la distance
|
||||||
|
coeffx = -0.2*vecX/rayon;
|
||||||
|
coeffy = 0.2*vecY/rayon;
|
||||||
|
angle[0] += coeffx;
|
||||||
|
angle[1] += coeffy;
|
||||||
|
|
||||||
|
//Majoration - minoration des angles moteurs
|
||||||
|
l0 = limite_moteur(angle[0]);
|
||||||
|
l1 = limite_moteur(angle[1]);
|
||||||
|
if (l0 != 0) angle[0] = l0;
|
||||||
|
if (l1 != 0) angle[1] = l1;
|
||||||
|
|
||||||
|
//printf("Nouveaux angles : %lf %lf %d %d\n",angle[0],angle[1],(int)angle[0],(int)angle[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ajust_pos(int pos, int ref){
|
||||||
|
if (pos > ref) return 0;
|
||||||
|
else return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
int limite_moteur(int val_pwm){
|
||||||
|
int MAX_PWM = 130, MIN_PWM = 30;
|
||||||
|
if (val_pwm > MAX_PWM){
|
||||||
|
return MAX_PWM;
|
||||||
|
}
|
||||||
|
else if (val_pwm < MIN_PWM){
|
||||||
|
return MIN_PWM;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void controle_moteur(double* angle){
|
||||||
|
|
||||||
|
//Ouverture port serie
|
||||||
|
FILE* fichier = NULL;
|
||||||
|
fichier = fopen("/dev/ttyACM0","w");
|
||||||
|
if(fichier==NULL){
|
||||||
|
printf("Erreur ouverture fichier\n");
|
||||||
|
perror("fopen failed for /dev/ttyACM0" );
|
||||||
|
exit( EXIT_FAILURE );
|
||||||
|
}
|
||||||
|
|
||||||
|
//Ecriture angles
|
||||||
|
fprintf(fichier,"%d\n",(int)angle[0]);
|
||||||
|
fprintf(fichier,"%d\n",(int)angle[1]);
|
||||||
|
|
||||||
|
//Fermeture
|
||||||
|
fclose(fichier);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int image_CV2SFML(IplImage* imcv, sf::Image imFlux){
|
||||||
|
|
||||||
|
int R, G, B;
|
||||||
|
int w = imcv->widthStep;
|
||||||
|
char* ptr = imcv->imageData;
|
||||||
|
|
||||||
|
imFlux.create(imcv->width,imcv->height, NULL); //Initialise une image vide
|
||||||
|
|
||||||
|
for( int y=0; y<imcv->height; y++ ) {
|
||||||
|
//uchar* ptr = (uchar*) ( imcv->imageData + y * imcv->widthStep );
|
||||||
|
for( int x=0; x<imcv->width; x++ ) {
|
||||||
|
//Recupération du pixel
|
||||||
|
B = ptr[y*w + 3*x];
|
||||||
|
G = ptr[y*w + 3*x + 1];
|
||||||
|
R = ptr[y*w + 3*x + 2];
|
||||||
|
|
||||||
|
//Ecriture du pixel associé
|
||||||
|
imFlux.setPixel(x,y,sf::Color(R,G,B,1)); //Alpha channel = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
// Covert color space to HSV as it is much easier to filter colors in the HSV color-space.
|
||||||
|
cvCvtColor(frame, HSV, CV_BGR2HSV);
|
||||||
|
|
||||||
|
//Blur
|
||||||
|
cvSmooth( HSV, HSV, CV_GAUSSIAN, 15, 0,0,0); //suppression des parasites par flou gaussien
|
||||||
|
|
||||||
|
//Binarisation
|
||||||
|
|
||||||
|
CvScalar valinf={(double)LowH,(double)LowS,(double)LowV};
|
||||||
|
CvScalar valsup={(double)HighH,(double)HighS,(double)HighV};
|
||||||
|
|
||||||
|
cvInRangeS(HSV, valinf,valsup, Binaire);
|
||||||
|
|
||||||
|
//En cas d'erreur sur les trois ligne précédentes
|
||||||
|
//cvInRangeS(HSV, CvScalar(LowH,LowS,LowV),CvScalar(HighH,HighS,HighV), Binaire);
|
||||||
|
|
||||||
|
//cvSmooth( Binaire, Binaire, CV_GAUSSIAN, 9, 9 ); //Legère suppression des parasites
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
|
||||||
|
|
||||||
|
cvMoments(Binaire, moments, 1);
|
||||||
|
// The actual moment values
|
||||||
|
double moment10 = cvGetSpatialMoment(moments, 1, 0);
|
||||||
|
double moment01 = cvGetSpatialMoment(moments, 0, 1);
|
||||||
|
double area = cvGetCentralMoment(moments, 0, 0);
|
||||||
|
|
||||||
|
*posX = moment10/area;
|
||||||
|
*posY = moment01/area;
|
||||||
|
|
||||||
|
free(moments);
|
||||||
|
}
|
||||||
|
|
||||||
|
void config(int* LowH, int* HighH, int* LowS, int* HighS, int* LowV, int* HighV){ //Affiche le panneau de configuration de tracking avec les arguments comme valeur de base
|
||||||
|
|
||||||
|
cvNamedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"
|
||||||
|
|
||||||
|
//Create trackbars in "Control" window
|
||||||
|
cvCreateTrackbar("LowH", "Control", LowH, 179,NULL); //Hue (0 - 179)
|
||||||
|
cvCreateTrackbar("HighH", "Control", HighH, 179,NULL);
|
||||||
|
|
||||||
|
cvCreateTrackbar("LowS", "Control", LowS, 255,NULL); //Saturation (0 - 255)
|
||||||
|
cvCreateTrackbar("HighS", "Control", HighS, 255,NULL);
|
||||||
|
|
||||||
|
cvCreateTrackbar("LowV", "Control", LowV, 255,NULL); //Value (0 - 255)
|
||||||
|
cvCreateTrackbar("HighV", "Control", HighV, 255,NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void affichage_config(IplImage* frame, IplImage* HSV, IplImage* Binaire){ //Affiche le flux vidéos et ses différent traitements
|
||||||
|
|
||||||
|
// Create a window in which the captured images will be presented
|
||||||
|
cvNamedWindow( "HSV", CV_WINDOW_AUTOSIZE );
|
||||||
|
cvNamedWindow( "Binaire", CV_WINDOW_AUTOSIZE );
|
||||||
|
cvNamedWindow( "Camera", CV_WINDOW_AUTOSIZE );
|
||||||
|
|
||||||
|
cvShowImage( "HSV", HSV); // Original stream in the HSV color space
|
||||||
|
cvShowImage( "Binaire", Binaire); // The stream after color filtering
|
||||||
|
cvShowImage( "Camera", frame ); // Flux caméra avec tracking objet
|
||||||
|
}
|
||||||
|
|
||||||
|
void Affichage_Tracking(IplImage* frame, int posX, int posY, int width, int height){ //Dessine les informations de tracking sur frame
|
||||||
|
|
||||||
|
//Affichage zone suivie objet
|
||||||
|
cvCircle(frame, cvPoint(width/2,height/2), height*JEU, CV_RGB(0, 255, 0), 4, 8, 0 );
|
||||||
|
|
||||||
|
if(posX<5&&posY<5){ //Si aucun objet spotted, pointeur rouge au centre
|
||||||
|
posX=width/2;
|
||||||
|
posY=height/2;
|
||||||
|
cvLine(frame, cvPoint(posX-20,posY), cvPoint(posX+20,posY), CV_RGB(255, 0, 0), 4, 8, 0 );
|
||||||
|
cvLine(frame, cvPoint(posX,posY-20), cvPoint(posX,posY+20), CV_RGB(255, 0, 0), 4, 8, 0 );
|
||||||
|
}
|
||||||
|
else{ //Objet spotted
|
||||||
|
//Affichage position de l'objet
|
||||||
|
cvLine(frame, cvPoint(posX-20,posY), cvPoint(posX+20,posY), CV_RGB(0, 0, 255), 4, 8, 0 );
|
||||||
|
cvLine(frame, cvPoint(posX,posY-20), cvPoint(posX,posY+20), CV_RGB(0, 0, 255), 4, 8, 0 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
CvHaarClassifierCascade* init_cascade(){
|
||||||
|
// Create a new Haar classifier
|
||||||
|
CvHaarClassifierCascade* cascade = 0;
|
||||||
|
const char* cascade_name = "haarcascade_frontalface_alt.xml";
|
||||||
|
|
||||||
|
|
||||||
|
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
|
||||||
|
|
||||||
|
// Check whether the cascade has loaded successfully. Else report and error and quit
|
||||||
|
if( !cascade ){
|
||||||
|
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
|
||||||
|
perror(" ");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return cascade;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to detect and draw any faces that is present in an image
|
||||||
|
void detect_and_draw( IplImage* img, CvHaarClassifierCascade* cascade )
|
||||||
|
{
|
||||||
|
|
||||||
|
pface* tab_face[2];
|
||||||
|
|
||||||
|
|
||||||
|
// Create memory for calculations
|
||||||
|
static CvMemStorage* storage = 0;
|
||||||
|
int scale = 1;
|
||||||
|
|
||||||
|
// Create a new image based on the input image
|
||||||
|
IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
|
||||||
|
|
||||||
|
// Create two points to represent the face locations
|
||||||
|
CvPoint pt1, pt2;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
// Allocate the memory storage
|
||||||
|
storage = cvCreateMemStorage(0);
|
||||||
|
|
||||||
|
// Create a new named window with title: result
|
||||||
|
cvNamedWindow( "result", 1 );
|
||||||
|
|
||||||
|
// Clear the memory storage which was used before
|
||||||
|
cvClearMemStorage( storage );
|
||||||
|
|
||||||
|
// Find whether the cascade is loaded, to find the faces. If yes, then:
|
||||||
|
if( cascade ){
|
||||||
|
|
||||||
|
// There can be more than one face in an image. So create a growable sequence of faces.
|
||||||
|
// Detect the objects and store them in the sequence
|
||||||
|
CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,1.1, 2, 0, cvSize(60, 60),cvSize(500, 500));
|
||||||
|
|
||||||
|
// Loop the number of faces found.
|
||||||
|
for( i = 0; i < (faces ? faces->total : 0); i++ ){
|
||||||
|
// Create a new rectangle for drawing the face
|
||||||
|
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
|
||||||
|
|
||||||
|
// Find the dimensions of the face,and scale it if necessary
|
||||||
|
pt1.x = r->x*scale;
|
||||||
|
pt2.x = (r->x+r->width)*scale;
|
||||||
|
pt1.y = r->y*scale;
|
||||||
|
pt2.y = (r->y+r->height)*scale;
|
||||||
|
|
||||||
|
// Draw the rectangle in the input image
|
||||||
|
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
|
||||||
|
|
||||||
|
if(i < MAX_FACE){
|
||||||
|
tab_face[i]->point =
|
||||||
|
tab_face[i]->largeur = r->width;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show the image in the window named "result"
|
||||||
|
cvShowImage( "result", img );
|
||||||
|
|
||||||
|
//free
|
||||||
|
cvReleaseImage( &temp );
|
||||||
|
}
|
60
Code/fonction.h
Normal file
60
Code/fonction.h
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
#ifndef FONCTION_H
|
||||||
|
#define FONCTION_H
|
||||||
|
|
||||||
|
|
||||||
|
/*INCLUDE*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <cv.h>
|
||||||
|
#include <highgui.h>
|
||||||
|
//#include <opencv2/highgui.hpp> //Pour le cvRound
|
||||||
|
//#include "opencv2/videoio/videoio_c.h" //Pour le CvCapture*
|
||||||
|
//#include <cxcore.h>
|
||||||
|
//#include <SFML/Window.h>
|
||||||
|
#include <SFML/Audio.hpp>
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Window.hpp>
|
||||||
|
#include <SFML/System.hpp> //inutilisé pour le moment
|
||||||
|
|
||||||
|
/*DEFINE*/
|
||||||
|
//ATTENTION AFFICHAGE OPENCV INCOMPATIBLE AVEC AFFICHAGE SFML
|
||||||
|
//ATTENTION SFML SUPPORTE UN NOMBRE LIMITE DE SPRITE EN FCT DU PC
|
||||||
|
#define JEU 0.15
|
||||||
|
#define CONFIG
|
||||||
|
//#define SFML
|
||||||
|
#define KIRBY
|
||||||
|
//#define ETOILE
|
||||||
|
|
||||||
|
#define MAX_FACE 2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*STRUCTURE*/
|
||||||
|
typedef struct _face{
|
||||||
|
CvPoint point;
|
||||||
|
int largeur;
|
||||||
|
} face
|
||||||
|
|
||||||
|
typedef struct pface *face;
|
||||||
|
|
||||||
|
|
||||||
|
/*HEADERS*/
|
||||||
|
void maj_angle(int vecX, int vecY, int rayon, double* angle); //Met à jour l'angle selon la distance CentreCamera - Cible
|
||||||
|
int ajust_pos(int pos, int ref);
|
||||||
|
void controle_moteur(double* angle);//Envoie les angles au moteur
|
||||||
|
int limite_moteur(int val_pwm);//Verifie que les valeurs envoyees aux moteurs sont correctes
|
||||||
|
|
||||||
|
void config(int* LowH, int* HighH, int* LowS, int* HighS, int* LowV, int* HighV); //Affiche le panneau de configuration de tracking avec les arguments comme valeur de base
|
||||||
|
void affichage_config(IplImage* frame, IplImage* HSV, IplImage* Binaire); //Affiche le flux vidéos et ses différent traitements
|
||||||
|
void Affichage_Tracking(IplImage* frame, int posX, int posY, int width, int height); //Dessine les informations de tracking sur frame
|
||||||
|
|
||||||
|
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 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
|
||||||
|
|
||||||
|
int image_CV2SFML(IplImage* imcv, sf::Image imFlux); //Construction de imsf (RGBA) à partir de imcv (BGR), avec alpha constant (=1)
|
||||||
|
|
||||||
|
CvHaarClassifierCascade* init_cascade();
|
||||||
|
void detect_and_draw( IplImage* img, CvHaarClassifierCascade* cascade );
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
BIN
Code/gmon.out
Normal file
BIN
Code/gmon.out
Normal file
Binary file not shown.
26161
Code/haarcascade_frontalface_alt.xml
Normal file
26161
Code/haarcascade_frontalface_alt.xml
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue