src/qvip/qvip.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. 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 <stdio.h>
00026 #include <stdlib.h>
00027 #include <iostream>
00028 
00029 #include <qvip.h>
00030 #include <qvipp.h>
00031 
00032 void FilterLocalMax(const QVImage<sFloat> &src, QVImage<uChar> &dest, uInt colMaskSize, uInt rowMaskSize, sFloat threshold)
00033         {
00034         const int cols = src.getCols(), rows = src.getRows();
00035         Set(0, dest);
00036         sFloat actual;
00037         QVIMAGE_INIT_READ(sFloat,src);
00038         QVIMAGE_INIT_WRITE(uChar,dest);
00039         for(int row = rowMaskSize; row < rows-rowMaskSize; row++)
00040                 for(int col = colMaskSize; col < cols-colMaskSize; col++)
00041                         {
00042                         actual = QVIMAGE_PIXEL(src, col, row,0);
00043                         if (actual >= threshold)
00044                                 {
00045                                 QVIMAGE_PIXEL(dest, col, row, 0) = IPP_MAX_8U;
00046                                 for (int j = row-rowMaskSize; (j < row+rowMaskSize) && (QVIMAGE_PIXEL(dest, col, row, 0) > 0); j++)
00047                                         for (int i = col-colMaskSize; i < col+colMaskSize; i++)
00048                                                 if ( ((i != col) || (j != row)) && (actual <= QVIMAGE_PIXEL(src, i, j, 0)) )
00049                                                         {
00050                                                         QVIMAGE_PIXEL(dest, col, row, 0) = 0;
00051                                                         break;
00052                                                         }
00053                                 }
00054                         }
00055         }
00056 
00057 void FilterHarrisCornerResponseImage(const QVImage<uChar> &image, QVImage<sFloat> &result, const QPoint &destROIOffset)
00058         {
00059         const uInt rows = image.getRows(), cols = image.getCols();
00060         QVImage<uChar> buffer;
00061         MinEigenValGetBufferSize(image, buffer);
00062         QVImage<sFloat> eigval(cols, rows);
00063 
00064         MinEigenVal(image, eigval, ippKernelSobel, 3, 5, buffer);
00065 
00066         result = QVImage<sFloat>(cols, rows);
00067 
00068         FilterEqualizeHistogram(eigval, result, destROIOffset);
00069         }
00070 
00071 void FilterDoG(const QVImage<uChar> &image, QVImage<sFloat> &result)
00072         {
00073         const uInt rows = image.getRows(), cols = image.getCols();
00074         QVImage<uChar> gauss3x3(cols, rows), gauss5x5(cols, rows);
00075         
00076         FilterGauss(image, gauss3x3, ippMskSize3x3);
00077         FilterGauss(image, gauss5x5, ippMskSize5x5);
00078 
00079         result = QVImage<sFloat>(cols, rows);
00080         AbsDiff(gauss3x3, gauss5x5, result);    
00081         }
00082 
00083 void SobelCornerResponseImage(const QVImage<sFloat> &image, QVImage<sFloat> &result)
00084         {
00085         std::cerr << "WARNING: SobelCornerResponseImage is deprecated. Use FilterHessianCornerResponseImage instead." << std::endl;
00086         FilterHessianCornerResponseImage(image, result);
00087         }
00088 
00089 void FilterHessianCornerResponseImage(const QVImage<sFloat> &image, QVImage<sFloat> &result, const QPoint &destROIOffset)
00090         {
00091         const uInt      cols = image.getCols(), rows = image.getRows();
00092         //const QVImage<sFloat> imageSFloat = image;
00093 
00094         QVImage<sFloat> Gx(cols, rows), Gy(cols, rows), Gxx(cols, rows),
00095                         Gxy(cols, rows), Gyx(cols, rows), Gyy(cols, rows);
00096 
00097         FilterSobelHorizMask(image,Gx);
00098         FilterSobelVertMask(image,Gy);
00099 
00100         FilterSobelHorizMask(Gx,Gxx);
00101         FilterSobelVertMask(Gy,Gyy);
00102 
00103         FilterSobelHorizMask(Gy,Gyx);
00104         FilterSobelVertMask(Gx,Gxy);
00105 
00106         QVImage<sFloat> GxyGyx(cols, rows), GxxGyy(cols, rows);
00107 
00108         Mul(Gxy, Gyx, GxyGyx);
00109         Mul(Gxx, Gyy, GxxGyy);
00110 
00111         QVImage<sFloat> diffGxyGyx_GxxGyy(cols, rows);
00112 
00113         Sub(GxyGyx, GxxGyy, diffGxyGyx_GxxGyy);
00114 
00115         QVIMAGE_INIT_WRITE(sFloat,diffGxyGyx_GxxGyy);
00116         for (uInt col = 0; col < cols; col++)
00117                 for(uInt row = 0; row < rows; row++)
00118                         if (QVIMAGE_PIXEL(diffGxyGyx_GxxGyy, col, row, 0) <= 0)
00119                                 QVIMAGE_PIXEL(diffGxyGyx_GxxGyy, col, row, 0) = 0;
00120 
00121         result = QVImage<sFloat>(cols, rows);
00122 
00123         FilterEqualizeHistogram(diffGxyGyx_GxxGyy, result, destROIOffset);
00124         }
00125 
00126 
00127 int myFloodFill(QVImage<uChar> &image, uInt x, uInt y, uInt value, uInt minVal, uInt maxVal)
00128         {
00129         // Value should be inside range [minVal, maxVal]
00130         Q_ASSERT( (value <= minVal) || (value >= maxVal) );
00131         Q_ASSERT( minVal <= maxVal );
00132 
00133         // Coordinates should be inside the image.
00134         if ( (x >= image.getCols()) || (y >= image.getRows()))
00135                 return 0;
00136 
00137         if ( (image(x,y) < minVal) || (image(x,y) > maxVal) )
00138                 return 0;
00139 
00140         image(x,y) = value;
00141 
00142         int val = 1;
00143         val += myFloodFill(image, x-1, y, value, minVal, maxVal);
00144         val += myFloodFill(image, x, y-1, value, minVal, maxVal);
00145         val += myFloodFill(image, x+1, y, value, minVal, maxVal);
00146         val += myFloodFill(image, x, y+1, value, minVal, maxVal);
00147 
00148         val += myFloodFill(image, x-1, y-1, value, minVal, maxVal);
00149         val += myFloodFill(image, x-1, y+1, value, minVal, maxVal);
00150         val += myFloodFill(image, x+1, y-1, value, minVal, maxVal);
00151         val += myFloodFill(image, x+1, y+1, value, minVal, maxVal);
00152 
00153         return val;
00154         }
00155 
00156 void FilterSeparable(const QVImage<sFloat, 1> &image, QVImage<sFloat, 1> &dest,
00157         const QVVector &rowFilter, const QVVector &colFilter, const QPoint &destROIOffset)
00158         {
00159         const uInt cols = image.getCols(), rows = image.getRows();
00160         QVImage<sFloat> rowFiltered(cols, rows);
00161         FilterRow(image, rowFiltered, rowFilter);
00162         FilterColumn(rowFiltered, dest, colFilter, destROIOffset);
00163         }
00164 
00166 
00167 #include <qvip/qvimagefeatures/qvcomponenttree.h>
00168 
00169 void pruneLowComponentTreeAux(QVImage<uChar> &image, QVComponentTree &componentTree, uInt minArea, uInt node, uInt validThreshold)
00170         {
00171         Q_ASSERT(componentTree.area(node)[componentTree.firstThreshold(node)] != 0);
00172         Q_ASSERT(componentTree.area(node)[componentTree.lastThreshold(node)] != 0);
00173         Q_ASSERT(validThreshold >= componentTree.lastThreshold(node));
00174 
00175         bool prune = false;
00176         int lastValidThreshold = validThreshold;
00177 
00178         // Here we decide if this node should be directly pruned
00179         // or if there's any sub-node we should prune
00180         for (int threshold = componentTree.lastThreshold(node); threshold >= componentTree.firstThreshold(node) && !prune; threshold--)
00181                 if (componentTree.area(node)[threshold] > 0)
00182                         {
00183                         if (componentTree.area(node)[threshold] < minArea)
00184                                 prune = true;
00185                         else
00186                                 lastValidThreshold = threshold;
00187                         }
00188 
00189         // We prune node, or get on with it's childrens
00190         if (prune)
00191                 myFloodFill(image, componentTree.seedX(node), componentTree.seedY(node), lastValidThreshold, 0, lastValidThreshold-1);
00192         else
00193                 for (uInt child = componentTree.firstChild(node); child != NULL_NODE; child = componentTree.nextSibling(child))
00194                         pruneLowComponentTreeAux(image, componentTree, minArea, child, lastValidThreshold);
00195         }
00196 
00197 void pruneHighComponentTreeAux(QVImage<uChar> &image, QVComponentTree &componentTree, uInt minArea, uInt node, uInt validThreshold)
00198         {
00199         Q_ASSERT(componentTree.area(node)[componentTree.firstThreshold(node)] != 0);
00200         Q_ASSERT(componentTree.area(node)[componentTree.lastThreshold(node)] != 0);
00201         Q_ASSERT(validThreshold >= componentTree.lastThreshold(node));
00202 
00203         bool prune = false;
00204         int lastValidThreshold = validThreshold;
00205 
00206         // Here we decide if this node should be directly pruned
00207         // or if there's any sub-node we should prune
00208         for (int threshold = componentTree.lastThreshold(node); threshold >= componentTree.firstThreshold(node) && !prune; threshold--)
00209                 if (componentTree.area(node)[threshold] > 0)
00210                         {
00211                         if (componentTree.area(node)[threshold] < minArea)
00212                                 prune = true;
00213                         else
00214                                 lastValidThreshold = threshold;
00215                         }
00216 
00217         // We prune node, or get on with it's childrens
00218         if (prune)
00219                 myFloodFill(image, componentTree.seedX(node), componentTree.seedY(node), 255-lastValidThreshold, 255-lastValidThreshold+1, 255-0);
00220         else
00221                 for (uInt child = componentTree.firstChild(node); child != NULL_NODE; child = componentTree.nextSibling(child))
00222                         pruneHighComponentTreeAux(image, componentTree, minArea, child, lastValidThreshold);
00223         }
00224 
00225 void FilterPruneComponentTreeSmallRegions(QVImage<uChar> &image, QVComponentTree &componentTree, uInt minArea)
00226         {
00227         qDebug() << "pruneRegions()";
00228         if (componentTree.isInverseTree())
00229                 {
00230                 if(componentTree.area(componentTree.rootNode())[componentTree.lastThreshold(componentTree.rootNode())] > minArea)
00231                         pruneHighComponentTreeAux(image, componentTree, minArea, componentTree.rootNode(), componentTree.lastThreshold(componentTree.rootNode()));
00232                 }
00233         else    {
00234                 if(componentTree.area(componentTree.rootNode())[componentTree.lastThreshold(componentTree.rootNode())] > minArea)
00235                         pruneLowComponentTreeAux(image, componentTree, minArea, componentTree.rootNode(), componentTree.lastThreshold(componentTree.rootNode()));
00236                 }
00237 
00238         qDebug() << "pruneRegions() <~ return";
00239         }
00240 
00242 
00243 #define DEFINE_QVDTA_FUNCTION_EQUALIZE_HISTOGRAM(TYPE, C)                                                               \
00244 void FilterEqualizeHistogram(const QVImage<TYPE,C> &image, QVImage<TYPE,C> &equalized, const QPoint &destROIOffset)     \
00245         {                                                                                                               \
00246         uInt    rows = image.getRows(), cols = image.getCols();         \
00247         TYPE    maxVal, minVal;                                         \
00248                                                                         \
00249         Max(image,maxVal);                                              \
00250         Min(image,minVal);                                              \
00251                                                                         \
00252         QVImage<TYPE,C> temp(cols, rows), result(cols, rows);           \
00253         SubC(image, minVal, temp);                                      \
00254         MulC(temp, 255/(maxVal-minVal), result, 1, destROIOffset);      \
00255         equalized = result;                                             \
00256         }
00257 
00258 DEFINE_QVDTA_FUNCTION_EQUALIZE_HISTOGRAM(uChar,1);
00259 
00260 #define DEFINE_QVDTA_FUNCTION_EQUALIZE_HISTOGRAM2(TYPE, C)                                                              \
00261 void FilterEqualizeHistogram(const QVImage<TYPE,C> &image, QVImage<TYPE,C> &equalized, const QPoint &destROIOffset)     \
00262         {                                                                                                               \
00263         uInt    rows = image.getRows(), cols = image.getCols();         \
00264         TYPE    maxVal, minVal;                                         \
00265                                                                         \
00266         Max(image,maxVal);                                              \
00267         Min(image,minVal);                                              \
00268                                                                         \
00269         QVImage<TYPE,C> temp(cols, rows), result(cols, rows);           \
00270         SubC(image, minVal, temp);                                      \
00271         MulC(temp, 255/(maxVal-minVal), result, destROIOffset);         \
00272         equalized = result;                                             \
00273         }
00274 DEFINE_QVDTA_FUNCTION_EQUALIZE_HISTOGRAM2(sFloat,1);
00275 
00276 
00277