CamAdventure
 All Classes Files Functions Variables Macros Pages
fonction.c
Go to the documentation of this file.
1 
9 #include "fonction.h"
10 
11 
12 void maj_angle(int vecX, int vecY, int rayon, double* angle){
13  //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
14 
15  double coeffx, coeffy;
16  int l0, l1;
17 
18  //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]);
19 
20 
21  //Ajout d'un angle moteur pondéré par la distance
22  coeffx = -0.2*vecX/rayon;
23  coeffy = 0.2*vecY/rayon;
24  angle[0] += coeffx;
25  angle[1] += coeffy;
26 
27  //Majoration - minoration des angles moteurs
28  l0 = limite_moteur(angle[0]);
29  l1 = limite_moteur(angle[1]);
30  if (l0 != 0) angle[0] = l0;
31  if (l1 != 0) angle[1] = l1;
32 
33  //printf("Nouveaux angles : %lf %lf %d %d\n",angle[0],angle[1],(int)angle[0],(int)angle[0]);
34 }
35 
36 int ajust_pos(int pos, int ref){
37  if (pos > ref) return 0;
38  else return pos;
39 }
40 
41 int limite_moteur(int val_pwm){
42  int MAX_PWM = 130, MIN_PWM = 30;
43  if (val_pwm > MAX_PWM){
44  return MAX_PWM;
45  }
46  else if (val_pwm < MIN_PWM){
47  return MIN_PWM;
48  }
49  else{
50  return 0;
51  }
52 }
53 
54 void controle_moteur(double* angle){
55 
56  //Ouverture port serie
57  FILE* fichier = NULL;
58  fichier = fopen("/dev/ttyACM0","w");
59  if(fichier==NULL){
60  printf("Erreur ouverture fichier\n");
61  perror("fopen failed for /dev/ttyACM0" );
62  exit( EXIT_FAILURE );
63  }
64 
65  //Ecriture angles
66  fprintf(fichier,"%d\n",(int)angle[0]);
67  fprintf(fichier,"%d\n",(int)angle[1]);
68 
69  //Fermeture
70  fclose(fichier);
71  return;
72 }
73 
74 int image_CV2SFML(IplImage* imcv, sf::Image imFlux){
75 
76  int R, G, B;
77  int w = imcv->widthStep;
78  char* ptr = imcv->imageData;
79 
80  imFlux.create(imcv->width,imcv->height, NULL); //Initialise une image vide
81 
82  for( int y=0; y<imcv->height; y++ ) {
83  //uchar* ptr = (uchar*) ( imcv->imageData + y * imcv->widthStep );
84  for( int x=0; x<imcv->width; x++ ) {
85  //Recupération du pixel
86  B = ptr[y*w + 3*x];
87  G = ptr[y*w + 3*x + 1];
88  R = ptr[y*w + 3*x + 2];
89 
90  //Ecriture du pixel associé
91  imFlux.setPixel(x,y,sf::Color(R,G,B,1)); //Alpha channel = 1
92  }
93  }
94 
95  return 0;
96 }
97 
98 
99 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
100 
101  // Covert color space to HSV as it is much easier to filter colors in the HSV color-space.
102  cvCvtColor(frame, HSV, CV_BGR2HSV);
103 
104  //Blur
105  cvSmooth( HSV, HSV, CV_GAUSSIAN, 15, 0,0,0); //suppression des parasites par flou gaussien
106 
107  //Binarisation
108 
109  CvScalar valinf={(double)LowH,(double)LowS,(double)LowV};
110  CvScalar valsup={(double)HighH,(double)HighS,(double)HighV};
111 
112  cvInRangeS(HSV, valinf,valsup, Binaire);
113 
114  //En cas d'erreur sur les trois ligne précédentes
115  //cvInRangeS(HSV, CvScalar(LowH,LowS,LowV),CvScalar(HighH,HighS,HighV), Binaire);
116 
117  //cvSmooth( Binaire, Binaire, CV_GAUSSIAN, 9, 9 ); //Legère suppression des parasites
118 }
119 
120 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
121 
122  CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
123 
124  cvMoments(Binaire, moments, 1);
125  // The actual moment values
126  double moment10 = cvGetSpatialMoment(moments, 1, 0);
127  double moment01 = cvGetSpatialMoment(moments, 0, 1);
128  double area = cvGetCentralMoment(moments, 0, 0);
129 
130  *posX = moment10/area;
131  *posY = moment01/area;
132 
133  free(moments);
134 }
135 
136 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
137 
138  cvNamedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"
139 
140  //Create trackbars in "Control" window
141  cvCreateTrackbar("LowH", "Control", LowH, 179,NULL); //Hue (0 - 179)
142  cvCreateTrackbar("HighH", "Control", HighH, 179,NULL);
143 
144  cvCreateTrackbar("LowS", "Control", LowS, 255,NULL); //Saturation (0 - 255)
145  cvCreateTrackbar("HighS", "Control", HighS, 255,NULL);
146 
147  cvCreateTrackbar("LowV", "Control", LowV, 255,NULL); //Value (0 - 255)
148  cvCreateTrackbar("HighV", "Control", HighV, 255,NULL);
149 }
150 
151 void affichage_config(IplImage* frame, IplImage* HSV, IplImage* Binaire){ //Affiche le flux vidéos et ses différent traitements
152 
153  // Create a window in which the captured images will be presented
154  cvNamedWindow( "HSV", CV_WINDOW_AUTOSIZE );
155  cvNamedWindow( "Binaire", CV_WINDOW_AUTOSIZE );
156  cvNamedWindow( "Camera", CV_WINDOW_AUTOSIZE );
157 
158  cvShowImage( "HSV", HSV); // Original stream in the HSV color space
159  cvShowImage( "Binaire", Binaire); // The stream after color filtering
160  cvShowImage( "Camera", frame ); // Flux caméra avec tracking objet
161 }
162 
163 void Affichage_Tracking(IplImage* frame, int posX, int posY, int width, int height){ //Dessine les informations de tracking sur frame
164 
165  //Affichage zone suivie objet
166  cvCircle(frame, cvPoint(width/2,height/2), height*JEU, CV_RGB(0, 255, 0), 4, 8, 0 );
167 
168  if(posX<5&&posY<5){ //Si aucun objet spotted, pointeur rouge au centre
169  posX=width/2;
170  posY=height/2;
171  cvLine(frame, cvPoint(posX-20,posY), cvPoint(posX+20,posY), CV_RGB(255, 0, 0), 4, 8, 0 );
172  cvLine(frame, cvPoint(posX,posY-20), cvPoint(posX,posY+20), CV_RGB(255, 0, 0), 4, 8, 0 );
173  }
174  else{ //Objet spotted
175  //Affichage position de l'objet
176  cvLine(frame, cvPoint(posX-20,posY), cvPoint(posX+20,posY), CV_RGB(0, 0, 255), 4, 8, 0 );
177  cvLine(frame, cvPoint(posX,posY-20), cvPoint(posX,posY+20), CV_RGB(0, 0, 255), 4, 8, 0 );
178  }
179 }
180 
181 
182 CvHaarClassifierCascade* init_cascade(){
183  // Create a new Haar classifier
184  CvHaarClassifierCascade* cascade = 0;
185  const char* cascade_name = "haarcascade_frontalface_alt.xml";
186 
187 
188  cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
189 
190  // Check whether the cascade has loaded successfully. Else report and error and quit
191  if( !cascade ){
192  fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
193  perror(" ");
194  return NULL;
195  }
196  return cascade;
197 }
198 
199 // Function to detect and draw any faces that is present in an image
200 void detect_and_draw( IplImage* img, CvHaarClassifierCascade* cascade, face** tab_face)
201 {
202 
203  // Create memory for calculations
204  static CvMemStorage* storage = 0;
205  int scale = 1;
206 
207  // Create a new image based on the input image
208  IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
209 
210  // Create two points to represent the face locations
211  CvPoint pt1, pt2;
212  int i;
213 
214  // Allocate the memory storage
215  storage = cvCreateMemStorage(0);
216 
217  // Create a new named window with title: result
218  cvNamedWindow( "result", 1 );
219 
220  // Clear the memory storage which was used before
221  cvClearMemStorage( storage );
222 
223  // Find whether the cascade is loaded, to find the faces. If yes, then:
224  if( cascade ){
225 
226  // There can be more than one face in an image. So create a growable sequence of faces.
227  // Detect the objects and store them in the sequence
228  CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,1.1, 2, 0, cvSize(60, 60),cvSize(500, 500));
229 
230  //Reset faces
231  tab_face[0]->largeur = 0;
232  tab_face[1]->largeur = 0;
233 
234  // Loop the number of faces found.
235  for( i = 0; i < (faces ? faces->total : 0); i++ ){
236  // Create a new rectangle for drawing the face
237  CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
238 
239  // Find the dimensions of the face,and scale it if necessary
240  pt1.x = r->x*scale;
241  pt2.x = (r->x+r->width)*scale;
242  pt1.y = r->y*scale;
243  pt2.y = (r->y+r->height)*scale;
244 
245  // Draw the rectangle in the input image
246  cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
247 
248  if(i < MAX_FACE){
249  tab_face[i]->point.x = (pt1.x + pt2.x)/2;
250  tab_face[i]->point.y = (pt1.y + pt2.y)/2;
251  tab_face[i]->largeur = r->width;
252 
253  printf("VALEURS FACES n°%d : %d %d %d\n",i, tab_face[i]->point.x,tab_face[i]->point.y,tab_face[i]->largeur);
254  }
255  }
256  }
257 
258  // Show the image in the window named "result"
259  cvShowImage( "result", img );
260 
261  //free
262  cvReleaseImage( &temp );
263 }
264 
265 //Renvoie la couleur moyenne de rec_face
266 void get_color(IplImage* image, face* rec_face, int* BGR){
267  printf("z\n");
268  CvScalar colors;
269  int largeur = rec_face->largeur;
270 printf("e\n");
271  cvSetImageROI(image,cvRect(rec_face->point.x -largeur/2, rec_face->point.y -largeur/2, largeur,largeur));
272 printf("r\n");
273  colors = cvAvg(image);
274 printf("t\n");
275  cvResetImageROI(image);
276 printf("y\n");
277  BGR[0] = colors.val[0];
278  BGR[1] = colors.val[1];
279  BGR[2] = colors.val[2];
280 printf("u\n");
281 }
282 
283 
284 
285 
286 
287 
288 
289 
290 
291 
292 
293 
294 
295 
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 detect_and_draw(IplImage *img, CvHaarClassifierCascade *cascade, face **tab_face)
Détecte et renvoie un rectangle pour chaque visage sur l'image.
Definition: fonction.c:200
CvHaarClassifierCascade * init_cascade()
Charge les fichiers cascades pour la reconnaissance faciale.
Definition: fonction.c:182
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.
#define MAX_FACE
Nombre maximum de faces traitées.
Definition: fonction.h:55
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
int limite_moteur(int val_pwm)
Fonction qui vérifie que les valeurs envoyees aux moteurs sont correctes.
Definition: fonction.c:41
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
int image_CV2SFML(IplImage *imcv, sf::Image imFlux)
Convertit une image opencv (IplImage) en une image SFML (sf::Image)
Definition: fonction.c:74
Contient les informations sur chaque face détectée : positions, largeur.