examples/rotoscoper/rotoscoper.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 
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <iostream>
00050 #include <QDebug>
00051 
00052 #include <qvip.h>
00053 
00054 #include <QVApplication>
00055 #include <QVMPlayerCamera>
00056 #include <QVDefaultGUI>
00057 #include <QVImageCanvas>
00058 #include <QVComponentTree>
00059 
00060 #ifndef DOXYGEN_IGNORE_THIS
00061 class ComponentTreeWorker: public QVWorker
00062         {
00063         public:
00064                 ComponentTreeWorker(QString name): QVWorker(name)
00065                         {
00066                         addProperty<int>("Maximal area to prune", inputFlag, 50,
00067                                 "Maximal size of the areas to be pruned in the image", 5, 10000);
00068                         addProperty< QVImage<uChar,1> >("Input image", inputFlag|outputFlag);
00069                         addProperty< QVImage<uChar,1> >("Pruned component tree image", outputFlag);
00070                         }
00071 
00072                 void iterate()
00073                         {
00075                         // Read parameters
00076                         QVImage<uChar> fromcamera = getPropertyValue< QVImage<uChar,1> >("Input image");
00077                         QVImage<uChar> image = fromcamera;
00078                         QVImage<uChar> temp = image;
00079 
00080                         const uInt      rows = image.getRows(), cols = image.getCols();
00081                         const uInt minAreaThreshold = getPropertyValue<int>("Maximal area to prune");
00082                         timeFlag("Read parameters");
00083                 
00085                         // Prune low areas from image, with component tree
00086                         QVComponentTree componentTreeLow(image,true);
00087                         timeFlag("Call to getComponentTree for low areas");
00088 
00089                         FilterPruneComponentTreeSmallRegions(image, componentTreeLow, minAreaThreshold);
00090                         timeFlag("Prune low areas from image");
00091 
00093                         // Prune high areas from image, with component tree
00094                         QVComponentTree componentTreeHigh(image);
00095                         timeFlag("Call to getComponentTree for high areas");
00096 
00097                         FilterPruneComponentTreeSmallRegions(image, componentTreeHigh, minAreaThreshold);
00098                         timeFlag("Prune high areas from image");
00099                 
00101                         // Publish resulting images
00102                         setPropertyValue< QVImage<uChar,1> >("Pruned component tree image", image);
00103                         timeFlag("Publish resulting images");
00104                         }
00105         };
00106 
00107 class CannyWorker: public QVWorker
00108         {
00109         public:
00110                 CannyWorker(QString name): QVWorker(name)
00111                         {
00112                         addProperty<double>("Threshold high", inputFlag,        150,    "High threshold for Canny operator", 50, 1000);
00113                         addProperty<double>("Threshold low", inputFlag,         50,     "Low threshold for Canny operator", 10, 500);
00114                         addProperty< QVImage<uChar,1> >("Canny image", outputFlag);
00115                         addProperty< QVImage<uChar,1> >("Input image", inputFlag|outputFlag);
00116                         }
00117 
00118                 void iterate()
00119                         {
00121                         // Read input parameters
00122                         QVImage<uChar,1> image = getPropertyValue< QVImage<uChar,1> >("Input image");
00123                         uInt cols = image.getCols(), rows = image.getRows();
00124                         QVImage<sFloat> imageFloat(cols, rows), dX(cols, rows), dY(cols, rows), dXNeg(cols, rows);
00125                         QVImage<uChar>  canny(cols, rows), buffer;
00126                 
00128                         // Convert image from uChar to sShort
00129                         Convert(image, imageFloat);
00130                         timeFlag("Convert image from uChar to sShort");
00131                 
00133                         // Obtain horizontal and vertical gradients from image
00134                         FilterSobelHorizMask(imageFloat, dY, ippMskSize3x3);
00135                         FilterSobelVertMask(imageFloat, dX, ippMskSize3x3);
00136                         MulC(dX, -1, dXNeg);
00137                         timeFlag("Obtain horizontal and vertical gradients from image");
00138                 
00140                         // Apply Canny operator
00141                         CannyGetSize(canny, buffer);
00142                         Canny(dXNeg, dY, canny, getPropertyValue<double>("Threshold low"), getPropertyValue<double>("Threshold high"), buffer);
00143                         timeFlag("Apply Canny operator");
00144                 
00146                         // Publish resulting images
00147                         setPropertyValue< QVImage<uChar,1> >("Canny image",canny);
00148                         timeFlag("Publish resulting images");
00149                         }
00150         };
00151 
00152 
00153 class ContourPainter: public QVWorker
00154         {
00155         public:
00156                 ContourPainter(QString name): QVWorker(name)
00157                         {
00158                         addProperty< QVImage<uChar,1> >("Borders image", inputFlag|outputFlag);
00159                         addProperty< QVImage<uChar,1> >("Flat colors image", inputFlag|outputFlag);
00160                         addProperty< QVImage<uChar,1> >("Output image", outputFlag);
00161                         }
00162 
00163                 void iterate()
00164                         {
00166                         // Read parameters
00167                         QVImage<uChar> bordersImage = getPropertyValue< QVImage<uChar,1> >("Borders image");
00168                         QVImage<uChar> flatColorsImage = getPropertyValue< QVImage<uChar,1> >("Flat colors image");
00169                         uInt    rows = bordersImage.getRows(), cols = bordersImage.getCols();
00170                         timeFlag("Read parameters");
00171                 
00173                         // Paint contours               
00174                         for(uInt col = 0; col < cols; col++)
00175                                 for (uInt row = 0; row < rows; row++)
00176                                         if (bordersImage(col, row))
00177                                                 flatColorsImage(col, row) = 0;
00178 
00180                         // Publish resulting images
00181                         setPropertyValue< QVImage<uChar,1> >("Output image", flatColorsImage);
00182                         timeFlag("Publish resulting images");
00183                         }
00184         };
00185 
00186 int main(int argc, char *argv[])
00187         {
00188         QVApplication app(argc, argv,
00189                 
00190                 "Example program for QVision library. Gets component tree from images."
00191 
00192                 );
00193         
00194         ComponentTreeWorker componentTreeWorker("Component Tree");
00195         CannyWorker cannyWorker("Canny operator");
00196         ContourPainter contourPainter("Contour painter");
00197 
00198         componentTreeWorker.linkProperty("Pruned component tree image", &cannyWorker, "Input image", QVWorker::SynchronousLink);
00199         componentTreeWorker.linkProperty("Pruned component tree image", &contourPainter, "Flat colors image", QVWorker::SynchronousLink);
00200 
00201         cannyWorker.linkProperty("Canny image", &contourPainter, "Borders image", QVWorker::SynchronousLink);
00202 
00203         QVMPlayerCamera camera("Video");
00204         camera.link(&componentTreeWorker,"Input image");
00205 
00206         QVDefaultGUI interface;
00207 
00208         QVImageCanvas imageCanvas("Rotoscoped image");
00209         imageCanvas.linkProperty(contourPainter,"Output image");
00210 
00211         return app.exec();
00212         }
00213 
00214 #endif
00215