root / trunk / tbeta / Windows / addons / ofxNCore / src / Modules / ofxNCoreVision.cpp @ 137

View | Annotate | Download (24.9 KB)

1
/*
2
*  ofxNCoreVision.cpp
3
*  NUI Group Community Core Vision
4
* 
5
*  Created by NUI Group Dev Team A on 2/1/09.
6
*  Copyright 2009 NUI Group/Inc. All rights reserved.
7
*
8
*/
9
10
#include "ofxNCoreVision.h"
11
#include "../Controls/gui.h"
12
13
/******************************************************************************
14
* The setup function is run once to perform initializations in the application
15
*****************************************************************************/
16
void ofxNCoreVision::setup()
17
{
18
        //set the title to 'tbeta'
19
        ofSetWindowTitle(" Community Core Vision ");
20
21
        //removes the 'x' button on windows which causes a crash due to a GLUT bug
22
#ifdef TARGET_WIN32
23
        HWND hwndConsole = FindWindowA(NULL, " Community Core Vision ");
24
        HMENU hMnu = ::GetSystemMenu(hwndConsole, FALSE);
25
        RemoveMenu(hMnu, SC_CLOSE, MF_BYCOMMAND);
26
#endif
27
28
        //ofSetBackgroundAuto(false);
29
30
        //create filter
31
        if ( filter == NULL )
32
        {
33
                filter = new ProcessFilters();
34
        } 
35
36
        //Load Settings from config.xml file
37
        loadXMLSettings();
38
39
        //Setup Window Properties
40
        ofSetWindowShape(winWidth,winHeight);
41
        ofSetFrameRate(camRate * 1.4);                        //This will be based on camera fps in the future
42
        ofSetVerticalSync(false);                    //Set vertical sync to false for better performance?
43
44
        //Pick the Source - camera or video
45
        if (bcamera)
46
        {
47
                activeInput = true;
48
49
/*                if( vidGrabber == NULL ) {
50
            vidGrabber = new ofVideoGrabber();
51
                        vidGrabber->listDevices();
52
            vidGrabber->setVerbose(true);
53
            vidGrabber->initGrabber(cwidth,cheight);  
54
                        int grabW = vidGrabber->width;
55
                        int grabH = vidGrabber->height;
56
                        printf("Camera Mode\nAsked for %i by %i - actual size is %i by %i \n\n", camWidth, camHeight, grabW, grabH);
57
        }
58
                #ifdef TARGET_WIN32
59
                /////ffmv - firefly camera only//////
60
                if(ffmv.getDeviceCount() > 0){
61
                   ffmv.listDevices();                
62
                   ffmv.initFFMV(camWidth,camHeight);
63
                   printf("Camera Mode\nAsked for %i by %i - actual size is %i by %i \n\n", camWidth, camHeight, camWidth, camHeight);
64
                }
65
*/
66
                /////PS3 - PS3 camera only//////
67
                if(ofxPS3::getDeviceCount() > 0)
68
                {
69
                        PS3 = new ofxPS3();
70
                        PS3->listDevices();
71
                        PS3->initPS3(camWidth, camHeight, camRate);
72
                        printf("Camera Mode\nAsked for %i by %i - actual size is %i by %i \n\n", camWidth, camHeight, camWidth, camHeight);
73
                }
74
//                #endif 
75
                /////**************************//////
76
                }
77
        else
78
        {
79
        if( vidPlayer == NULL ) {
80
                        activeInput = true;
81
            vidPlayer = new ofVideoPlayer();            
82
            vidPlayer->loadMovie( videoFileName );
83
            vidPlayer->play(); 
84
                        printf("Video Mode\n\n");
85
                        camHeight = vidPlayer->height;
86
                        camWidth = vidPlayer->width;
87
        }
88
        }
89
90
        /*****************************************************************************************************
91
        * Allocate images (needed for drawing/processing images)
92
        ******************************************************************************************************/
93
        processedImg.allocate(camWidth, camHeight); //main Image that'll be processed.
94
        processedImg.setUseTexture(false);
95
        sourceImg.allocate(camWidth, camHeight);    //Source Image
96
        sourceImg.setUseTexture(false);                                //We don't need to draw this so don't create a texture
97
        /******************************************************************************************************/
98
99
        //Fonts - Is there a way to dynamically change font size?
100
        verdana.loadFont("verdana.ttf", 8, true, true);           //Font used for small images
101
        sidebarTXT.loadFont("verdana.ttf", 8, true, true);
102
        bigvideo.loadFont("verdana.ttf", 13, true, true);  //Font used for big images.
103
104
        //Static Images
105
        background.loadImage("images/background.jpg"); //Main (Temp?) Background
106
107
        gui = ofxGui::Instance(this);
108
        setupGUI();
109
110
        calib.setup(camWidth, camHeight, &tracker);
111
112
        filter->allocate( camWidth, camHeight );
113
114
        if (autoTracker)
115
        {
116
                printf("Starting in tracking mode...\n");
117
                showConfiguration = true;
118
        }
119
120
        if (bMiniMode)
121
        {
122
                bShowInterface = false;
123
                printf("Starting in Mini Mode...\n");
124
                ofSetWindowShape(190, 200); //minimized size
125
                //ofSetWindowTitle("Mini");
126
                filter->bMiniMode = bMiniMode;
127
        }
128
        else{
129
                //full mode        
130
                bShowInterface = true;
131
        }
132
        printf("Community Core Vision is setup!\n\n");
133
}
134
135
/****************************************************************
136
*        Load/Save config.xml file Settings
137
****************************************************************/
138
void ofxNCoreVision::loadXMLSettings()
139
{
140
        // TODO: a seperate XML to map keyboard commands to action
141
        message = "Loading config.xml...";
142
        // Can this load via http?
143
        if ( XML.loadFile("config.xml"))
144
        {
145
                message = "Settings Loaded!";
146
        }
147
        else
148
        {
149
                //FAIL!
150
                message = "No Settings Found...";
151
        }
152
        //--------------------------------------------------------------
153
        //  START BINDING XML TO VARS
154
        //--------------------------------------------------------------
155
        //frameRate                        = XML.getValue("CONFIG:APPLICATION:FRAMERATE",0);
156
157
        winWidth                                        = XML.getValue("CONFIG:WINDOW:WIDTH", 950);
158
        winHeight                                        = XML.getValue("CONFIG:WINDOW:HEIGHT", 600);
159
        bcamera                                                = XML.getValue("CONFIG:CAMERA_0:USECAMERA", 1);
160
        deviceID                                        = XML.getValue("CONFIG:CAMERA_0:DEVICE", 0);
161
        camWidth                                        = XML.getValue("CONFIG:CAMERA_0:WIDTH", 320);
162
        camHeight                                        = XML.getValue("CONFIG:CAMERA_0:HEIGHT", 240);
163
        camRate                                                = XML.getValue("CONFIG:CAMERA_0:FRAMERATE", 0);
164
        videoFileName                                = XML.getValue("CONFIG:VIDEO:FILENAME", "RearDI.m4v");
165
        maxBlobs                                        = XML.getValue("CONFIG:BLOBS:MAXNUMBER", 20);
166
        bShowLabels                                        = XML.getValue("CONFIG:BOOLEAN:LABELS",0);
167
        bDrawOutlines                                = XML.getValue("CONFIG:BOOLEAN:OUTLINES",0);
168
        filter->bLearnBakground                = XML.getValue("CONFIG:BOOLEAN:LEARNBG",0);
169
        filter->bVerticalMirror                = XML.getValue("CONFIG:BOOLEAN:VMIRROR",0);
170
        filter->bHorizontalMirror        = XML.getValue("CONFIG:BOOLEAN:HMIRROR",0);
171
        //Filters
172
        filter->bHighpass                        = XML.getValue("CONFIG:BOOLEAN:HIGHPASS",1);
173
        filter->bAmplify                        = XML.getValue("CONFIG:BOOLEAN:AMPLIFY", 1);
174
        filter->bSmooth                                = XML.getValue("CONFIG:BOOLEAN:SMOOTH", 1);
175
        filter->bDynamicBG                        = XML.getValue("CONFIG:BOOLEAN:DYNAMICBG", 1);
176
        //MODES
177
        bGPUMode                                        = XML.getValue("CONFIG:BOOLEAN:GPU", 0);
178
        bMiniMode                   = XML.getValue("CONFIG:BOOLEAN:MINIMODE",0);
179
        //CONTROLS
180
        tracker.MIN_MOVEMENT_THRESHOLD        = XML.getValue("CONFIG:INT:MINMOVEMENT",0);
181
        MIN_BLOB_SIZE                                = XML.getValue("CONFIG:INT:MINBLOBSIZE",2);
182
        MAX_BLOB_SIZE                                = XML.getValue("CONFIG:INT:MAXBLOBSIZE",100);
183
        backgroundLearnRate                        = XML.getValue("CONFIG:INT:BGLEARNRATE", 0.01f);
184
        //Filter Settings
185
        filter->threshold                        = XML.getValue("CONFIG:INT:THRESHOLD",0);
186
        filter->highpassBlur                = XML.getValue("CONFIG:INT:HIGHPASSBLUR",0);
187
        filter->highpassNoise                = XML.getValue("CONFIG:INT:HIGHPASSNOISE",0);
188
        filter->highpassAmp                        = XML.getValue("CONFIG:INT:HIGHPASSAMP",0);
189
        filter->smooth                                = XML.getValue("CONFIG:INT:SMOOTH",0);
190
        //--------------------------------------------------- TODO XML NETWORK SETTINGS
191
        bTUIOMode                                        = XML.getValue("CONFIG:BOOLEAN:TUIO",0);
192
        myTUIO.bOSCMode                                = XML.getValue("CONFIG:BOOLEAN:OSCMODE",1);
193
        myTUIO.bTCPMode                                = XML.getValue("CONFIG:BOOLEAN:TCPMODE",1);
194
        myTUIO.bHeightWidth                        = XML.getValue("CONFIG:BOOLEAN:HEIGHTWIDTH",0);
195
        tmpLocalHost                                = XML.getValue("CONFIG:NETWORK:LOCALHOST", "localhost");
196
        tmpPort                                                = XML.getValue("CONFIG:NETWORK:TUIOPORT_OUT", 3333);
197
        myTUIO.setup(tmpLocalHost.c_str(), tmpPort); //have to convert tmpLocalHost to a const char*
198
        //--------------------------------------------------------------
199
        //  END XML SETUP
200
}
201
202
void ofxNCoreVision::saveConfiguration()
203
{
204
        XML.setValue("CONFIG:CAMERA_0:USECAMERA", bcamera);
205
        XML.setValue("CONFIG:CAMERA_0:DEVICE", deviceID);
206
        XML.setValue("CONFIG:CAMERA_0:WIDTH", camWidth);
207
        XML.setValue("CONFIG:CAMERA_0:HEIGHT", camHeight);
208
        XML.setValue("CONFIG:CAMERA_0:FRAMERATE", camRate);
209
        XML.setValue("CONFIG:BOOLEAN:PRESSURE",bShowPressure);
210
        XML.setValue("CONFIG:BOOLEAN:LABELS",bShowLabels);
211
        XML.setValue("CONFIG:BOOLEAN:OUTLINES",bDrawOutlines);
212
        XML.setValue("CONFIG:BOOLEAN:LEARNBG", filter->bLearnBakground);    
213
        XML.setValue("CONFIG:BOOLEAN:VMIRROR", filter->bVerticalMirror);
214
        XML.setValue("CONFIG:BOOLEAN:HMIRROR", filter->bHorizontalMirror);
215
        XML.setValue("CONFIG:BOOLEAN:HIGHPASS", filter->bHighpass);
216
        XML.setValue("CONFIG:BOOLEAN:AMPLIFY", filter->bAmplify);
217
        XML.setValue("CONFIG:BOOLEAN:SMOOTH", filter->bSmooth);
218
        XML.setValue("CONFIG:BOOLEAN:DYNAMICBG", filter->bDynamicBG);
219
        XML.setValue("CONFIG:BOOLEAN:GPU", bGPUMode);        
220
        XML.setValue("CONFIG:INT:MINMOVEMENT", tracker.MIN_MOVEMENT_THRESHOLD);
221
        XML.setValue("CONFIG:INT:MINBLOBSIZE", MIN_BLOB_SIZE);
222
        XML.setValue("CONFIG:INT:MAXBLOBSIZE", MAX_BLOB_SIZE);
223
        XML.setValue("CONFIG:INT:BGLEARNRATE", backgroundLearnRate);
224
        XML.setValue("CONFIG:INT:THRESHOLD", filter->threshold);
225
        XML.setValue("CONFIG:INT:HIGHPASSBLUR", filter->highpassBlur);
226
        XML.setValue("CONFIG:INT:HIGHPASSNOISE", filter->highpassNoise);
227
        XML.setValue("CONFIG:INT:HIGHPASSAMP", filter->highpassAmp);
228
        XML.setValue("CONFIG:INT:SMOOTH", filter->smooth);
229
        XML.setValue("CONFIG:BOOLEAN:MINIMODE", bMiniMode);        
230
        XML.setValue("CONFIG:BOOLEAN:TUIO",bTUIOMode);
231
        XML.setValue("CONFIG:BOOLEAN:HEIGHTWIDTH", myTUIO.bHeightWidth);
232
        XML.setValue("CONFIG:BOOLEAN:OSCMODE", myTUIO.bOSCMode);
233
        XML.setValue("CONFIG:BOOLEAN:TCPMODE", myTUIO.bTCPMode);
234
235
        //        XML.setValue("CONFIG:NETWORK:LOCALHOST", *myTUIO.localHost);
236
        //        XML.setValue("CONFIG:NETWORK:TUIO_PORT_OUT",myTUIO.TUIOPort);
237
238
        XML.saveFile("config.xml");
239
}
240
241
/******************************************************************************
242
* The update function runs continuously. Use it to update states and variables
243
*****************************************************************************/
244
void ofxNCoreVision::update()
245
{        
246
        if(exited) return;
247
248
        bNewFrame = false;
249
250
        if (activeInput)
251
        {
252
                if (bcamera) //if camera
253
                {
254
                        if(PS3!=NULL)//ps3 camera
255
                        {
256
                                bNewFrame = PS3->isFrameNew();        
257
                        }
258
/*                        else if(deviceID < = vidGrabber->getDeviceCount()) //videoInput camera
259
                         {
260
                                 vidGrabber->grabFrame();
261
                                 bNewFrame = vidGrabber->isFrameNew();
262
                         }
263
                         else{//else firefly MV camera
264
                                 ffmv.grabFrame();
265
                                 bNewFrame = true;                        
266
                         }
267
*/                }
268
                else //if video
269
                {
270
                        vidPlayer->idleMovie();
271
                        bNewFrame = vidPlayer->isFrameNew();
272
                }
273
274
                //if no new frame, return                
275
                if(!bNewFrame){
276
                        return;
277
                }
278
                else//else process camera frame
279
                {
280
                        ofBackground(0, 0, 0);
281
282
                        // Calculate FPS of Camera
283
                        frames++;
284
                        float time = ofGetElapsedTimeMillis();
285
                        if (time > (lastFPSlog + 1000))
286
                        {
287
                                fps = frames;
288
                                frames = 0;
289
                                lastFPSlog = time;
290
                        }//End calculation
291
292
                        float beforeTime = ofGetElapsedTimeMillis();
293
294
                        if (bGPUMode)
295
                        {
296
                                grabFrameToGPU(filter->gpuSourceTex);
297
                                filter->applyGPUFilters();
298
                                contourFinder.findContours(filter->gpuReadBackImageGS,  (((camWidth * camHeight) * .0001) * MIN_BLOB_SIZE) - ((camWidth * camHeight) * .0001), (((camWidth * camHeight) * .0001) * MAX_BLOB_SIZE) - ((camWidth * camHeight) * .0001), maxBlobs, false);  
299
                        }
300
                        else
301
                        {
302
                                grabFrameToCPU();
303
                                filter->applyCPUFilters( processedImg );
304
                                contourFinder.findContours(processedImg,  (((camWidth * camHeight) * .0001) * MIN_BLOB_SIZE) - ((camWidth * camHeight) * .0001), (((camWidth * camHeight) * .0001) * MAX_BLOB_SIZE) - ((camWidth * camHeight) * .0001), maxBlobs, false);
305
                        }
306
307
                        //Track found contours/blobss
308
                        tracker.track(&contourFinder);
309
                        //get DSP time
310
                        differenceTime = ofGetElapsedTimeMillis() - beforeTime;
311
312
                        //Dynamic Background subtraction LearRate
313
                        if (filter->bDynamicBG)
314
                        {
315
                                filter->fLearnRate = backgroundLearnRate * .0001; //If there are no blobs, add the background faster.
316
                                if (contourFinder.nBlobs > 0) //If there ARE blobs, add the background slower.
317
                                {
318
                                        filter->fLearnRate = backgroundLearnRate * .0001;
319
                                }
320
                        }//End Background Learning rate
321
322
                        if (bTUIOMode)
323
                        {
324
                                //Start sending OSC
325
                                myTUIO.sendTUIO();
326
                        }
327
                }
328
        }
329
}
330
331
/************************************************
332
*                                Frame Grab
333
************************************************/
334
335
//Grab frame from CPU
336
void ofxNCoreVision::grabFrameToCPU()
337
{
338
        //Set sourceImg as new camera/video frame
339
        if (bcamera)
340
        {
341
                if(PS3!=NULL){
342
                        sourceImg.setFromPixels(PS3->getPixels(), camWidth, camHeight);
343
                        //convert to grayscale
344
                         processedImg = sourceImg;
345
                }
346
/*                 else if(deviceID < = vidGrabber->getDeviceCount())
347
                 {
348
                         sourceImg.setFromPixels(vidGrabber->getPixels(), camWidth, camHeight);
349
                         //convert to grayscale
350
                         processedImg = sourceImg;
351
                 }
352
                 else{
353
                        processedImg.setFromPixels(ffmv.fcImage[ffmv.getDeviceID()].pData,camWidth,camHeight);
354
                 }
355
*/        }
356
        else
357
        {
358
                sourceImg.setFromPixels(vidPlayer->getPixels(), camWidth, camHeight);
359
                //convert to grayscale
360
                processedImg = sourceImg;
361
        }
362
}
363
364
//Grab frame from GPU
365
void ofxNCoreVision::grabFrameToGPU(GLuint target)
366
{
367
        //grab the frame to a raw openGL texture
368
        if (bcamera)
369
        {
370
                glEnable(GL_TEXTURE_2D);
371
                //glPixelStorei(1);
372
                glBindTexture(GL_TEXTURE_2D, target);                                
373
                if(PS3!=NULL)
374
                {
375
                        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, camWidth, camHeight, GL_RGB, GL_UNSIGNED_BYTE, PS3->getPixels());
376
                }
377
                else if(deviceID <= vidGrabber->getDeviceCount())
378
                {
379
                        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, camWidth, camHeight, GL_RGB, GL_UNSIGNED_BYTE, vidGrabber->getPixels());
380
                }
381
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
382
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
383
                glBindTexture(GL_TEXTURE_2D,0);
384
        }
385
        else
386
        {
387
                glEnable(GL_TEXTURE_2D);
388
                glBindTexture(GL_TEXTURE_2D, target);
389
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, camWidth, camHeight, GL_RGB, GL_UNSIGNED_BYTE, vidPlayer->getPixels());
390
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
391
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
392
                glBindTexture(GL_TEXTURE_2D,0);
393
        }
394
}
395
396
397
/******************************************************************************
398
* The draw function paints the textures onto the screen. It runs after update.
399
*****************************************************************************/
400
void ofxNCoreVision::draw()
401
{
402
        if(exited) return;
403
404
        if (showConfiguration)
405
        {
406
                //temporary. only auto draw if in fullscreen
407
                if (bNewFrame && bFullscreen == false)
408
                {
409
                        drawToScreen();
410
                }
411
                else
412
                {
413
                        drawToScreen();
414
                }
415
                if (!bCalibration && !bMiniMode)
416
                        gui->draw();
417
        }
418
}
419
420
void ofxNCoreVision::drawToScreen()
421
{
422
        //if calibration
423
        if (bCalibration)
424
        {
425
                //Don't draw main interface
426
                calib.passInContourFinder(contourFinder.nBlobs, contourFinder.blobs);
427
                calib.doCalibration();
428
        }
429
        //if mini mode
430
        else if (bMiniMode)
431
        {
432
                drawMiniMode();
433
        }
434
        //if full mode
435
        else if (bShowInterface)
436
        {
437
                drawFullMode();                  
438
439
                if(bDrawOutlines || bShowLabels){
440
                        drawFingerOutlines();
441
                }
442
        }
443
}
444
445
446
void ofxNCoreVision::drawFullMode(){
447
448
        ofSetColor(0xFFFFFF);
449
        //Draw Background Image
450
        background.draw(0, 0);
451
        //Draw arrows
452
        ofSetColor(187, 200, 203);
453
        ofFill();
454
        ofTriangle(680, 420, 680, 460, 700, 440);
455
        ofTriangle(70, 420, 70, 460, 50, 440);
456
        ofSetColor(255, 255, 0);
457
458
        ofSetColor(0xFFFFFF);
459
        //Draw Image Filters To Screen
460
        if (bGPUMode) filter->drawGPU();
461
        else filter->draw();
462
463
        ofSetColor(0x000000);
464
        if (bShowPressure)
465
        {
466
                bigvideo.drawString("Pressure Map", 140, 20);
467
        }
468
        else
469
        {
470
                bigvideo.drawString("Source Image", 140, 20);
471
        }
472
        bigvideo.drawString("Tracked Image", 475, 20);
473
474
        //draw link to tbeta website
475
        ofSetColor(79, 79, 79);
476
        ofFill();
477
        ofRect(ofGetWidth() - 228,ofGetHeight() - 14, 228, 14);
478
        ofSetColor(0xFFFFFF);
479
        ofDrawBitmapString("|  ~  |tbeta.nuigroup.com", ofGetWidth() - 228, ofGetHeight() - 2);
480
481
        //Display Application information in bottom right
482
        string str = "Calc. Time [ms]: ";
483
        str+= ofToString(differenceTime, 0)+"\n\n";
484
485
        if (bcamera)
486
        {
487
                string str2 = "Camera [Res]:     ";
488
                str2+= ofToString(PS3->getCamWidth(), 0) + " x " + ofToString(PS3->getCamHeight(), 0)  + "\n";
489
                string str4 = "Camera [fps]:     ";
490
                str4+= ofToString(fps, 0)+"\n";
491
                ofSetColor(0xFFFFFF);
492
                sidebarTXT.drawString(str + str2 + str4, 740, 410);
493
        }
494
        else
495
        {
496
                string str2 = "Video [Res]:      ";
497
                str2+= ofToString(vidPlayer->width, 0) + " x " + ofToString(vidPlayer->height, 0)  + "\n";
498
                string str4 = "Video [fps]:       ";
499
                str4+= ofToString(fps, 0)+"\n";
500
                ofSetColor(0xFFFFFF);
501
                sidebarTXT.drawString(str + str2 + str4, 740, 410);
502
        }
503
504
        if (bTUIOMode)
505
        {
506
                //Draw Port and IP to screen
507
                ofSetColor(0xffffff);
508
                char buf[256];
509
                sprintf(buf, "Sending TUIO messages to:\nHost: %s\nPort: %i", myTUIO.localHost, myTUIO.TUIOPort);
510
                sidebarTXT.drawString(buf, 740, 480);
511
        }
512
513
        ofSetColor(0xFF0000);
514
        sidebarTXT.drawString("Press spacebar to toggle mini mode", ofGetWidth() - 215, 580);   
515
}
516
517
void ofxNCoreVision::drawMiniMode()
518
{
519
        //black background
520
        ofSetColor(0,0,0);
521
        ofRect(0,0,ofGetWidth(), ofGetHeight());
522
        //draw outlines        
523
        if (bDrawOutlines){
524
                for (int i=0; i<contourFinder.nBlobs; i++)
525
                {
526
                        contourFinder.blobs[i].drawContours(0,0, camWidth, camHeight+175, ofGetWidth(), ofGetHeight());
527
                }
528
        }
529
530
        //draw grey rectagles for text information
531
        ofSetColor(128,128,128);
532
        ofFill();
533
        ofRect(0,ofGetHeight() - 83, ofGetWidth(), 20);
534
        ofRect(0,ofGetHeight() - 62, ofGetWidth(), 20);
535
        ofRect(0,ofGetHeight() - 41, ofGetWidth(), 20);
536
        ofRect(0,ofGetHeight() - 20, ofGetWidth(), 20);
537
538
        //draw text
539
        ofSetColor(250,250,250);
540
        sidebarTXT.drawString("Calc. Time  [ms]:        " + ofToString(differenceTime,0),10, ofGetHeight() - 70 );
541
        if (bcamera){
542
                sidebarTXT.drawString("Camera [fps]:            " + ofToString(fps,0),10, ofGetHeight() - 50 );
543
        }
544
        else {
545
                sidebarTXT.drawString("Video [fps]:             " + ofToString(fps,0),10, ofGetHeight() - 50 );
546
        }
547
        sidebarTXT.drawString("Blob Count:               " + ofToString(contourFinder.nBlobs,0),10, ofGetHeight() - 29 );
548
        sidebarTXT.drawString("Sending TUIO:  " ,10, ofGetHeight() - 9 );
549
550
        //draw green tuio circle
551
        if (bTUIOMode)
552
        {
553
                //Draw GREEN CIRCLE 'ON' LIGHT
554
                ofSetColor(0x00FF00);
555
                ofFill();
556
                ofCircle(ofGetWidth() - 17 , ofGetHeight() - 10, 5);
557
                ofNoFill();
558
        }
559
}
560
561
void ofxNCoreVision::drawFingerOutlines()
562
{
563
        //Find the blobs for drawing
564
        for (int i=0; i<contourFinder.nBlobs; i++)
565
        {
566
                if (bDrawOutlines)
567
                {
568
                        //Draw contours (outlines) on the source image
569
                        contourFinder.blobs[i].drawContours(40, 30, camWidth, camHeight, MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT);
570
                }
571
                if (bShowLabels) //Show ID label;
572
                {
573
                        float xpos = contourFinder.blobs[i].centroid.x * (MAIN_WINDOW_WIDTH/camWidth);
574
                        float ypos = contourFinder.blobs[i].centroid.y * (MAIN_WINDOW_HEIGHT/camHeight);
575
576
                        ofSetColor(0xCCFFCC);
577
                        char idStr[1024];
578
                        sprintf(idStr, "id: %i", contourFinder.blobs[i].id);
579
                        verdana.drawString(idStr, xpos + 365, ypos + contourFinder.blobs[i].boundingRect.height/2 + 45);
580
                }
581
        }
582
        ofSetColor(0xFFFFFF);
583
}
584
585
/*****************************************************************************
586
* KEY EVENTS
587
*****************************************************************************/
588
void ofxNCoreVision::keyPressed(int key)
589
{
590
        // detect escape key
591
        if(key==0x1b)
592
        {
593
                exited=true;
594
        }
595
596
        if (showConfiguration)
597
        {
598
                switch (key)
599
                {
600
                        // ughhh!!!!
601
                        // my main problem is that the slider doesn't move....
602
                case 'a':
603
                        filter->threshold++;
604
                        gui->update(appPtr->trackedPanel_threshold, kofxGui_Set_Int, &appPtr->filter->threshold, sizeof(int));
605
                        break;
606
                case 'z':
607
                        filter->threshold--;
608
                        gui->update(appPtr->trackedPanel_threshold, kofxGui_Set_Int, &appPtr->filter->threshold, sizeof(int));
609
                        break;
610
                case 'b':
611
                        filter->bLearnBakground = true;
612
                        break;
613
                case 'o':
614
                        bDrawOutlines ? bDrawOutlines = false : bDrawOutlines = true;
615
                        gui->update(appPtr->trackedPanel_outlines, kofxGui_Set_Bool, &appPtr->bDrawOutlines, sizeof(bool));
616
                        break;
617
                case 'h':
618
                        filter->bHorizontalMirror ? filter->bHorizontalMirror = false : filter->bHorizontalMirror = true;
619
                        gui->update(appPtr->propertiesPanel_flipH, kofxGui_Set_Bool, &appPtr->filter->bHorizontalMirror, sizeof(bool));
620
                        break;
621
                case 'j':
622
                        filter->bVerticalMirror ? filter->bVerticalMirror = false : filter->bVerticalMirror = true;
623
                        gui->update(appPtr->propertiesPanel_flipV, kofxGui_Set_Bool, &appPtr->filter->bVerticalMirror, sizeof(bool));
624
                        break;
625
                case 't':
626
                        myTUIO.bOSCMode = !myTUIO.bOSCMode;
627
                        myTUIO.bTCPMode = false;
628
                        bTUIOMode = myTUIO.bOSCMode;
629
                        gui->update(appPtr->optionPanel_tuio_tcp, kofxGui_Set_Bool, &appPtr->myTUIO.bTCPMode, sizeof(bool));
630
                        gui->update(appPtr->optionPanel_tuio_osc, kofxGui_Set_Bool, &appPtr->myTUIO.bOSCMode, sizeof(bool));
631
                        //clear blobs
632
                        myTUIO.blobs.clear();                        
633
                        break;
634
                case 'f':           
635
                        myTUIO.bOSCMode = false;
636
                        myTUIO.bTCPMode = !myTUIO.bTCPMode;
637
                        bTUIOMode = myTUIO.bTCPMode;
638
                        gui->update(appPtr->optionPanel_tuio_tcp, kofxGui_Set_Bool, &appPtr->myTUIO.bTCPMode, sizeof(bool));
639
                        gui->update(appPtr->optionPanel_tuio_osc, kofxGui_Set_Bool, &appPtr->myTUIO.bOSCMode, sizeof(bool));
640
                        //clear blobs
641
                        myTUIO.blobs.clear();                        
642
                        break;
643
                case 'g':
644
                        bGPUMode ? bGPUMode = false : bGPUMode = true;
645
                        gui->update(appPtr->gpuPanel_use, kofxGui_Set_Bool, &appPtr->bGPUMode, sizeof(bool));
646
                        filter->bLearnBakground = true;
647
                        break;
648
                case 'v':
649
                        if (bcamera)
650
                                vidGrabber->videoSettings();
651
                        break;
652
                case 'l':
653
                        bShowLabels ? bShowLabels = false : bShowLabels = true;
654
                        gui->update(appPtr->trackedPanel_ids, kofxGui_Set_Bool, &appPtr->bShowLabels, sizeof(bool));
655
                        break;
656
                case 'p':
657
                        bShowPressure ? bShowPressure = false : bShowPressure = true;
658
                        break;
659
                case ' ': 
660
                        if (bMiniMode && !bCalibration) // NEED TO ADD HERE ONLY GO MINI MODE IF NOT CALIBRATING
661
                        {
662
                                bMiniMode = false;
663
                                bShowInterface = true;
664
                                filter->bMiniMode = bMiniMode;
665
                                ofSetWindowShape(950,600); //default size
666
                        }
667
                        else if(!bCalibration)
668
                        {
669
                                bMiniMode = true;
670
                                bShowInterface = false;
671
                                filter->bMiniMode = bMiniMode;
672
                                ofSetWindowShape(190,200); //minimized size
673
                        }
674
                        break;
675
                case 'x': //Exit Calibrating
676
                        if (bCalibration)
677
                        {        bShowInterface = true;
678
                        bCalibration = false;
679
                        calib.calibrating = false;
680
                        tracker.isCalibrating = false;
681
                        if (bFullscreen == true) ofToggleFullscreen();
682
                        bFullscreen = false;
683
                        ofSetBackgroundAuto(false);
684
                        }
685
                        break;
686
                }
687
        }
688
}
689
690
void ofxNCoreVision::keyReleased(int key)
691
{
692
        if (showConfiguration)
693
        {
694
                if ( key == 'c' && !bCalibration)
695
                {        
696
                        bShowInterface = false;
697
                        //Enter/Exit Calibration
698
                        bCalibration = true;
699
                        calib.calibrating = true;
700
                        tracker.isCalibrating = true;
701
                        if (bFullscreen == false) ofToggleFullscreen();
702
                        bFullscreen = true;
703
                        ofSetBackgroundAuto(true);
704
                }
705
        }
706
        if ( key == '~' || key == '`' && !bMiniMode && !bCalibration) showConfiguration = !showConfiguration;
707
}
708
709
/*****************************************************************************
710
*        Touch EVENTS
711
*****************************************************************************/
712
void ofxNCoreVision::TouchDown( ofxTBetaCvBlob b)
713
{
714
        if (bTUIOMode)//If sending TUIO, add the blob to the map list
715
        {
716
                //if blob is not otuside calibration mesh
717
                if (b.centroid.x != 0 && b.centroid.y != 0)
718
                        myTUIO.blobs[b.id] = b;
719
        }
720
}
721
722
void ofxNCoreVision::TouchUp( ofxTBetaCvBlob b)
723
{
724
        if (bTUIOMode)//If sending TUIO delete Blobs from map list
725
        {
726
                std::map<int, ofxTBetaCvBlob>::iterator iter;
727
                for (iter = myTUIO.blobs.begin(); iter != myTUIO.blobs.end(); iter++)
728
                {
729
                        if (iter->second.id == b.id)
730
                        {
731
                                myTUIO.blobs.erase(iter);
732
                                break;
733
                        }
734
                }
735
        }
736
}
737
738
void ofxNCoreVision::TouchMoved( ofxTBetaCvBlob b)
739
{
740
        if (bTUIOMode)//If sending TUIO, add the blob to the map list
741
        {
742
                //if blob is not otuside calibration mesh
743
                if (b.centroid.x != 0 && b.centroid.y != 0)
744
                        myTUIO.blobs[b.id] = b;
745
        }
746
}
747
748
/*****************************************************************************
749
*        MOUSE EVENTS
750
*****************************************************************************/
751
void ofxNCoreVision::mouseMoved(int x, int y)
752
{
753
}
754
755
void ofxNCoreVision::mouseDragged(int x, int y, int button)
756
{
757
        if (showConfiguration)
758
                gui->mouseDragged(x, y, button); //guilistener
759
}
760
761
void ofxNCoreVision::mousePressed(int x, int y, int button)
762
{
763
        if (showConfiguration)
764
        {
765
                gui->mousePressed(x, y, button); //guilistener
766
                if (x > ofGetWidth() - 230 && y > ofGetHeight() - 14){ofLaunchBrowser("http://www.openframeworks.cc/forum/viewtopic.php?p=9651#9651");}
767
        }
768
}
769
770
void ofxNCoreVision::mouseReleased()
771
{
772
        if (showConfiguration)
773
                gui->mouseReleased(mouseX, mouseY, 0); //guilistener
774
}
775
776
/*****************************************************************************
777
* ON EXIT
778
*****************************************************************************/
779
void ofxNCoreVision::exit()
780
{
781
        if(PS3!=NULL) delete PS3;
782
        // -------------------------------- SAVE STATE ON EXIT
783
        saveConfiguration();
784
785
        printf("Vision module has exited!\n");
786
}