00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <QVMSER>
00026
00027 void getMSERContours(const QVImage<uChar, 1> &image, const QList<QVMSER> &MSERList, QList< QVPolyline > &polylineMSERList)
00028 {
00029 const uInt rows = image.getRows(), cols = image.getCols();
00030
00031 QVImage<uChar> notImage(cols, rows);
00032 for (uInt col = 0; col < cols; col++)
00033 for (uInt row = 0; row < rows; row++)
00034 notImage(col, row) = 255 - image(col, row);
00035
00036 for (uInt msers = 0; msers < MSERList.size(); msers++)
00037 {
00038 QVPolyline polylineMSER = getConnectedSetBorderContourThreshold(notImage,
00039 MSERList.at(msers).seed, 255 - MSERList.at(msers).threshold);
00040 polylineMSERList.append (polylineMSER);
00041 }
00042 }
00043
00044 #define RELATIVE_DISTANCE(X,Y) (ABS((X-Y)/(Y)))
00045 void getMSER(const QVImage<uChar,1> &image, QList<QVMSER> &MSERList, const int delta, const int minArea, const int maxArea, const double diffAreaThreshold)
00046 {
00048
00049 QVComponentTree componentTree(image);
00050
00052
00053 uInt histogram[256];
00054 double q_function[256];
00055
00056 for (uInt node = 0; node < componentTree.getNumNodes(); node++)
00057 {
00058 int firstThres = componentTree.firstThreshold(node), lastThres = componentTree.lastThreshold(node);
00059
00060 if (lastThres - firstThres - 2*delta - 2 < 0)
00061 continue;
00062
00063
00064 for (int threshold = firstThres, lastHistogramValue; threshold <= lastThres; threshold++)
00065 {
00066 uInt area = componentTree.area(node)[threshold];
00067 if(area != 0)
00068 lastHistogramValue = area;
00069 histogram[threshold] = lastHistogramValue;
00070 }
00071
00072
00073 for (int threshold = firstThres + delta; threshold <= lastThres - delta; threshold++)
00074 {
00075 q_function[threshold] = (double)(histogram[threshold + delta] - histogram[threshold - delta]) / histogram[threshold];
00076 }
00077
00078
00079 int lastMSERThreshold = -1, minLastMSERThreshold = 0;
00080 for (int threshold = firstThres + delta + 1; threshold <= lastThres - delta - 1; threshold++)
00081 {
00082 if ( ((int)histogram[threshold] < minArea) || ((int)histogram[threshold] > maxArea) )
00083 continue;
00084
00085 if ( (q_function[threshold + 1] > q_function[threshold]) && (q_function[threshold - 1] > q_function[threshold]) )
00086
00087 {
00088 if (lastMSERThreshold == -1)
00089
00090 {
00091 lastMSERThreshold = threshold;
00092 minLastMSERThreshold = threshold;
00093 }
00094 else
00095 if (RELATIVE_DISTANCE((float)histogram[lastMSERThreshold], (float)histogram[threshold]) < diffAreaThreshold)
00096
00097 {
00098 if (q_function[minLastMSERThreshold] > q_function[threshold])
00099 minLastMSERThreshold = threshold;
00100 }
00101 else
00102 {
00103 MSERList.append(QVMSER(QPoint(componentTree.seedX(node), componentTree.seedY(node)), minLastMSERThreshold));
00104 lastMSERThreshold = threshold;
00105 minLastMSERThreshold = threshold;
00106 }
00107 }
00108 }
00109 if (lastMSERThreshold != -1)
00110 MSERList.append(QVMSER(QPoint(componentTree.seedX(node), componentTree.seedY(node)), minLastMSERThreshold));
00111 }
00112
00113 }
00114