Salut,
J'ai un peu galèré sous OpenCv. Cela fait 15jours que je bosse dessus. Je me suis rendu compte qu'un certain nombre de personne posait des questions concernant les webcams et le traitement des images. Voici le petit bout de code auquel je suis parvenu apres de multiples essais. Un bonne base me semble-t-il pour débuter et ECHANGER. Source a compiler en C++ ( sinon, il faut remplacer le (void *) du callback par un &
J'ai aussi optimisé la routine d'acces au pixel de la doc officielle, mais je suis loin d'être un "gourou" du C/C++. J'èspère que ceci sera amélioré par d'autres.
Jean-Pierre
ps: Il y a un test de vitesse et un affichage du nombre de frames. Utile pour l'optimisation. La résolution de la webcam est de 320*240
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 #include <cv.h> #include <highgui.h> #include <stdio.h> #include <cvcam.h> // int frame=0; uchar* pixel_ptr; void callback(IplImage* img) { int x,y; int iwd=img->widthStep; char *idt=img->imageData; double t1=cvGetTickCount( ); frame++; for (x=0;x<320;x++) { for (y=0;y<240;y++) { pixel_ptr = &((uchar*)(idt + iwd*y))[x*3]; pixel_ptr[0] = 0; // Pixel Bleu //pixel_ptr[1] = 0; // Pixel Vert //pixel_ptr[2] = 0; // Pixel Rouge //if (pixel_ptr[0] >100) pixel_ptr[0] = 0 ; // Petit traitement ;-) } } printf("Temps 1 %d %f \n",frame,cvGetTickCount( )-t1); } int main( int argc, char** argv ) { int ncams = cvcamGetCamerasCount( ); // 0 -> Première Webcam ou perih de capture trouvé cvcamSetProperty(0, CVCAM_PROP_ENABLE , CVCAMTRUE); cvcamSetProperty(0, CVCAM_PROP_CALLBACK ,(void *)callback); if( !cvcamInit() ) return 0; cvcamStart(); cvWaitKey(0); // Un Wait ici. Le traitement est effectué en tache de fond cvcamStop(); cvcamExit(); }
Edit : Deuxième version. Beaucoup plus rapide !!! 2,5 fois.
Edit 2 : Semble plus rapide pour un acces ligne par ligne ???.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 // #include <cv.h> #include <highgui.h> #include <stdio.h> #include <cvcam.h> // int frame=0; // void callback(IplImage* img) { int i,j,k,x,y; int iwd=img->widthStep; char *idt=img->imageData; int channels = img->nChannels; double t1=cvGetTickCount( ); frame++; for (x=0;x<320;x++) { for (y=0;y<240;y++) { idt[y*iwd+x*channels]=0; // Pixel Bleu //idt[y*iwd+x*channels+1]=0; // Pixel Vert //idt[y*iwd+x*channels+2]=0; // Pixel Rouge } } printf("Temps 1 %d %f \n",frame,cvGetTickCount( )-t1); } int main( int argc, char** argv ) { int ncams = cvcamGetCamerasCount( ); cvcamSetProperty(0, CVCAM_PROP_ENABLE , CVCAMTRUE); cvcamSetProperty(0, CVCAM_PROP_CALLBACK ,(void *)callback); if( !cvcamInit() ) return 0; cvcamStart(); cvWaitKey(0); cvcamStop(); cvcamExit(); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 // OPTIMISEE POUR UN ACCESS LIGNE PAR LIGNE #include <cv.h> #include <highgui.h> #include <stdio.h> #include <cvcam.h> // int frame=0; uchar* pixel_ptr; void callback(IplImage* img) { int channels = img->nChannels; int x,y,i,j; int ihei=img->height; int iwd=img->widthStep; double t1=cvGetTickCount( ); frame++; for(i=0; i<ihei; i+=10) { for(j=(iwd)*i; j<(iwd)*(i+1); j+=channels) { img->imageData[j] = 255; //img->imageData[j+1] = 0; //img->imageData[j+2] = 0; } } printf("Temps 1 %d %f \n",frame,cvGetTickCount( )-t1); } int main( int argc, char** argv ) { int ncams = cvcamGetCamerasCount( ); cvcamSetProperty(0, CVCAM_PROP_ENABLE , CVCAMTRUE); cvcamSetProperty(0, CVCAM_PROP_CALLBACK ,(void *)callback); if( !cvcamInit() ) return 0; cvcamStart(); cvWaitKey(0); cvcamStop(); cvcamExit(); }
Partager