-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSource.cpp
More file actions
599 lines (531 loc) · 14.1 KB
/
Source.cpp
File metadata and controls
599 lines (531 loc) · 14.1 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int val[3];
enum GrayAction { g_none, g_cvt, g_hue, g_sat, g_val, g_hue_val};
enum BinarizeAction { b_none, b_threshold, b_rgb, b_hsv, b_otsu };
enum FeatureAction { f_none, f_binarize, f_canny, f_action_lines, f_diff, f_harris, f_pretzel, f_otsu, f_hsvHue, f_hsvSat, f_hsvVal, f_contours};
GrayAction g_action = g_none;
BinarizeAction b_action = b_none;
FeatureAction f_action = f_none;
bool displayHist;
bool gaussianSmoothing;
bool erodeDilate;
RNG rng(12345);
#define ENTER 13
#define LEFT_ARROW 2424832
#define UP_ARROW 2490368
#define RIGHT_ARROW 2555904
#define DOWN_ARROW 2621440
#define MIN_IMG_NUMBER 1
#define MAX_IMG_NUMBER 24
#define STARTING_IMG_NUMBER 1
Mat img, imgHsv;
int imageNumber;
Point2i ballCenter(260, 175);
int roiAbove = 20;
int roiSide = 70;
int roiBelow = 170;
void processImage();
void loadImage(Mat& img, int imageNumber)
{
char name[20];
sprintf(name, "img (%d).bmp", imageNumber);
string str_name(name);
img = imread(str_name);
}
void showHistogram(Mat& img)
{
int bins = 256; // number of bins
int nc = img.channels(); // number of channels
vector<Mat> hist(nc); // histogram arrays
// Initalize histogram arrays
for (int i = 0; i < hist.size(); i++)
hist[i] = Mat::zeros(1, bins, CV_32SC1);
// Calculate the histogram of the image
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
for (int k = 0; k < nc; k++)
{
uchar val = nc == 1 ? img.at<uchar>(i, j) : img.at<Vec3b>(i, j)[k];
hist[k].at<int>(val) += 1;
}
}
}
// For each histogram arrays, obtain the maximum (peak) value
// Needed to normalize the display later
int hmax[3] = { 0,0,0 };
for (int i = 0; i < nc; i++)
{
for (int j = 0; j < bins - 1; j++)
hmax[i] = hist[i].at<int>(j) > hmax[i] ? hist[i].at<int>(j) : hmax[i];
}
const char* wname[3] = { "blue", "green", "red" };
Scalar colors[3] = { Scalar(255,0,0), Scalar(0,255,0), Scalar(0,0,255) };
vector<Mat> canvas(nc);
// Display each histogram in a canvas
for (int i = 0; i < nc; i++)
{
canvas[i] = Mat::ones(125, bins, CV_8UC3);
for (int j = 0, rows = canvas[i].rows; j < bins - 1; j++)
{
line(
canvas[i],
Point(j, rows),
Point(j, rows - (hist[i].at<int>(j) * rows / hmax[i])),
nc == 1 ? Scalar(200, 200, 200) : colors[i],
1, 8, 0
);
}
imshow(nc == 1 ? "value" : wname[i], canvas[i]);
}
}
string cmd = "";
void pressKey(int key)
{
if (key == LEFT_ARROW)
{
imageNumber -= 1;
if (imageNumber < MIN_IMG_NUMBER)
imageNumber = MAX_IMG_NUMBER;
}
else if (key == RIGHT_ARROW)
{
imageNumber += 1;
if (imageNumber > MAX_IMG_NUMBER)
imageNumber = MIN_IMG_NUMBER;
}
else if (key == ENTER)
{
// Grayscale commands
if (cmd == "gn")
g_action = g_none;
else if (cmd == "gh")
g_action = g_hue;
else if (cmd == "gs")
g_action = g_sat;
else if (cmd == "gv")
g_action = g_val;
else if (cmd == "gc")
g_action = g_cvt;
else if (cmd == "ghv")
g_action = g_hue_val;
// Binarization commands
else if (cmd == "bn")
b_action = b_none;
else if (cmd == "bt")
b_action = b_threshold;
else if (cmd == "brgb")
b_action = b_rgb;
else if (cmd == "bhsv")
b_action = b_hsv;
else if (cmd == "bo")
b_action = b_otsu;
// Smoothing commands
else if (cmd == "ed")
erodeDilate = !erodeDilate;
else if (cmd == "g")
gaussianSmoothing = !gaussianSmoothing;
// Feature commands
else if (cmd == "none")
f_action = f_none;
else if (cmd == "c")
f_action = f_contours;
else if (cmd == "t")
f_action = f_pretzel;
else if (cmd == "b")
f_action = f_binarize;
else if (cmd == "u")
f_action = f_otsu;
else if (cmd == "c")
f_action = f_canny;
else if (cmd == "l")
f_action = f_action_lines;
else if (cmd == "d")
f_action = f_diff;
else if (cmd == "h")
f_action = f_hsvHue;
else if (cmd == "s")
f_action = f_hsvSat;
else if (cmd == "v")
f_action = f_hsvVal;
else if (cmd == "save")
imwrite("test.png", img);
else if (cmd == "hist")
displayHist = !displayHist;
cmd = "";
cout << endl << ">";
}
else if (key >= 'a' && key <= 'z')
{
cmd = cmd + ((char)key);
cout << ((char)key);
}
else if (key != -1)
printf("%d\n", key);
}
Point2f calcLineIntersection(Size imgSize, float rho, float theta, bool vertical, bool dir)
{
int dirInt = dir ? 1 : -1;
float dy1 = -cos(theta)*dirInt;
float dx1 = sin(theta)*dirInt;
Point2f point(rho*cos(theta), rho*sin(theta));
if (vertical)
{
float dx2 = (dx1 < 0 ? 0 : imgSize.width) - point.x;
int dy2 = dy1 * dx2 / dx1;
return Point(point.x + dx2, point.y + dy2);
}
else
{
float dy2 = (dy1 < 0 ? 0 : imgSize.height) - point.y;
int dx2 = dx1 * dy2 / dy1;
return Point(point.x + dx2, point.y + dy2);
}
}
Point calcRectIntersection(Size imgSize, float rho, float theta, bool dir)
{
Point p1 = calcLineIntersection(imgSize, rho, theta, true, dir);
if (p1.x >= 0 && p1.y >= 0 && p1.x <= imgSize.width && p1.y <= imgSize.height)
return p1;
else
return calcLineIntersection(imgSize, rho, theta, false, dir);
}
void calcCrop(Size& imgSize, vector<Vec2f>& lines, vector<Point2f>& pts, Rect& roi)
{
Mat edgesC;
int roiX1 = 0;
int roiX2 = imgSize.width;
for (int i = 0; i < lines.size(); i++)
{
float rho = lines[i][0], theta = lines[i][1];
Point2f p1 = calcRectIntersection(imgSize, rho, theta, true);
Point2f p2 = calcRectIntersection(imgSize, rho, theta, false);
if (p1.x < imgSize.width / 2)
{
if (p1.x > roiX1)
roiX1 = p1.x;
}
else
if (p1.x < roiX2)
roiX2 = p1.x;
if (p2.x < imgSize.width / 2)
{
if (p2.x > roiX1)
roiX1 = p2.x;
}
else
if (p2.x < roiX2)
roiX2 = p2.x;
pts.push_back(p1);
pts.push_back(p2);
}
Rect roi2(roiX1, 0, roiX2 - roiX1, imgSize.height);
roi = roi2;
}
void drawCrop(Mat& img, vector<Point2f> pts, Rect& roi)
{
for (int i = 0; i < pts.size(); i += 2)
{
line(img, pts[i], pts[i + 1], pts[i].x > img.size().width / 2 ? Scalar(0, 0, 255) : Scalar(255, 0, 0), 1, CV_AA);
}
rectangle(img, roi, Scalar(0, 255, 0), 1, CV_AA);
}
void getChannel(Mat& img, Mat& imgChannel, int channel)
{
Mat channels[3];
split(img, channels);
imgChannel = channels[channel];
}
void setHsv(Mat& before, Mat& hsv, Mat& after, int channel)
{
vector<Mat> channels;
cvtColor(before, hsv, CV_BGR2HSV);
split(hsv, channels);
if (channel != 0)
channels[0].setTo(179);
if (channel != 1)
channels[1].setTo(255);
if (channel != 2)
channels[2].setTo(255);
merge(channels, hsv);
cvtColor(hsv, after, CV_HSV2BGR);
}
void channelDist(Mat& hsv, Mat& dist, int hueVal, int channel)
{
Mat channelImg;
getChannel(hsv, channelImg, channel);
if (channel == 0)
{
Mat dist1, dist2;
absdiff(channelImg, hueVal, dist1);
absdiff(channelImg, hueVal + ((hueVal < 90) ? 180 : -180), dist2);
min(dist1, dist2, dist);
}
else
absdiff(channelImg, hueVal, dist);
}
void processImage()
{
static Mat frame2;
loadImage(img, imageNumber);
// Crop the image
/// Convert the image to grayscale
Mat gray;
cvtColor(img, gray, CV_BGR2GRAY);
Mat detectedEdges;
/// Reduce noise with a kernel 3x3
blur(gray, detectedEdges, Size(3, 3));
/// Canny detector
int val2 = 30;
Canny(detectedEdges, detectedEdges, val2, val2 * 3, 3);
vector<Vec2f> lines;
HoughLines(detectedEdges, lines, 1, CV_PI / 180, 150);
Mat edgesC;
cvtColor(detectedEdges, edgesC, CV_GRAY2BGR);
vector<Point2f> pts;
Rect roi;
calcCrop(img.size(), lines, pts, roi);
Mat img2(img, roi);
if (imageNumber != 16)
img = img2;
//Apply grayscale
if (g_action == g_hue)
{
cvtColor(img, imgHsv, CV_BGR2HSV);
channelDist(imgHsv, img, 16, 0);
bitwise_not(img, img);
}
else if (g_action == g_sat)
{
cvtColor(img, imgHsv, CV_BGR2HSV);
getChannel(imgHsv, img, 1);
}
else if (g_action == g_val)
{
cvtColor(img, imgHsv, CV_BGR2HSV);
getChannel(imgHsv, img, 2);
}
else if (g_action == g_hue_val)
{
Mat d0, d2;
cvtColor(img, imgHsv, CV_BGR2HSV);
channelDist(imgHsv, d0, 16, 0);
bitwise_not(d0, d0);
getChannel(imgHsv, d2, 2);
addWeighted(d0, .5, d2, .5, 0, img);
}
//Gaussian Smoothing
if(gaussianSmoothing && img.channels() == 1)
GaussianBlur(img, img, Size(3, 3), 0);
//Binarize
if(b_action == b_threshold && img.channels() == 1)
cv::threshold(img, img, val[0], 255, CV_THRESH_BINARY);
else if (b_action == b_rgb && img.channels() == 3)
inRange(img, Scalar(val[0], val[1], val[2]), Scalar(255, 255, 255), img);
else if (b_action == b_hsv && img.channels() == 3)
{
cvtColor(img, imgHsv, CV_BGR2HSV);
inRange(imgHsv, Scalar(val[0], val[1], val[2]), Scalar(255, 255, 255), img);
}
else if (b_action == b_otsu && img.channels() == 1)
cv::threshold(img, img, 60, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
//Erode and Dilate
if(erodeDilate && img.channels() == 1)
{
erode(img, img, getStructuringElement(MORPH_CROSS, Size(2 * 2 + 1, 2 * 2 + 1), Point(2, 2)));
dilate(img, img, getStructuringElement(MORPH_CROSS, Size(2 * 4 + 1, 2 * 4 + 1), Point(4, 4)));
}
//Other actions
if (f_action == f_pretzel && img.channels() == 1)
{
//Find contours
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(img, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
img = Mat::zeros(img.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++)
drawContours(img, contours, i, Scalar(255, 0, 0));
// Find parent contour
int pretzelContourIndex = -1;
int minPretzelSize = 1000;
int largestContourSize = minPretzelSize;
for (int i = 0; i < hierarchy.size(); i++)
{
if (hierarchy[i][3] == -1)
{
Moments mm = moments((Mat)contours[i]);
if (mm.m00 > largestContourSize)
{
pretzelContourIndex = i;
largestContourSize = mm.m00;
printf("Size: %d\n", (int)mm.m00);
}
}
}
int pretzelSize = largestContourSize;
// Evaluate pretzel based on contour children
int minHoleSize = 10;
int pass = -1;
if (pretzelContourIndex != -1)
{
// Find center of mass
Moments mm = moments((Mat)contours[pretzelContourIndex]);
double centerX = (mm.m10 / mm.m00);
double centerY = (mm.m01 / mm.m00);
circle(img, Point(centerX, centerY), 4, Scalar(0, 255, 0));
int borderSize = 100;
if (centerY > borderSize && centerY < img.size().height - borderSize)
{
int numberOfHoles = 0;
int child = hierarchy[pretzelContourIndex][2];
while (child != -1)
{
if (contours[child].size() > minHoleSize)
numberOfHoles++;
child = hierarchy[child][0];
}
if (numberOfHoles <= 1)
pass = 2;
else if (numberOfHoles == 2)
pass = 1;
else if (numberOfHoles == 3)
pass = 0;
}
}
if (pass == -1)
printf("None\n");
else if (pass == 0)
printf("Good\n");
else if (pass == 1)
printf("Bad\n");
else if (pass == 2)
printf("Ugly\n");
}
else if (f_action == f_hsvHue && img.channels() == 3)
setHsv(img, imgHsv, img, 0);
else if (f_action == f_hsvSat && img.channels() == 3)
setHsv(img, imgHsv, img, 1);
else if (f_action == f_hsvVal && img.channels() == 3)
setHsv(img, imgHsv, img, 2);
else if (f_action == f_canny)
{
// Convert the image to grayscale
Mat gray;
cvtColor(img, gray, CV_BGR2GRAY);
Mat detectedEdges;
// Reduce noise with a kernel 3x3
blur(gray, detectedEdges, Size(3, 3));
// Canny detector
Canny(detectedEdges, detectedEdges, val[0], val[0] * 3, 3);
img = detectedEdges;
}
else if (f_action == f_action_lines)
{
img = edgesC;
drawCrop(img, pts, roi);
}
else if (f_action == f_diff)
{
if (frame2.data)
{
img = img.clone(); // Mat::zeros(frame.size(), frame.type());
absdiff(img, frame2, img);
}
frame2 = img.clone();
}
else if (f_action == f_harris)
{
/// Convert the image to grayscale
Mat gray;
Mat dst, dst_norm, dst_norm_scaled;
cvtColor(img, gray, CV_BGR2GRAY);
if (val[0] < 1)
val[0] = 1;
/// Parameters for Shi-Tomasi algorithm
vector<Point2f> corners;
double qualityLevel = 0.01;
double minDistance = 10;
int blockSize = 3;
bool useHarrisDetector = false;
double k = 0.04;
/// Copy the source image
Mat copy;
copy = img.clone();
/// Apply corner detection
goodFeaturesToTrack(gray, corners, val[0], qualityLevel, minDistance, Mat(), blockSize, useHarrisDetector, k);
/// Draw corners detected
int r = 4;
for (int i = 0; i < corners.size(); i++)
{
circle(copy, corners[i], r, Scalar(rng.uniform(0, 255), rng.uniform(0, 255),
rng.uniform(0, 255)), -1, 8, 0);
}
img = copy;
/// Set the neeed parameters to find the refined corners
Size winSize = Size(5, 5);
Size zeroZone = Size(-1, -1);
TermCriteria criteria = TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 40, 0.001);
/// Calculate the refined corner locations
cornerSubPix(gray, corners, winSize, zeroZone, criteria);
/// Write them down
//for (int i = 0; i < corners.size(); i++)
//{
// cout << " -- Refined Corner [" << i << "] (" << corners[i].x << "," << corners[i].y << ")" << endl;
//}
}
imshow("White", img);
if(displayHist)
showHistogram(img);
}
void trackbarCallback(int, void*)
{
processImage();
}
void mouseCallback(int event, int x, int y, int flags, void* userdata)
{
if (event == EVENT_LBUTTONDOWN)
{
if (imgHsv.empty())
{
printf("empty\n");
return;
}
Vec3b v = imgHsv.at<Vec3b>(y, x);
printf("%d %d %d\n", v[0], v[1], v[2]);
//Mat dist;
//channelDist(imgHsv, dist, imgHsv.at<Vec3b>(y, x)[0], 0);
//imshow("White", dist);
}
}
int main(int argc, char** argv)
{
VideoWriter VOut;
imageNumber = STARTING_IMG_NUMBER;
namedWindow("White", CV_WINDOW_AUTOSIZE);
setMouseCallback("White", mouseCallback, NULL);
createTrackbar("H", "White", &val[0], 255, trackbarCallback);
createTrackbar("S", "White", &val[1], 255, trackbarCallback);
createTrackbar("V", "White", &val[2], 255, trackbarCallback);
while (true)
{
//Check for keyboard input
int key = waitKey(0);
if (key == 27) //Escape
return 0;
else
pressKey(key);
processImage();
}
//if (!VOut.isOpened())
//{
// VOut.open("VideoOut.avi", CV_FOURCC('M', 'P', 'E', 'G'), 10, img.size(), 1);
// printf("Opening video stream with size %d %d\n", img.size().width, img.size().height);
//}
//VOut << img;
}