00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <qgarlib/CannyGradientImage.H>
00043 #include <qgarlib/Component.H>
00044 #include <qgarlib/ConnectedComponents.H>
00045 #include <qgarlib/ErodedBinaryImage.H>
00046 #include <qgarlib/GenImage.H>
00047 #include <qgarlib/GradientModuleImage.H>
00048 #include <qgarlib/NiblackBinaryImage.H>
00049 #include <qgarlib/StandardDeviationImage.H>
00050
00051
00052
00053 namespace qgar
00054 {
00055
00056
00057
00058
00059
00060 NiblackBinaryImage::NiblackBinaryImage(const GreyLevelImage& anImg,
00061 const int aLowThres,
00062 const int aHighThres,
00063 const int aMaskSize,
00064 const float aK,
00065 const float aPostThres)
00066
00067 : BinaryImage(anImg.width(), anImg.height())
00068
00069 {
00070
00071 FloatImage* pMeanImg = 0;
00072
00073
00074 StandardDeviationImage stdImg(FloatImage(anImg), pMeanImg, aMaskSize);
00075
00076
00077 GreyLevelImage::value_type* bRow = new GreyLevelImage::value_type [_width];
00078 GreyLevelImage::value_type* gRow = new GreyLevelImage::value_type [_width];
00079
00080 float* mRow = new float [_width];
00081 float* sRow = new float [_width];
00082
00083
00084 for (int i = 0 ; i < _height ; ++i)
00085 {
00086
00087 pMeanImg->row(i, mRow);
00088
00089 stdImg.row(i, sRow);
00090
00091 anImg.row(i, gRow);
00092
00093
00094 float* m = mRow;
00095 float* s = sRow;
00096 GreyLevelImage::value_type* g = gRow;
00097 GreyLevelImage::value_type* b = bRow;
00098
00099
00100 for (int j = 0 ; j < _width ; ++j, ++b, ++g)
00101 {
00102 if (*g < aLowThres)
00103 {
00104 *b = 1;
00105 }
00106 else if (*g > aHighThres)
00107 {
00108 *b = 0;
00109 }
00110 else if (*g < (GreyLevelImage::value_type) (*m++ + aK * *s++))
00111 {
00112 *b = 1;
00113 }
00114 else
00115 {
00116 *b = 0;
00117 }
00118 }
00119
00120
00121 setRow(i, bRow);
00122 }
00123
00124
00125 if (aPostThres > 0)
00126 {
00127
00128 BinaryImage* contours = new BinaryImage(*this);
00129
00130
00131 ConnectedComponents* compConnexes = new ConnectedComponents(*contours);
00132
00133
00134 int labCnt = compConnexes->componentCnt();
00135
00136 float* gsum = new float [labCnt];
00137 qgFill(gsum, labCnt, 0.f);
00138
00139 int* psum = new int [labCnt];
00140 qgFill(psum, labCnt, 0);
00141
00142
00143 Component::label_type* labRow = new Component::label_type[_width];
00144
00145 labRow[1] = 0;
00146 labRow[_width - 1] = 0;
00147
00148
00149 CannyGradientImage* gradImg = new CannyGradientImage(anImg);
00150 GradientModuleImage gradModImg(*gradImg);
00151 delete gradImg;
00152
00153
00154
00155 ErodedBinaryImage* eroImg = new ErodedBinaryImage(*contours);
00156 (*contours) -= (*eroImg);
00157 delete eroImg;
00158
00159
00160 float* fRow = new float [_width];
00161
00162
00163 Component::label_type* pMapCCImg =
00164 (compConnexes->accessComponentImage()).pPixMap() + _width;
00165
00166 for (int i = 1 ; i < (_height - 1) ; ++i)
00167 {
00168
00169
00170 pMapCCImg += 2;
00171 PRIVATEgetBlackLabels(pMapCCImg, labRow);
00172
00173
00174 contours->row(i, bRow);
00175
00176
00177 gradModImg.row(i, fRow);
00178 Component::label_type* p = labRow;
00179 GreyLevelImage::value_type* q = bRow;
00180 float* r = fRow;
00181 for (int j = 0 ; j < _width ; ++j, ++p, ++q, ++r)
00182 {
00183 if (*q != 0)
00184 {
00185 gsum[(int)(*p)] += *r;
00186 psum[(int)(*p)] += 1;
00187 }
00188 }
00189 }
00190
00191 delete contours;
00192
00193
00194 for (int i = 0 ; i < labCnt ; ++i)
00195 {
00196 if (psum[i] != 0)
00197 {
00198 gsum[i] /= psum[i];
00199 }
00200 }
00201
00202
00203 pMapCCImg = (compConnexes->accessComponentImage()).pPixMap() + _width;
00204
00205 for (int i = 1 ; i < _height - 1 ; ++i)
00206 {
00207
00208 pMapCCImg += 2;
00209 PRIVATEgetBlackLabels(pMapCCImg, labRow);
00210
00211
00212 row(i, bRow);
00213
00214
00215 Component::label_type* p = labRow;
00216 GreyLevelImage::value_type* pb = bRow;
00217 for (int j = 0 ; j < _width ; ++j, ++p, ++pb)
00218 {
00219 if (((*pb) != 0) && (gsum[(int)(*p)] < aPostThres))
00220 {
00221 *pb = 0;
00222 }
00223 }
00224
00225
00226 setRow(i, bRow);
00227 }
00228
00229
00230 delete [] fRow;
00231 delete [] psum;
00232 delete [] gsum;
00233 delete compConnexes;
00234 }
00235
00236
00237 delete [] bRow;
00238 delete [] gRow;
00239 delete [] mRow;
00240 delete [] sRow;
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250 void
00251 NiblackBinaryImage::PRIVATEgetBlackLabels(Component::label_type* aPMapCCImg,
00252 Component::label_type* aBuffer)
00253 {
00254
00255
00256
00257 Component::label_type prevLabel = 0;
00258 bool currColorIsWhite = true;
00259
00260 for (int idx = 1 ; idx < (_width - 2) ; ++idx, ++aPMapCCImg)
00261 {
00262 Component::label_type currLabel = *aPMapCCImg;
00263
00264 if (currLabel != prevLabel)
00265 {
00266 currColorIsWhite = !currColorIsWhite;
00267 prevLabel = currLabel;
00268 }
00269
00270 if (currColorIsWhite)
00271 {
00272 aBuffer[idx] = 0;
00273 }
00274 else
00275 {
00276 aBuffer[idx] = currLabel;
00277 }
00278 }
00279 }
00280
00281
00282
00283 }