PARP Research Group University of Murcia, Spain


src/qvip/qvimagefeatures/qvmser.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008, 2009. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
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                         // Weird: using the following line produces a core error:
00036                         //notImage(col, row) = 255 - image(col, row);
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         // Prune low areas from image, with component tree
00052         QVComponentTree componentTree(image);
00053 
00055         // Tree transversing
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                 // We obtain histogram
00067                 for (int threshold = firstThres; threshold <= lastThres; threshold++)
00068                                 {
00069                                 uInt area = componentTree.area(node)[threshold], lastHistogramValue = 0 /* esta variable no serĂ­a necesaria, se puede asignar area */;
00070                                 if(area != 0)
00071                                         lastHistogramValue = area;
00072                                 histogram[threshold] = lastHistogramValue;
00073                                 }
00074 
00075                 // Calculate 'q' function (from the paper)
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                 // Get local minima of the function, and append them to MSER list
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                                 // We have a minimum in the q function
00090                                 {
00091                                 if (lastMSERThreshold == -1)
00092                                         // We don't have any previous local minima in the node
00093                                         {
00094                                         lastMSERThreshold = threshold;
00095                                         minLastMSERThreshold = threshold;
00096                                         }
00097                                 else
00098                                         if (RELATIVE_DISTANCE((float)histogram[lastMSERThreshold], (float)histogram[threshold]) < diffAreaThreshold)
00099                                                 // if distance with last minima isn't enough, we just actialize minimum MSER
00100                                                 {
00101                                                 if (q_function[minLastMSERThreshold] > q_function[threshold])
00102                                                                 minLastMSERThreshold = threshold;
00103                                                 }
00104                                         else    // We have a previous minima, and far enough from the actual minimum, we add the smallest MSER found in the previous set of minimums
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 



QVision framework. PARP research group, copyright 2007, 2008.