examples/rotoscoper/rotoscoper.cpp

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

Generated on Fri Feb 22 18:26:55 2008 for QVision by  doxygen 1.5.3