examples/gyroscope/gyroscope.cpp

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

Generated on Thu Jul 17 17:23:27 2008 for QVision by  doxygen 1.5.3