examples/features/features.cpp

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 
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <iostream>
00047 #include <QDebug>
00048 
00049 #include <qvdta.h>
00050 #include <qvip.h>
00051 
00052 #include <QVApplication>
00053 #include <QVMPlayerCamera>
00054 #include <QVDefaultGUI>
00055 #include <QVImageCanvas>
00056 #include <QVPolyline>
00057 #include <QVHarrisPointDetector>
00058 #include <QVCannyBorderDetector>
00059 #include <QVMSERDetector>
00060 
00061 #ifndef DOXYGEN_IGNORE_THIS
00062 class ContourExtractorWorker: public QVWorker
00063         {
00064         public:
00065                 ContourExtractorWorker(QString name): QVWorker(name)
00066                         {
00067                         addProperty<int>("Threshold", inputFlag,        128, "Threshold for a point to count as pertaining to a region", 0, 255);
00068                         addProperty<int>("MinAreaIPE", inputFlag,       0, "Minimal area to keep points in the IPE algorithm", 0, 50);
00069                         addProperty< QVImage<uChar,3> >("Input image", inputFlag|outputFlag);
00070                         addProperty< QList<QVPolyline> >("Internal contours", outputFlag);
00071                         addProperty< QList<QVPolyline> >("External contours", outputFlag);
00072                         }
00073 
00074                 void iterate()
00075                         {
00076                         // 0. Read input parameters
00077                         const QVImage<uChar,1> image = getPropertyValue< QVImage<uChar,3> >("Input image");
00078                         const uInt      rows = image.getRows(), cols = image.getCols(),
00079                                         threshold = getPropertyValue< int >("Threshold"),
00080                                         minAreaIPE = getPropertyValue< int >("MinAreaIPE");
00081 
00082                         timeFlag("Read input parameters");
00083                 
00084                         // 1. Get contours from image
00085                         const QList<QVPolyline> contours = getConnectedSetBorderContoursThreshold(image, threshold);
00086                         timeFlag("Get contours from image");
00087 
00088                         // 2. Apply IPE
00089                         QList<QVPolyline> ipeContours;
00090 
00091                         foreach(QVPolyline polyline, contours)
00092                                 {
00093                                 QVPolyline ipePolyline;
00094                                 IterativePointElimination(polyline, ipePolyline, minAreaIPE);
00095                                 if (ipePolyline.size() > 0)
00096                                         ipeContours.append(ipePolyline);
00097                                 }
00098 
00099                         timeFlag("IPE filtering");
00100 
00101                         // 3. Export contours to output property
00102                         QList<QVPolyline> internalContours, externalContours;
00103 
00104                         foreach(QVPolyline polyline, ipeContours)
00105                                 if (polyline.direction)
00106                                         internalContours.append(polyline);
00107                                 else
00108                                         externalContours.append(polyline);
00109 
00110                         setPropertyValue< QList< QVPolyline> >("Internal contours",internalContours);
00111                         setPropertyValue< QList< QVPolyline> >("External contours",externalContours);
00112                         timeFlag("Computed output contours");
00113                         }
00114         };
00115 
00116 int main(int argc, char *argv[])
00117         {
00118         QVApplication app(argc, argv,
00119                 "Example program for QVision library. Obtains several features from input video frames."
00120                 );
00121 
00122         ContourExtractorWorker contoursWorker("Contours Extractor Worker");
00123         QVCannyBorderDetector cannyWorker("Canny Operator Worker");
00124         QVHarrisPointDetector cornersWorker("Corners Worker");
00125         QVMSERDetector mserWorker("MSER Worker");
00126 
00127         QVMPlayerCamera camera("Video");
00128 
00129         camera.link(&cannyWorker,"Input image");
00130         camera.link(&contoursWorker,"Input image");
00131         camera.link(&cornersWorker,"Input image");
00132         camera.link(&mserWorker,"Input image");
00133 
00134         QVDefaultGUI interface;
00135 
00136         QVImageCanvas cannyCanvas("Canny");
00137         cannyCanvas.linkProperty(cannyWorker,"Output image");
00138         cannyCanvas.linkProperty(cannyWorker,"Output contours");
00139 
00140         QVImageCanvas contourCanvas("Contours");
00141         contourCanvas.linkProperty(contoursWorker, "Input image");
00142         contourCanvas.linkProperty(contoursWorker,"Internal contours", Qt::red);
00143         contourCanvas.linkProperty(contoursWorker,"External contours", Qt::blue);
00144 
00145         QVImageCanvas cornersCanvas("Corners");
00146         cornersCanvas.linkProperty(cornersWorker, "Input image");
00147         cornersCanvas.linkProperty(cornersWorker,"Corners", Qt::red, false);
00148 
00149         QVImageCanvas mserCanvas("MSER Regions");
00150         mserCanvas.linkProperty(mserWorker, "Input image");
00151         mserCanvas.linkProperty(mserWorker,"MSER contours", Qt::red);
00152 
00153         return app.exec();
00154         }
00155 
00156 #endif