-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetection.cpp
224 lines (209 loc) · 7.29 KB
/
detection.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
/**
@file detection.h
@project Wind Turbine detection
@author Wytse de Witte
@email [email protected]
@date 05-11-2020
*/
//class header
#include <fstream>
#include "detection.hpp"
//Namespace
using namespace cv;
using namespace cv::xfeatures2d;
Detector::Detector() {
apertureSize = 3;
houghThreshold = 50;
houghMinLength = 100;
houghMaxLineGap = 15;
}
int Detector::capture() {
// Declare the output variables
Mat frame;
/*
// Loads an image //
frame = imread( "images/20.jpg", IMREAD_COLOR );
// Check if image is loaded fine
if(frame.empty()){
printf(" Error opening image\n");
return -1;
}*/
/*
// manual video //
VideoCapture cap("videos/1.mp4"); // open the default camera
if(!cap.isOpened()) { // check if we succeeded
std::cout << "cannot open camera "<< std::endl;
return -1;
}
-- GRAB AND WRITE LOOP
std::cout << "Start grabbing" << std::endl
<< "Press any key to terminate" << std::endl;
*/
//-------Video Feed-------//
// open the default camera using default API
cap.open(0);
/// if you want to display videos
// VideoCapture cap("videos/mini3.mp4");
// OR advance usage: select any API backend
// int deviceID = 1; // 0 = open default camera
// int apiID = cv::CAP_ANY; // 0 = autodetect default API
// // open selected camera using selected API
// cap.open(deviceID, apiID);
// check if we succeeded
if (!cap.isOpened()) { // check if we succeeded
std::cout << "cannot open camera " << std::endl;
return -1;
}
for (;;) {
//cap >> frame; // get a new frame from camera
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
fps = cap.get(CAP_PROP_POS_FRAMES); // retrieves the current frame number
// check if we succeeded
if (frame.empty()) {
std::cerr << "ERROR! blank frame grabbed\n";
break;
}
//Run the detector (detect houghlines)
detect(frame);
// Display image.
cv::imshow("Output", frame);
if (waitKey(5) >= 0)
break;
}
// Wait and Exit
waitKey(0);
return 0;
}
void Detector::detect(Mat frame) {
//-------Masking with HSV----------//
Mat HSV, mask, HSVmasked;
// Convert from BGR to HSV colorspace
cv::cvtColor(frame, HSV, COLOR_BGR2HSV);
// Create a mask (this one takes all the brown and green)
int low_H = 85, low_S = 50, low_V = 50;
int high_H = 120, high_S = 255, high_V = 255;
// Detect the object based on HSV Range Values
inRange(HSV, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), mask);
// segment out the background
bitwise_or(frame, frame, HSVmasked, mask = mask);
cv::imshow("HSV_filter", HSVmasked);
//-------Canny----------//
Mat cannyT, gBlur, mBlur;
GaussianBlur(HSVmasked, gBlur, Size(5, 5), 5);
medianBlur(gBlur, mBlur, 7);
imshow("Gaussian & median blur", mBlur);
Canny(mBlur, cannyT, 50, 100, apertureSize, true);
imshow("Canny", cannyT);
//-------Hough lines----------//
/*
dst: Output of the edge detector. It should be a grayscale image (although in fact it is a binary one)
lines: A vector that will store the parameters (x_{start}, y_{start}, x_{end}, y_{end}) of the detected lines
rho : The resolution of the parameter r in pixels. We use 1 pixel.
theta: The resolution of the parameter \theta in radians. We use 1 degree (CV_PI/180)
threshold: The minimum number of intersections to “detect” a line
minLinLength: The minimum number of points that can form a line. Lines with less than this number of points are disregarded.
maxLineGap: The maximum gap between two points to be considered in the same line. */
HoughLinesP(cannyT, lines, 1, CV_PI / 360, houghThreshold, houghMinLength, houghMaxLineGap);
for (auto &i : lines) {
line(frame, Point(i[0], i[1]), Point(i[2], i[3]), Scalar(0, 0, 255), 2, LINE_AA);
}
blade(frame, lines);
Rect boundingBox = lines2boundingbox(frame, lines);
rectangle(frame, boundingBox, Scalar(0, 0, 0, 255));
}
void Detector::blade(Mat frame, vector<Vec4i> lines) {
vector<int> angle;
int counter = 0;
angle.push_back(5);
float x1, y1, x2, y2;
for (auto l : lines) {
x1 = l[0];
x2 = l[2];
y1 = l[1];
y2 = l[3];
Point p1, p2;
p1 = Point(x1, y1);
p2 = Point(x2, y2);
//counter += 1;
//calculate angle in radian, if you need it in degrees just do angle * 180 / PI
angle.at(counter) = atan2(p1.y - p2.y, p1.x - p2.x) * 360 / CV_PI;
//check if angle isn't negative, if so change x,y points
for (int i : angle) {
if (i < 0) {
int holdy;
int holdx;
holdy = p1.y;
p1.y = p2.y;
p2.y = holdy;
holdx = p1.x;
p1.x = p2.x;
p2.x = holdx;
angle.at(counter) = atan2(p1.y - p2.y, p1.x - p2.x) * 360 / CV_PI;
}
}
outputData.open("output.txt", std::ios::app);
outputData << p1.x << "," << p1.y << "," << p2.x << "," << p2.y << "\n";
outputData.close();
// for testing purposes.
// if(fps <= 30){
// std::cout << p1.x << "," << p1.y << "," << p2.x << "," << p2.y << std::endl;
// }
for (size_t i = 0; i < angle.size(); i++) {
int startingAngle;
startingAngle = angle[i];
for (int currentAngle : angle) {
int upperLimit, lowerLimit;
upperLimit = startingAngle + currentAngle;
lowerLimit = startingAngle - currentAngle;
if (upperLimit > 110 && upperLimit < 130 || lowerLimit > 110 && lowerLimit < 130) {
//std::cout << "Blade starts at: x: " << p1.x << " y: " << p1.y << std::endl;
line(frame, Point(x1, y1), Point(x2, y2), Scalar(255, 0, 0), 1, LINE_AA);
}
}
}
}
}
Rect Detector::lines2boundingbox(Mat frame, vector<Vec4i> lines) {
int Xmax = frame.size[0] / 2;
int Xmin = frame.size[0] / 2;
int Ymax = frame.size[1] / 2;
int Ymin = frame.size[1] / 2;
Rect boundingBox;
for (auto &line : lines) {
//Create bounding box
if (Xmax < line[0] || Xmax < line[2]) {
if (line[0] > line[2]) {
Xmax = line[0];
} else {
Xmax = line[2];
}
}
if (Xmin > line[0] || Xmin > line[2]) {
if (line[0] < line[2]) {
Xmin = line[0];
} else {
Xmin = line[2];
}
}
if (Ymax < line[1] || Ymax < line[3]) {
if (line[1] > line[3]) {
Ymax = line[1];
} else {
Ymax = line[3];
}
}
if (Ymin > line[1] || Ymin > line[3]) {
if (line[1] < line[3]) {
Ymin = line[1];
} else {
Ymin = line[3];
}
}
}
boundingBox.x = Xmin;
boundingBox.y = Ymin;
boundingBox.width = Xmax - Xmin;
boundingBox.height = Ymax - Ymin;
return boundingBox;
}