PARP Research Group University of Murcia, Spain


examples/gyroscope/gyroscope.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 
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <iostream>
00041 
00042 #include <qvmath.h>
00043 #include <qvdta.h>
00044 #include <qvip.h>
00045 #include <qvprojective.h>
00046 
00047 #include <QVApplication>
00048 #include <QVMPlayerCamera>
00049 #include <QVDefaultGUI>
00050 #include <QVImageCanvas>
00051 #include <QVMatrix>
00052 
00053 #include <QVImageCanvas>
00054 #include <QVMatrix>
00055 #include <QVWorker>
00056 
00057 #include <hessiancornersextractor.h>
00058 #include <culebrillasdisplayer.h>
00059 #include <featuretracker.h>
00060 #include <planardetector.h>
00061 
00062 //#define GRAB_MOUSE
00063 
00064 #ifndef DOXYGEN_IGNORE_THIS
00065 
00067 
00068 #include <X11/Xlib.h>
00069 #include <X11/Xutil.h>
00070 #include <X11/Xos.h>
00071 #include <X11/Xatom.h> 
00072 #include <X11/cursorfont.h>
00073 #include <X11/extensions/XTest.h>
00074 
00076 // Worker classes
00077 
00078 class MouseTracker: public QVWorker
00079         {
00080         private:
00081                 QPointF center;
00082                 QVMatrix accum;
00083 
00084                 void mouseMove( int x, int y )
00085                         {
00086                         Display *display = XOpenDisplay(NULL);
00087                         int screen = DefaultScreen( display );
00088                         XTestFakeMotionEvent ( display, screen, x, y, 0 );
00089                         XCloseDisplay( display );
00090                         }
00091                 
00092                 void mouseButtonPress( int button, int isPress, unsigned long delay )
00093                         {
00094                         Display *display = XOpenDisplay(NULL);
00095                         int screen = DefaultScreen( display );
00096                         XTestFakeButtonEvent ( display, button , isPress, delay );
00097                         XCloseDisplay( display );
00098                         }
00099 
00100         public:
00101                 MouseTracker(QString name): QVWorker(name), center(0,0), accum(QVMatrix::identity(3))
00102                         {
00103                         // Input properties
00104                         addProperty< double >("Sensitivity", inputFlag, 1, "--", 0.1, 10);
00105                         addProperty< QVImage<uChar> >("Output image", outputFlag);
00106                         addProperty< QList<QPointF> >("Cross", outputFlag);
00107                         addProperty< QVMatrix >("Euclidean transformation", inputFlag);
00108 
00109                         addTrigger("Reset pointer");
00110 
00111                         QVImage<uChar> img(500,500);
00112                         Set(0, img);
00113                         setPropertyValue< QVImage<uChar> >("Output image", img);
00114                         }
00115 
00116                 void processTrigger(QString triggerName)
00117                         {
00118                         center = QPointF(0,0);
00119                         accum = QVMatrix::identity(3);
00120                         }
00121 
00122                 void iterate()
00123                         {
00124                         // 0. Read input property values.
00125                         const QVMatrix Hfound = getPropertyValue< QVMatrix >("Euclidean transformation");
00126                         const double sensitivity = getPropertyValue< double >("Sensitivity");
00127 
00128                         accum = Hfound * accum;
00129 
00130                         QVVector v1(3), v2(3), v3(3);
00131                         v1[0] = 0.1; v1[1] = 0.0; v1[2] = 1.0;
00132                         v2[0] = 0.0; v2[1] = 0.0; v2[2] = 1.0;
00133                         v3[0] = 0.0; v3[1] = 0.1; v3[2] = 1.0;
00134 
00135                         QPointF center1 = sensitivity * 250 * (accum * v1),
00136                                 center2 = sensitivity * 250 * (accum * v2),
00137                                 center3 = sensitivity * 250 * (accum * v3),
00138                                 center1_bis(250 - center1.x(), 250 + center1.y()),
00139                                 center2_bis(250 - center2.x(), 250 + center2.y()),
00140                                 center3_bis(250 - center3.x(), 250 + center3.y());
00141 
00142                         QList<QPointF> cross;
00143                         cross.append(center1_bis);
00144                         cross.append(center2_bis);
00145                         cross.append(center3_bis);
00146 
00147                         #ifdef GRAB_MOUSE
00148                                 mouseMove(center3.x(), center3.y());
00149                         #else
00150                                 setPropertyValue< QList<QPointF> >("Cross", cross);
00151                         #endif
00152                         timeFlag("Export culebrillas as polylines");
00153                         }
00154         };
00155 
00156 int main(int argc, char *argv[])
00157         {
00158         QVApplication app(argc, argv,
00159                 "Example program for QVision library. Finds a template pattern on the image."
00160                 );
00161 
00162         QVMPlayerCamera camera("Video");
00163 
00164         // Corner extraction
00165         HessianCornersExtractor cornersExtractor("Hessian corners extractor");
00166         camera.linkProperty(&cornersExtractor,"Input image");
00167 
00168         // Feature tracker
00169         FeatureTracker featureTracker("Feature tracker worker");
00170         cornersExtractor.linkProperty("Corner responses", &featureTracker, "Feature responses", QVWorker::SynchronousLink);
00171         cornersExtractor.linkProperty("Corner locations", &featureTracker, "Feature locations", QVWorker::SynchronousLink);
00172 
00173         // Culebrillas processor
00174         MouseTracker mouseTracker("Mouse tracker");
00175         featureTracker.linkProperty(&mouseTracker, QVWorker::SynchronousLink);
00176 
00177         #ifndef GRAB_MOUSE
00178 
00179         // Planar detector
00180         PlanarDetector planarDetector("Planar finder", false);
00181         featureTracker.linkProperty("Culebrillas container", &planarDetector, "Culebrillas container", QVWorker::AsynchronousLink);
00182 
00183         // Data output
00184         QVDefaultGUI interface;
00185 
00186         QVImageCanvas imageCanvasPointer("Culebrillas");
00187         mouseTracker.linkProperty("Output image", imageCanvasPointer);
00188         mouseTracker.linkProperty("Cross", imageCanvasPointer);
00189         imageCanvasPointer.setPropertyValue<QColor>("Cross $$ color", Qt::white);
00190 
00191         // Culebrillas
00192         CulebrillasDisplayer culebrillasDisplayer("Culebrillas processor");
00193         cornersExtractor.linkProperty("Input image", &culebrillasDisplayer, "Input image", QVWorker::SynchronousLink);
00194         featureTracker.linkProperty("Culebrillas container", &culebrillasDisplayer, "Culebrillas container", QVWorker::SynchronousLink);
00195         planarDetector.linkProperty("Planar culebrillas", &culebrillasDisplayer, "Planar culebrillas", QVWorker::AsynchronousLink);
00196 
00197         QVImageCanvas imageCanvas1("Culebrillas");
00198         culebrillasDisplayer.linkProperty("Input image", imageCanvas1);
00199         //imageCanvas1.linkProperty(culebrillasDisplayer, "Culebrillas head", Qt::red);
00200         //imageCanvas1.linkProperty(culebrillasDisplayer, "Planar culebrillas polylines head", Qt::red);
00201 
00202         //imageCanvas1.linkProperty(culebrillasDisplayer, "Planar culebrillas polylines head", Qt::blue);
00203         culebrillasDisplayer.linkProperty("Culebrillas", imageCanvas1);
00204         culebrillasDisplayer.linkProperty("Planar culebrillas polylines", imageCanvas1);
00205 
00206         #endif
00207 
00208         return app.exec();
00209         }
00210 
00211 #endif



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