Kuwahara.cpp
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 |
|
24 |
bool CImage::Kuwahara2(int NbPixel,CImage *ImgDest) |
25 |
{ |
26 |
if(hBmp==0){ |
27 |
MessageBox(NULL," Kuwahara : L'image source est vide", |
28 |
NULL,MB_OK|MB_ICONWARNING); |
29 |
return 0; |
30 |
} |
31 |
if((NbPixel & 1) && NbPixel<3 && NbPixel>16){ |
32 |
MessageBox(NULL," Kuwahara : NbPixel doit etre impaire et compris entre 3 et 15", |
33 |
NULL,MB_OK|MB_ICONWARNING); |
34 |
return 0; |
35 |
} |
36 |
|
37 |
if(ImgType == RGBi) |
38 |
ConvertRGB2Gray(); |
39 |
|
40 |
if(ImgDest!=0 && ImgDest!=this) |
41 |
ImgDest->Copy(this); |
42 |
|
43 |
if(ImgDest==0) |
44 |
ImgDest=this; |
45 |
|
46 |
double *Kuwa=new double[Width*Height]; |
47 |
|
48 |
|
49 |
|
50 |
|
51 |
//recupération des pixels |
52 |
GetBitmapBits(hBmp,Width*Height*4,ucBits); |
53 |
|
54 |
double Std_Zone[4],Mean_Zone[4]; |
55 |
|
56 |
double MinStd; |
57 |
|
58 |
RECT Zone[4]; |
59 |
|
60 |
int i,j,k,r,q,ZoneChosen,NbPixTot=(NbPixel/2+1)*(NbPixel/2+1); |
61 |
|
62 |
//Pour tous les pixels a l'exception des pixels du bord |
63 |
for(j=NbPixel/2;j<Height-NbPixel/2;j++) |
64 |
{ |
65 |
for(i=NbPixel/2;i<Width-NbPixel/2;i++) |
66 |
{ |
67 |
|
68 |
Zone[0].left=i-NbPixel/2; |
69 |
Zone[0].right=i; |
70 |
Zone[0].top=j-NbPixel/2; |
71 |
Zone[0].bottom=j; |
72 |
|
73 |
Zone[1].left=i; |
74 |
Zone[1].right=i+NbPixel/2; |
75 |
Zone[1].top=j-NbPixel/2; |
76 |
Zone[1].bottom=j; |
77 |
|
78 |
Zone[2].left=i-NbPixel/2; |
79 |
Zone[2].right=i; |
80 |
Zone[2].top=j; |
81 |
Zone[2].bottom=j+NbPixel/2; |
82 |
|
83 |
Zone[3].left=i; |
84 |
Zone[3].right=i+NbPixel/2; |
85 |
Zone[3].top=j; |
86 |
Zone[3].bottom=j+NbPixel/2; |
87 |
|
88 |
//Calcul de la valeur Moyenne pour les 4 zones |
89 |
for(k=0;k<4;k++) |
90 |
{ |
91 |
//Initialisation |
92 |
Mean_Zone[k]=0; |
93 |
|
94 |
|
95 |
//Pour tous les pixels de la zone |
96 |
for(q=Zone[k].left;q<=Zone[k].right;q++) |
97 |
for(r=Zone[k].top;r<=Zone[k].bottom;r++) |
98 |
Mean_Zone[k]+=ucBits[(q+r*Width)*4]; |
99 |
|
100 |
Mean_Zone[k]/=(double)NbPixTot; |
101 |
} |
102 |
|
103 |
//Calcul de la valeur de l'ecart type pour les 4 zones |
104 |
MinStd=100000000; |
105 |
for(k=0;k<4;k++) |
106 |
{ |
107 |
//Initialisation |
108 |
Std_Zone[k]=0; |
109 |
|
110 |
|
111 |
//Pour tous les pixels de la zone |
112 |
for(q=Zone[k].left;q<=Zone[k].right;q++) |
113 |
for(r=Zone[k].top;r<=Zone[k].bottom;r++) |
114 |
Std_Zone[k]+=((ucBits[(q+r*Width)*4] - Mean_Zone[k])*(ucBits[(q+r*Width)*4] - Mean_Zone[k])); |
115 |
|
116 |
Std_Zone[k]/=(double)NbPixTot; |
117 |
if(Std_Zone[k] < MinStd) { |
118 |
MinStd=Std_Zone[k]; |
119 |
ZoneChosen = k; |
120 |
} |
121 |
} |
122 |
|
123 |
//Affectation de la valeur du pixel central |
124 |
Kuwa[i+j*Width]=Mean_Zone[ZoneChosen]; |
125 |
|
126 |
} |
127 |
} |
128 |
int Pos; |
129 |
|
130 |
|
131 |
ImgDest->ImgType=GRAY; |
132 |
for(j=NbPixel/2;j<Height-NbPixel/2;j++) { |
133 |
for(i=NbPixel/2;i<Width-NbPixel/2;i++) |
134 |
{ |
135 |
|
136 |
k=(i+j*Width); |
137 |
Pos=4*k; |
138 |
|
139 |
ImgDest->ucBits[Pos]=Limit((int)Kuwa[k]); |
140 |
ImgDest->ucBits[Pos+1]=ImgDest->ucBits[Pos]; |
141 |
ImgDest->ucBits[Pos+2]=ImgDest->ucBits[Pos]; |
142 |
} |
143 |
} |
144 |
|
145 |
delete []Kuwa; |
146 |
|
147 |
SetBitmapBits (ImgDest->hBmp,(Width*Height)*4,ImgDest->ucBits); |
148 |
return 1; |
149 |
|
150 |
} |
lien1 lien2 lien3