00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <qvdta/qvcontour.h>
00026
00027 namespace qvdta
00028 {
00029
00030
00031
00032
00033
00034
00035 const char coorX[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
00036 const char coorY[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
00037
00038
00039 QVPolyline getContourThresholdFromBorderPoint(const QVImage<uChar> &image, const int startPointX, const int startPointY, const uChar threshold)
00040 {
00041
00042 QVPolyline lista;
00043
00044
00045 lista.loop = true;
00046 lista.append(QPoint(startPointX, startPointY));
00047
00048 QVIMAGE_INIT_READ(uChar,image);
00049 QRect roi = image.getROI();
00050
00051
00052 Q_ASSERT_X(roi.contains(startPointX, startPointY), "getContourThresholdFromBorderPoint", "start point out of image ROI");
00053 Q_ASSERT_X(QVIMAGE_PIXEL(image, startPointX, startPointY, 0) >= threshold, "getContourThresholdFromBorderPoint", "start point is not contained in a connected set");
00054
00055
00056
00057
00058
00059 uChar searchDir = 128, numOuterPixels = 0;
00060 for (int i = 0; i<8; i++)
00061 {
00062 int x = startPointX +coorX[i], y = startPointY +coorY[i];
00063 if (!roi.contains(x, y))
00064 {
00065 numOuterPixels++;
00066 searchDir = i;
00067 }
00068 else if (QVIMAGE_PIXEL(image, x, y,0) < threshold)
00069 {
00070 numOuterPixels++;
00071 searchDir = i;
00072 }
00073 }
00074
00075
00076 Q_ASSERT_X(searchDir < 8, "getContourThresholdFromBorderPoint", "start point is inside the set, not in the border");
00077
00078
00079 if (numOuterPixels == 8)
00080 return lista;
00081
00082
00083
00084
00085 int sumSearchDir = 0, actualPointX = startPointX, actualPointY = startPointY;
00086 while (true)
00087 {
00088
00089 uChar d;
00090 int nextPointX, nextPointY;
00091 for (d = 0; d < 8; d++)
00092 {
00093 searchDir = (searchDir+1)%8;
00094 nextPointX = actualPointX + coorX[searchDir];
00095 nextPointY = actualPointY + coorY[searchDir];
00096 if (roi.contains(nextPointX, nextPointY))
00097 if ( (QVIMAGE_PIXEL(image, nextPointX, nextPointY,0) >= threshold) )
00098 break;
00099 }
00100
00101 sumSearchDir += d - 3;
00102
00103 actualPointX = nextPointX;
00104 actualPointY = nextPointY;
00105
00106 if ( QVIMAGE_PIXEL(image, actualPointX, actualPointY,0) < threshold )
00107 break;
00108
00109 if ( startPointX == actualPointX && startPointY == actualPointY)
00110 break;
00111
00112 lista.append(QPoint(actualPointX, actualPointY));
00113
00114 searchDir = searchDir + 4;
00115 }
00116
00117
00118
00119 lista.direction = (sumSearchDir >= 0);
00120 lista.dir = sumSearchDir;
00121
00122
00123 return lista;
00124 }
00125
00126 QVPolyline getContourThreshold(const QVImage<uChar> &image, const QPoint startPoint, const uChar threshold)
00127 {
00128
00129
00130
00131
00132 int startPointX = startPoint.x(), startPointY = startPoint.y();
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 return getContourThresholdFromBorderPoint(image, startPointX, startPointY, threshold);
00156 }
00157
00158 QList<QVPolyline> getContoursThreshold(const QVImage <uChar> &image, const uChar threshold)
00159 {
00160
00161 qDebug() << "getPolylinesThreshold()";
00162 QVImage<uChar> mask(image.getCols()+1, image.getRows()+1);
00163 qvipp::Set(mask,0);
00164
00165 QVIMAGE_INIT_READ(uChar,image);
00166 QVIMAGE_INIT_WRITE(uChar,mask);
00167
00168 QRect roi = image.getROI();
00169
00170 QList<QVPolyline> polylineList;
00171
00172
00173 for (int row = roi.y(); row < roi.y() + roi.height(); row++)
00174 for (int col = roi.x(); col < roi.y() + roi.width(); col++)
00175 {
00176
00177 if (QVIMAGE_PIXEL(image, col, row,0) >= threshold)
00178 {
00179
00180
00181
00182 if ( !QVIMAGE_PIXEL(mask, col, row,0) )
00183 {
00184
00185 QVPolyline lista = getContourThresholdFromBorderPoint(image, col, row, threshold);
00186
00187 draw(mask, lista, true, true);
00188
00189 polylineList.append(lista);
00190
00191 }
00192
00193
00194
00195 while (roi.contains(col+1, row))
00196 {
00197 if ( QVIMAGE_PIXEL(image, col+1, row,0) < threshold )
00198 break;
00199 col++;
00200 }
00201
00202
00203
00204 if ( !QVIMAGE_PIXEL(mask, col, row,0) )
00205 {
00206 QVPolyline lista = getContourThresholdFromBorderPoint(image, col, row, threshold);
00207 draw(mask, lista, true, true);
00208 polylineList.append(lista);
00209 }
00210
00211 }
00212
00213 }
00214
00215 qDebug() << "getPolylinesThreshold():"<< polylineList.size() << "contours obtained";
00216 qDebug() << "getPolylinesThreshold() <~ return";
00217 return polylineList;
00218 }
00219 }