PARP Research Group University of Murcia, Spain


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



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