root / branches / tbeta / Windows-PS3EyeMuticam / addons / ofxNCore / src / Modules / ofxNCoreVision.cpp @ 186

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