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