-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraPose.cpp
407 lines (343 loc) · 11.2 KB
/
CameraPose.cpp
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* CameraPose Class
* Used to collect, filter, and interpret camera data
* Written by Greg Seaman, Adam Park, and Ryan McNulty
*/
#include <robot_if++.h>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <math.h>
#include <cstdio>
#include "CameraPose.h"
#include "modified_color.h"
#include <list>
using namespace std;
/*
* Initializes the CameraPose to handle camera based navigation
* Configures camera and initializes images and display windows
*/
CameraPose::CameraPose(RobotInterface *r){
robot = r;
robot->Move(RI_HEAD_MIDDLE,1);
//Initializes images for storing most recent camera data
cameraImage = cvCreateImage(cvSize(SCREEN_WIDTH, SCREEN_HEIGHT), IPL_DEPTH_8U, 3);
hsvImage = cvCreateImage(cvSize(SCREEN_WIDTH, SCREEN_HEIGHT), IPL_DEPTH_8U, 3);
filteredImage = cvCreateImage(cvSize(SCREEN_WIDTH, SCREEN_HEIGHT), IPL_DEPTH_8U, 1);
pinkLow = cvCreateImage(cvSize(SCREEN_WIDTH, SCREEN_HEIGHT), IPL_DEPTH_8U, 1);
pinkHigh = cvCreateImage(cvSize(SCREEN_WIDTH, SCREEN_HEIGHT), IPL_DEPTH_8U, 1);
yellow = cvCreateImage(cvSize(SCREEN_WIDTH, SCREEN_HEIGHT), IPL_DEPTH_8U, 1);
//Setup display windows
//cvNamedWindow("Unfiltered", CV_WINDOW_AUTOSIZE);
//cvNamedWindow("Filtered Pink High", CV_WINDOW_AUTOSIZE);
//cvNamedWindow("Filtered Pink Low", CV_WINDOW_AUTOSIZE);
//cvNamedWindow("Filtered Yellow", CV_WINDOW_AUTOSIZE);
//cvNamedWindow("Filtered", CV_WINDOW_AUTOSIZE);
//cvNamedWindow("All Filtered", CV_WINDOW_AUTOSIZE);
// Setup the camera
if(robot->CameraCfg(RI_CAMERA_DEFAULT_BRIGHTNESS, RI_CAMERA_DEFAULT_CONTRAST, 5, RI_CAMERA_RES_640, RI_CAMERA_QUALITY_HIGH)) {
std::cout << "Failed to configure the camera!" << std::endl;
exit(-1);
}
}
CameraPose::~CameraPose(){}
/*
* Updates robot and gets current image & manipulates them
*/
list<squarePair> CameraPose::updateCamera(){
// Update the robot's sensor information
robot->update();
// Get the current camera image
robot->getImage(cameraImage);
// Convert to hsv
cvCvtColor(cameraImage, hsvImage, CV_BGR2HSV);
turnError = 0;
//Process squares
squares_t * currentSquares;
list<squarePair> yellowPairs, pinkPairs, allPairs;
//Filter and find yellow squares
cvInRangeS(hsvImage, RC_YELLOW_LOW, RC_YELLOW_HIGH, yellow);
currentSquares = robot->findSquares(yellow, MIN_SQUARE);
//Match and display yellow squares
removeOverlap(currentSquares);
turnError += getSquareSide(currentSquares);
drawSquares(currentSquares, CV_RGB(0,255,0));
yellowPairs = matchSquares(currentSquares, YELLOW);
printCenters(yellowPairs);
//Filter and find pink squares
cvInRangeS(hsvImage, RC_PINK1_LOW, RC_PINK1_HIGH, pinkLow);
cvInRangeS(hsvImage, RC_PINK2_LOW, RC_PINK2_HIGH, pinkHigh);
cvOr(pinkLow, pinkHigh, filteredImage, NULL);
currentSquares = robot->findSquares(filteredImage, MIN_SQUARE);
//Match and display pink squares
removeOverlap(currentSquares);
turnError += getSquareSide(currentSquares);
drawSquares(currentSquares, CV_RGB(255,0,0));
pinkPairs = matchSquares(currentSquares, PINK);
printCenters(pinkPairs);
displayImages();
//Save image
char file_name [256];
sprintf(file_name,"camera.%04d.jpg", image_ctr);
cvSaveImage(file_name,cameraImage);
sprintf(file_name,"filtered.%04d.jpg", image_ctr);
cvSaveImage(file_name,filteredImage);
sprintf(file_name,"yellow.%04d.jpg",image_ctr);
cvSaveImage(file_name,yellow);
/*
sprintf(file_name,"pinklow.%04d.jpg", image_ctr);
cvSaveImage(file_name,pinkLow);
sprintf(file_name,"pinkhigh.%04d.jpg", image_ctr);
cvSaveImage(file_name,pinkHigh); */
image_ctr+=1;
printf("Image %d\n", image_ctr);
//Return both yellow and pink pairs
yellowPairs.splice(yellowPairs.end(), pinkPairs);
allPairs = yellowPairs;
//If no pairs found strafe left then right to search for more
/*
static int retry_count = 0;
retry_count+=1;
if(allPairs.size()==0 && retry_count<3){
if (retry_count==1){
robot->Move(RI_MOVE_FWD_LEFT,1);
}
else if(retry_count==2){
robot->Move(RI_MOVE_FWD_RIGHT,1);
}
updateCamera();
}
else
retry_count = 0;
*/
return allPairs;
}
/*
* Displays current images - filtered and unfiltered
*/
void CameraPose::displayImages(){
cvShowImage("Unfiltered", cameraImage);
//cvShowImage("Filtered", filteredImage);
//cvShowImage("Filtered Pink High", pinkHigh);
//cvShowImage("Filtered Pink Low", pinkLow);
//cvShowImage("Filtered Yellow", yellow);
//cvShowImage("Filtered All", filteredImage);
cvWaitKey(100);
}
/*
* Draw boxes around squares found in robot->findSquares
* draw in color displayColor
*/
void CameraPose::drawSquares(squares_t *squares, CvScalar displayColor){
CvPoint pt1, pt2;
while(squares != NULL) {
int sq_amt = (int) (sqrt(squares->area) / 2);
// Upper Left to Lower Left
pt1.x = squares->center.x - sq_amt;
pt1.y = squares->center.y - sq_amt;
pt2.x = squares->center.x - sq_amt;
pt2.y = squares->center.y + sq_amt;
cvLine(cameraImage, pt1, pt2, displayColor, 2, CV_AA, 0);
// Lower Left to Lower Right
pt1.x = squares->center.x - sq_amt;
pt1.y = squares->center.y + sq_amt;
pt2.x = squares->center.x + sq_amt;
pt2.y = squares->center.y + sq_amt;
cvLine(cameraImage, pt1, pt2, displayColor, 2, CV_AA, 0);
// Upper Left to Upper Right
pt1.x = squares->center.x - sq_amt;
pt1.y = squares->center.y - sq_amt;
pt2.x = squares->center.x + sq_amt;
pt2.y = squares->center.y - sq_amt;
cvLine(cameraImage, pt1, pt2, displayColor, 2, CV_AA, 0);
// Lower Right to Upper Right
pt1.x = squares->center.x + sq_amt;
pt1.y = squares->center.y + sq_amt;
pt2.x = squares->center.x + sq_amt;
pt2.y = squares->center.y - sq_amt;
cvLine(cameraImage, pt1, pt2, displayColor, 2, CV_AA, 0);
squares = squares->next;
}
}
/*
* Comparison function by area for list sort to use
*/
bool compare_areas (squarePair first, squarePair second){
if((first.left->area + first.right->area) > (second.left->area + second.right->area)) return true;
else return false;
}
/*
* Removes the squares that overlap by comparing the centers
*/
void CameraPose::removeOverlap(squares_t *squares){
squares_t *current;
squares_t *last;
while(squares != NULL){
last = squares;
current = squares->next;
while(current != NULL){
//Test if x and y values are close
if(abs(squares->center.y - current->center.y) < SCREEN_HEIGHT/18
&& abs(squares->center.x - current->center.x) < SCREEN_WIDTH/18){
last->next = current->next;
}
current = current->next;
}
squares = squares->next;
}
}
/*
* Find squares of same height and draw lines between them in specified color
*/
list<squarePair> CameraPose::matchSquares(squares_t *squares, int color){
squares_t *tempSquares;
list <squarePair> pair_list;
squarePair temp_pair;
temp_pair.left = NULL; temp_pair.right = NULL;
while(squares != NULL){
tempSquares = squares->next;
while(tempSquares != NULL){
//Test if y values are close, if x values are far, and if centers in top 3/5 of screen
if(abs(squares->center.y - tempSquares->center.y) < SCREEN_HEIGHT/15
&& abs(squares->center.x - tempSquares->center.x) > SCREEN_WIDTH/6
&& (squares->center.y) < SCREEN_HEIGHT * (3.0/5.0)
&& (tempSquares->center.y) < SCREEN_HEIGHT * (3.0/5.0)){
//Record squares
int newArea = (squares->area + tempSquares->area)/2;
if(squares->center.x < tempSquares->center.x){
temp_pair.left = squares;
temp_pair.right = tempSquares;
}
else{
temp_pair.left = tempSquares;
temp_pair.right = squares;
}
temp_pair.color = color;
pair_list.push_back(temp_pair);
break;
}
tempSquares = tempSquares->next;
}
squares = squares->next;
}
//Sort the squarePairs largest area to smallest
pair_list.sort(compare_areas);
return pair_list;
}
/*
* Draws a line between the squarePairs & puts a tick at the line's midpoint
*/
void CameraPose::printCenters(list<squarePair> pairs){
list<squarePair>::iterator it;
cvLine(cameraImage, cvPoint(SCREEN_WIDTH/2,0), cvPoint(SCREEN_WIDTH/2,SCREEN_HEIGHT), CV_RGB(128,128,128), 2, CV_AA, 0);
int center = 0;
int height = 0;
CvPoint pt1, pt2;
for(it=pairs.begin(); it!=pairs.end(); it++){
//Draw connecting line
pt1 = cvPoint(it->left->center.x, it->left->center.y);
pt2 = cvPoint(it->right->center.x, it->right->center.y);
cvLine(cameraImage, pt1, pt2, CV_RGB(0,0,255), 2, CV_AA, 0);
//Draw midpoint
center = (it->left->center.x + it->right->center.x)/2;
height = (it->left->center.y + it->right->center.y)/2;
cvLine(cameraImage, cvPoint(center,height-5), cvPoint(center,height+5), CV_RGB(255,0,0), 2, CV_AA, 0);
}
}
/*
* For the largest pair of squares,
* Return the distance their midpoint is from the screen's center
*/
int CameraPose::getCenterError(list<squarePair> pairs){
list<squarePair>::iterator it;
int centers = 0;
if(pairs.size() == 0)
return 0;
else{
list<squarePair>::iterator it = pairs.begin();
centers = (it->left->center.x + it->right->center.x)/2;
if(pairs.size() > 1){
it++;
}
return centers - (SCREEN_WIDTH/2);
}
// return (centers/pairs.size()) - (SCREEN_WIDTH/2);
}
/*
* Returns the average center x position to be used for turning
*/
int CameraPose::getSquareSide(squares_t *squares){
if(squares == NULL)
return 0;
else{
squares_t * current = squares;
int center = 0;
int num = 0;
while(current != NULL){
center += current->center.x;
current = current->next;
num++;
}
if(num == 0)
return 0;
else
return center/num - SCREEN_WIDTH/2;
}
}
/*
* Returns the turn error if pairs are found
*/
int CameraPose::getTurnError(list<squarePair> pairs){
list<squarePair>::iterator it;
if(pairs.size() == 0){
return turnError;
}
else{
list<squarePair>::iterator it = pairs.begin();
int left_error = abs(it->left->center.x - SCREEN_WIDTH/2);
int right_error = abs(it->right->center.x - SCREEN_WIDTH/2);
//return left_error - right_error;
return 0;
}
}
/*
* Find the distance to the next cell based on where it finds squarePairs in the image
* -based on the location and size of squarePairs
*/
int CameraPose::getCellError(list<squarePair> pairs){
list<squarePair>::iterator it = pairs.begin();
int error = 0;
if(pairs.size() == 0)
return 0;
else{
if(it->color == PINK){
error += it->left->center.y - Y_CELL_CENTER_PINK;
error += it->right->center.y - Y_CELL_CENTER_PINK;
printf("PINK left x %d, right x %d\n", it->left->center.x, it->right->center.x);
}else if(it->color == YELLOW){
error += it->left->center.y - Y_CELL_CENTER_YELLOW;
error += it->right->center.y - Y_CELL_CENTER_YELLOW;
printf("Yellow left x %d, right x %d\n", it->left->center.x, it->right->center.x);
}
return error/2;
}
}
int CameraPose::getCellErrorX(list<squarePair> pairs){
list<squarePair>::iterator it = pairs.begin();
int error = 0;
if(pairs.size() == 0)
return 0;
else{
if(it->color == PINK){
error += it->left->center.x - X_CELL_CENTER_LEFT;
error += it->left->center.x - X_CELL_CENTER_RIGHT;
printf("PINK left x %d, right x %d\n", it->left->center.x, it->right->center.x);
}else if(it->color == YELLOW){
error += it->left->center.x - X_CELL_CENTER_LEFT;
error += it->left->center.x - X_CELL_CENTER_RIGHT;
printf("Yellow left x %d, right x %d\n", it->left->center.x, it->right->center.x);
}
return error/2;
}
}