-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
152 lines (119 loc) · 4.52 KB
/
main.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
//opencv
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <NetSend.hpp>
using namespace cv;
/** Global Variables */
const int max_value_H = 360/2;
const int max_value = 255;
const int max_value_R = 20;
const String window_capture_name = "Video Capture";
const String window_detection_name = "Object Detection";
const String window_slider_name = "Slider Window";
int low_H = 50, low_S = 0, low_V = 0;
int high_H = max_value_H, high_S = max_value, high_V = max_value;
//! [low]
static void on_low_H_thresh_trackbar(int, void *) {
low_H = min(high_H-1, low_H);
setTrackbarPos("Low H", window_slider_name, low_H);
}
//! [low]
//! [high]
static void on_high_H_thresh_trackbar(int, void *) {
high_H = max(high_H, low_H+1);
setTrackbarPos("High H", window_slider_name, high_H);
}
//! [high]
static void on_low_S_thresh_trackbar(int, void *) {
low_S = min(high_S-1, low_S);
setTrackbarPos("Low S", window_slider_name, low_S);
}
static void on_high_S_thresh_trackbar(int, void *) {
high_S = max(high_S, low_S+1);
setTrackbarPos("High S", window_slider_name, high_S);
}
static void on_low_V_thresh_trackbar(int, void *) {
low_V = min(high_V-1, low_V);
setTrackbarPos("Low V", window_slider_name, low_V);
}
static void on_high_V_thresh_trackbar(int, void *) {
high_V = max(high_V, low_V+1);
setTrackbarPos("High V", window_slider_name, high_V);
}
int main(int argc, char* argv[]) {
NetSend infoSender(8080);
infoSender.sendData();
//! [cap]
VideoCapture cap(argc > 1 ? atoi(argv[1]) : 0);
//! [cap]
//! [window]
namedWindow(window_capture_name);
namedWindow(window_detection_name);
namedWindow(window_slider_name);
//! [window]
//! [trackbar]
// Trackbars to set thresholds for HSV values
createTrackbar("Low H", window_slider_name, &low_H, max_value_H, on_low_H_thresh_trackbar);
createTrackbar("High H", window_slider_name, &high_H, max_value_H, on_high_H_thresh_trackbar);
createTrackbar("Low S", window_slider_name, &low_S, max_value, on_low_S_thresh_trackbar);
createTrackbar("High S", window_slider_name, &high_S, max_value, on_high_S_thresh_trackbar);
createTrackbar("Low V", window_slider_name, &low_V, max_value, on_low_V_thresh_trackbar);
createTrackbar("High V", window_slider_name, &high_V, max_value, on_high_V_thresh_trackbar);
//! [trackbar]
Mat frame, frame_HSV, frame_threshold, frame_contours;
while (true) {
//! [while]
cap >> frame;
if(frame.empty()) {
break;
}
// Convert from BGR to HSV colorspace
cvtColor(frame, frame_HSV, COLOR_BGR2HSV);
// Detect the object based on HSV Range Values
inRange(frame_HSV, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), frame_threshold);
//! [while]
// erode + dialate
Mat kernal = Mat();
dilate(frame_threshold, frame_threshold, kernal, Point(-1, -1), 10);
erode(frame_threshold, frame_threshold, kernal, Point(-1, -1), 10);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
frame_contours = frame_threshold.clone();
findContours(frame_contours, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector<Point> largestContour;
double largestArea = 0;
for(int i = 0; i < contours.size(); i++) {
Scalar color = Scalar(0, 0, 255);
double area = contourArea(contours[i]);
if (area > largestArea) {
largestContour = contours[i];
largestArea = area;
}
RotatedRect rect = minAreaRect(contours[i]);
Point2f rect_points[4]; rect.points( rect_points );
for( int j = 0; j < 4; j++ )
line(frame, rect_points[j], rect_points[(j+1)%4], Scalar(0, 255, 0), 5, 8);
}
if (largestArea > 0) {
RotatedRect rect = minAreaRect(largestContour);
Point2f rect_points[4]; rect.points( rect_points );
for( int j = 0; j < 4; j++ )
line(frame, rect_points[j], rect_points[(j+1)%4], Scalar(0, 0, 255), 5, 8 );
Point2f center = rect.center;
circle(frame, Point((int) center.x, (int) center.y), 4, Scalar(0, 0, 255), -1);
infoSender.sendData(center.x, center.y);
}
//! [show]
// Show the frames
imshow(window_capture_name, frame);
imshow(window_detection_name, frame_threshold);
//! [show]
char key = (char) waitKey(30);
if (key == 'q' || key == 27) {
break;
}
}
infoSender.closePort();
return 0;
}