summaryrefslogtreecommitdiff
path: root/gaunt01/src/testApp.cpp
blob: 3b81cdd9f261cb2961a1c1ac708972b420683dda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "testApp.h"

//--------------------------------------------------------------
//units ~ 10cm
//
void testApp::setup(){
	
	bLearnBakground = true;
	cam_angle=0;
	threshold = 80;
	
	loadSettings("settings.xml");

	#ifdef _USE_LIVE_VIDEO
        vidGrabber.setVerbose(true);
        vidGrabber.initGrabber(320,240);
	#else
        vidPlayer.loadMovie("camoutput.wmv"); //footage/ camera needs to be the same res as opencv planes and output
        vidPlayer.play();
	#endif

	colorImg.allocate(640,480);
	grayImage.allocate(640,480);
	grayBg.allocate(640,480);
	grayDiff.allocate(640,480);

	ofVec3f centre=ofVec3f(ofGetWidth()/2,0,0);
	ofVec3f normal=ofVec3f(0,0,-1);
	ray=ofRay();
	plane=ofPlane(centre,normal);
	plane.color=ofColor(255,255,255);

	projector=ofProjector(2.0f, ofVec2f(0.0f, 0.0f),ofGetWidth(),ofGetHeight()); //1.535f
	projector.setPosition(ofGetWidth()/2,ofGetHeight()/2,-ofGetWidth());
	projector.lookAt(ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0),ofVec3f(0, -1, 0));

	cam=ofCamera();
	cam.setPosition(ofGetWidth()/2,ofGetHeight()/2,-ofGetWidth());
	cam.lookAt(ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0),ofVec3f(0, -1, 0));
	cam.setFov(42.0); //39.85); //53.13);
	
	trapDoor=trapdoor(ofRectangle(0,ofGetHeight()/4,ofGetHeight(),ofGetHeight()),ofVec2f(20,20));
	
	
	
}

//--------------------------------------------------------------
void testApp::update(){
	ofBackground(100,100,100);

    bool bNewFrame = false;

	#ifdef _USE_LIVE_VIDEO
       vidGrabber.grabFrame();
	   bNewFrame = vidGrabber.isFrameNew();
    #else
        vidPlayer.idleMovie();
        bNewFrame = vidPlayer.isFrameNew();
	#endif

	if (bNewFrame){

		#ifdef _USE_LIVE_VIDEO
            colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
	    #else
            colorImg.setFromPixels(vidPlayer.getPixels(), 640,480);
        #endif

        grayImage = colorImg;
		if (bLearnBakground == true){
			grayBg = grayImage;		// the = sign copys the pixels from grayImage into grayBg (operator overloading)
			bLearnBakground = false;
		}

		// take the abs value of the difference between background and incoming and then threshold:
		grayDiff.absDiff(grayBg, grayImage);
		grayDiff.threshold(threshold);

		// find contours which are between the size of 20 pixels and 1/3 the w*h pixels.
		// also, find holes is set to true so we will get interior contours as well....
		
		//hard coded size threshold of 100 pix
		contourFinder.findContours(grayDiff, 200, (640*480)/3, 20, false);	// don't find holes
	}

}

//--------------------------------------------------------------
void testApp::draw(){

	/*
	// draw the incoming, the grayscale, the bg and the thresholded difference
	ofSetHexColor(0xffffff);
	colorImg.draw(20,20);
	grayImage.draw(360,20);
	grayBg.draw(20,280);
	grayDiff.draw(360,280);

	// then draw the contours:
 */
	ofFill();
	ofSetHexColor(0x333333);
	//ofRect(360,540,320,240);
	ofSetHexColor(0xa0a0a0);

	// we could draw the whole contour finder
	//contourFinder.draw(360,540);
	
	//keep running background average- how do you use a custom background


	colorImg.draw(0,0,ofGetWidth(),ofGetHeight());

	cam.begin();
	ofPushMatrix();

	ofRotate(cam_angle,1,0,0);
	//cam.Rotate(cam_angle,1,0,0);

	for (float i=0;i<=ofGetWidth();i+=ofGetWidth()/10) {
        glBegin(GL_LINES);
            glVertex3f(i,0,0);
            glVertex3f(i,ofGetWidth(),0);
        glEnd();
        glBegin(GL_LINES);
            glVertex3f(0,i,0);
            glVertex3f(ofGetWidth(),i,0);
        glEnd();
	}

	ofPopMatrix();
	

	//ofSphere(pos.x,pos.y,pos.z,5);
	
	vector<ofVec3f> newPlayers;
	float movethresh=10;

	for (int i = 0; i < contourFinder.nBlobs; i++){
		//do some bounds checking, size threshold and overlap removal
		//in order to translate blobs into players
		//attempt to track players - maintain ID no
		//attempt to base blobs
		//TODO attempt to estimate bounds shape
		//project all of this into the 3D space
		ofRectangle r=contourFinder.blobs[i].boundingRect;
		
		ofVec2f blobBase=ofVec2f(r.x+(r.width/2),r.y+r.height-(r.width/2));
		
		contourFinder.blobs[i].draw(0,0);
		ofPoint p=contourFinder.blobs[i].centroid;

		ofSetHexColor(0xffff00);
		
		char numStr[16];
		sprintf(numStr, "%i", i);
		ofDrawBitmapString(numStr, r.x, r.y);
		
		ofVec3f pp;
		ray=projector.castPixel(blobBase.x,blobBase.y); //(ofGetHeight()-y));
		bool hit = plane.intersect(ray,pp);	

		
		for (int i=0;i<players.size();i++) {
			if (abs(players[i].x-pp.x)<10&&abs(players[i].z-pp.z)) {
				//identified a player
				//this is flawed: tracking boxes in a skewed axis
				//how to get UV coords on the plane
				//maybe rotate the camera instead of the plane
				
			}
		}
	
		ofPushMatrix();
		ofTranslate(pp);
		ofRotate(cam_angle,1,0,0);
		ofBox(0,-10,0,20);
		//for now, draw in this axis
		ofPopMatrix();
	}
	
	trapDoor.checkUpdate(players);
	ofPushMatrix();
	ofRotate(cam_angle,1,0,0);
	//for now, draw in this axis
	trapDoor.draw();
	ofPopMatrix();
	    
	cam.end();



	// finally, a report:

	ofSetHexColor(0xffffff);
	char reportStr[1024];
	sprintf(reportStr, "threshold %i\nnum blobs found %i, fps: %f", threshold, contourFinder.nBlobs, ofGetFrameRate());
	ofDrawBitmapString(reportStr, 10, ofGetHeight()-40);
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){

	switch (key){
		case ' ':
			bLearnBakground = true;
			break;
		case '+':
			threshold ++;
			if (threshold > 255) threshold = 255;
			break;
		case '-':
			threshold --;
			if (threshold < 0) threshold = 0;
			break;
		case 'a':
			cam_angle+=1;
			break;
		case 'z':
			cam_angle-=1;
			break;
		case 's':
			saveSettings("settings.xml");
			break;
	}
	plane.setNormal(ofVec3f(0,sin(cam_angle*0.01745329),-cos(cam_angle*0.01745329)));
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
	ray=projector.castPixel(x,y); //+(ofGetHeight()/2)); //(ofGetHeight()-y));
	bool hit = plane.intersect(ray,pos);
	//pos=ofVec3f(pos.x*2,pos.y*2,pos.z*2);
	if (hit) printf("ray:%i,%i hit plane:%f,%f,%f\n",x,y,pos.x,pos.y,pos.z);
	else printf("ray:%i,%i missed plane\n",x,y);

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){

}

void testApp::loadSettings(string filename){
	if( !XML.loadFile(filename) ){
		printf("unable to load %s check data/ folder\n",filename.c_str());
	}else{
		cam_angle=ofToInt(XML.getAttribute("gauntlet","cam_angle","none",0));
		threshold=ofToInt(XML.getAttribute("gauntlet","threshold","none",0));
		printf("loaded %s\n",filename.c_str());
	}
}
//--------------------------------------------------------------
void testApp::saveSettings(string filename){
	XML.setAttribute("gauntlet","cam_angle",ofToString(cam_angle),0);
	XML.setAttribute("gauntlet","threshold",ofToString(threshold),0);
	XML.saveFile(filename);
	printf("saved %s\n",filename.c_str());
}