ScriptCercle.cpp (Détection des cercles Hough)

1  /***********************************************************************************  
2      ImAnalyse : software in image processing and image analysis 
3     
4      Copyright (C) 27 avril 2008  <Vincent MORARD> 
5    Version: 2.0 
6      Contact: vincent<POINT>morard<AROBAS>cpe<POINT>fr 
7    Website: http://pistol.petesampras.free.fr 
8   
9      This program is free software: you can redistribute it and/or modify 
10      it under the terms of the GNU General Public License as published by 
11      the Free Software Foundation, either version 3 of the License, or 
12      (at your option) any later version. 
13   
14      This program is distributed in the hope that it will be useful, 
15      but WITHOUT ANY WARRANTY; without even the implied warranty of 
16      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
17      GNU General Public License for more details. 
18   
19      You should have received a copy of the GNU General Public License 
20      along with this program.  If not, see <http://www.gnu.org/licenses/ 
21  **********************************************************************************/ 
22  #include "CImage.h" 
23  #include "DlgCallback.h" 
24  void Message3(HWND hList,HWND hDlg,char *Mes,int Buf) 
25  { 
26    SendMessage(hList,LB_ADDSTRING, 0, (LPARAM)Mes); 
27    SendMessage(hDlg,WM_UPDATE,Buf,0); 
28    Sleep(0); 
29  } 
30  //************************************************************************************ 
31  //Thread Cercle1 Detections des cercles de l'image sans avoir d'informations sur les rayons  
32  //des cercles 
33  //************************************************************************************ 
34  DWORD WINAPI DetectionCercle(LPVOID lpParam ) 
35  { 
36    HWND hDlg,hOK,hwndList; 
37    int R,X,Y; 
38    Param *P; 
39    P=(Param*)lpParam; 
40    hDlg=(HWND) P->hwnd; 
41    int RayonMin=P->Data1; 
42    int RayonMax=P->Data2; 
43   
44    hwndList=GetDlgItem(hDlg, IDC_LIST1); 
45    hOK=GetDlgItem(hDlg, IDOK); 
46   
47    int t = GetTickCount();  //initialisation du temps 
48   
49    SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Début du traitement"); 
50   
51    char szExe[_MAX_PATH],*Q=szExe; 
52    GetModuleFileName(NULL,szExe,_MAX_PATH); 
53    while(*Q!=0)Q++; 
54    while(*Q!='\\')Q--;*Q=0; 
55    strcat(szExe,"\\monnaie.jpg"); 
56    if(Img[0].LoadJPG_GIF(szExe)==0)return 0; 
57   
58    Message3(hwndList,hDlg,"Ouverture de l'image : Fait",0); 
59   
60    if(Img[0].ConvertRGB2Gray()==0)return 0; 
61   
62    if(Img[0].BruitUniforme(30,&Img[1])==0)return 0; 
63    Message3(hwndList,hDlg,"Ajout d'un bruit uniforme : Fait",1); 
64   
65    if(Img[1].Sobel(GRAY,&Img[2])==0)return 0; 
66    Message3(hwndList,hDlg,"Sobel : Fait",2); 
67   
68    if(Img[2].Threashold(250,&Img[3])==0)return 0; 
69    Message3(hwndList,hDlg,"Seuillage : Fait",3); 
70   
71    if(Img[3].HoughCircleCompute(RayonMin,RayonMax,&Img[4],&X,&Y,&R)==0)return 0; 
72   
73    if(Img[4].HoughContrasteAuto()==0)return 0; 
74    Message3(hwndList,hDlg,"Houg: Détection de cercle : Fait",4); 
75   
76    if(Img[0].DrawCircle(R,X+1,Y+1,255,0,0)==0)return 0; 
77    Message3(hwndList,hDlg,"Dessin du cercle trouvé : Fait",0); 
78   
79   
80   
81   
82   
83    SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Fin du traitement, temps (ms) "); 
84    t = GetTickCount() - t; 
85    char Buf[20]; 
86    strcpy(Buf,itoa(t,Buf,10)); 
87    SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)Buf); 
88   
89    EnableWindow(hOK,TRUE); 
90    return 1; 
91   
92  } 
93   
94   
95  //************************************************************************************ 
96  //Thread cercle2 : Teste de la robustesse aux bruits de la détection de cercle dans une image 
97  //************************************************************************************ 
98  DWORD WINAPI DetectionCercle2(LPVOID lpParam ) 
99  { 
100    HWND hDlg,hOK,hwndList; 
101    int R,X,Y; 
102    Param *P; 
103    P=(Param*)lpParam; 
104    hDlg=(HWND) P->hwnd; 
105    int Bruit=P->Data1; 
106   
107    hwndList=GetDlgItem(hDlg, IDC_LIST1); 
108    hOK=GetDlgItem(hDlg, IDOK); 
109   
110    int t = GetTickCount();  //initialisation du temps 
111   
112    SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Début du traitement"); 
113   
114    char szExe[_MAX_PATH],*Q=szExe; 
115    GetModuleFileName(NULL,szExe,_MAX_PATH); 
116    while(*Q!=0)Q++; 
117    while(*Q!='\\')Q--;*Q=0; 
118    strcat(szExe,"\\Cercle.jpg"); 
119    if(Img[0].LoadJPG_GIF(szExe)==0)return 0; 
120   
121    Message3(hwndList,hDlg,"Ouverture de l'image : Fait",0); 
122   
123    if(Img[0].ConvertRGB2Gray()==0)return 0; 
124    if(Img[0].BruitUniforme(Bruit,&Img[1])==0)return 0; 
125    Message3(hwndList,hDlg,"Ajout du bruit : Fait",1); 
126   
127    if(Img[1].Sobel(GRAY,&Img[2])==0)return 0; 
128    Message3(hwndList,hDlg,"Sobel : Fait",2); 
129   
130    if(Img[2].Threashold(250,&Img[3])==0)return 0; 
131    Message3(hwndList,hDlg,"Seuillage : Fait",3); 
132   
133    if(Img[3].HoughCircleCompute(40,40,&Img[4],&X,&Y,&R)==0)return 0; 
134    if(Img[4].HoughContrasteAuto()==0)return 0; 
135    Message3(hwndList,hDlg,"Hough: detection de cerlce : Fait",4); 
136   
137    if(Img[1].DrawCircle(R,X+1,Y+1,255,0,0)==0)return 0; 
138    Message3(hwndList,hDlg,"Dessin du cercle trouvé : Fait",1); 
139   
140   
141   
142    SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Fin du traitement, temps (ms) "); 
143    t = GetTickCount() - t; 
144    char Buf[20]; 
145    strcpy(Buf,itoa(t,Buf,10)); 
146    SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)Buf); 
147   
148    EnableWindow(hOK,TRUE); 
149    return 1; 
150   
151  } 
152   
153  //************************************************************************************ 
154  //Thread Cercle3 : Détection de cercle sur l'image d'un oeil.  
155  //Le but de ce traitement est de détecter le contour de la pupile ainsi que le contour de l'iris 
156  //Le but final étant de trouver le centre de l'oeil et les rayon des cercles associés. 
157  //************************************************************************************ 
158  DWORD WINAPI DetectionCercle3(LPVOID lpParam ) 
159  { 
160    HWND hDlg,hOK,hwndList; 
161    int R,X,Y; 
162   
163    hDlg=(HWND) lpParam; 
164    hwndList=GetDlgItem(hDlg, IDC_LIST1); 
165    hOK=GetDlgItem(hDlg, IDOK); 
166   
167    int t = GetTickCount();  //initialisation du temps 
168   
169    SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Début du traitement"); 
170   
171    char szExe[_MAX_PATH],*Q=szExe; 
172    GetModuleFileName(NULL,szExe,_MAX_PATH); 
173    while(*Q!=0)Q++; 
174    while(*Q!='\\')Q--;*Q=0; 
175    strcat(szExe,"\\Hough.jpg"); 
176    if(Img[0].LoadJPG_GIF(szExe)==0)return 0; 
177   
178   
179    Img[0].ConvertRGB2Gray(); 
180    Message3(hwndList,hDlg,"Ouverture de l'image : Fait",0); 
181   
182    if(Img[0].Sobel(GRAY,&Img[1])==0)return 0; 
183    Message3(hwndList,hDlg,"Sobel : Fait",1); 
184   
185    if(Img[1].Threashold(254,&Img[2])==0)return 0; 
186    Message3(hwndList,hDlg,"Seuillage : Fait",2); 
187   
188    if(Img[2].HoughCircleCompute(36,38,&Img[3],&X,&Y,&R)==0)return 0; 
189    if(Img[3].HoughContrasteAuto()==0)return 0; 
190    Message3(hwndList,hDlg,"Hough: détection de cercle : Fait",3); 
191   
192    if(Img[0].DrawCircle(R,X,Y,255,0,0)==0)return 0; 
193    Message3(hwndList,hDlg,"Dessin du cercle trouvé : Fait",0); 
194   
195    //Fin de la détection de la pupile coord: R, X, Y 
196   
197    if(Img[0].Median(GRAY,9,40,&Img[4])==0)return 0; 
198    Message3(hwndList,hDlg,"Filtre médian : Fait",4); 
199   
200    if(Img[4].Sobel(GRAY,&Img[5])==0)return 0; 
201    Message3(hwndList,hDlg,"Sobel : Fait",5); 
202   
203    if(Img[5].Threashold(20,&Img[6])==0)return 0; 
204    Message3(hwndList,hDlg,"Seuillage: Fait",6); 
205   
206    if(Img[6].HoughCircleCompute(100,100,&Img[7],&X,&Y,&R)==0)return 0; 
207    if(Img[7].HoughContrasteAuto()==0)return 0; 
208    Message3(hwndList,hDlg,"Hough: détection de cercle : Fait",6); 
209   
210    if(Img[0].DrawCircle(R,X+1,Y+1,0,255,0)==0)return 0; 
211    Message3(hwndList,hDlg,"Dessin du cercle trouvé : Fait",0); 
212   
213    //Fin du traitement, le second cercle est de coordonnée R,X,Y 
214   
215    SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Fin du traitement, temps (ms) "); 
216    t = GetTickCount() - t; 
217    char Buf[20]; 
218    strcpy(Buf,itoa(t,Buf,10)); 
219    SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)Buf); 
220   
221    EnableWindow(hOK,TRUE); 
222    return 1; 
223   
224  } 

lien1 lien2 lien3