-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision.cpp.save.2
185 lines (116 loc) · 3.59 KB
/
vision.cpp.save.2
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
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int hUpperLimit = 62;
int hLowerLimit = 96;
int sUpperLimit = 76;
int sLowerLimit = 248;
int vUpperLimit = 27;
int vLowerLimit = 255;
int avgR;
int avgG;
int avgB;
VideoCapture cap;
Mat rgbImg;
Mat grayImg;
Mat binaryImg;
Mat drawing;
bool setExposure= true;
//sean is reallyy bad
void changeExposure(){
int currentExposure = 100;
// String exposureCommand = new String();
// exposureCommand += " v4l2-ctl -c exposure_auto=1 && v4l2-ctl --set-ctrl exposure_absolute="+currentExposure+" && v4l&& v4l2-ctl --set-ctrl contrast=255";
}
void threshImageRGB(){
cvtColor(rgbImg, binaryImg, CV_RGB2GRAY );
cvtColor(rgbImg, grayImg, CV_RGB2GRAY );
for(int x = 0; x<rgbImg.rows;x++){ //Loops through every pixel of the image
for(int y = 0; y<rgbImg.cols;y++){
int B = (int)rgbImg.at<Vec3b>(x,y)[0];
int G = (int)rgbImg.at<Vec3b>(x,y)[1];
int R = (int)rgbImg.at<Vec3b>(x,y)[2];
//gray = green - (blue*blue_scale + red*red_scale)
int Gray = G - (B*.80+R*.5);
//If gray is less negative it is equal to 0
if(Gray<0){
Gray = 0;
}
//Sets the point to the gray value
grayImg.at<uchar>(x,y) = Gray;
}
}
threshold(grayImg, binaryImg, 16.0, 255, THRESH_BINARY);
}
void threshImageHSV(){
Mat hsvImg;
cvtColor(rgbImg, binaryImg, CV_RGB2GRAY );
cvtColor(rgbImg, grayImg, CV_RGB2GRAY );
cvtColor(rgbImg, hsvImg, CV_BGR2HSV);
for(int i = 0;i < rgbImg.rows;i++){
for(int j = 0;j < rgbImg.cols;j++){
int Gray = 0;
Vec3b hsv=hsvImg.at<Vec3b>(i,j);
int h=hsv.val[0]; //hue
int s=hsv.val[1]; //saturation
int v=hsv.val[2]; //value
if(h < hUpperLimit && h > hUpperLimit){
if(s < sUpperLimit && h > sUpperLimit){
if(h < hUpperLimit && h > hUpperLimit){
Gray = 0;
}
}
}
grayImg.at<uchar>(i,j) = 0;
}
}
threshold(grayImg, binaryImg, 16.0, 255, THRESH_BINARY);
}
void findTargets(){
const Scalar RED(0, 0, 255);
vector <vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binaryImg, contours, hierarchy,RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for( int i = 0; i< contours.size(); i++ ){
//this loops through each contour and determine with some simple test if is the correct target
vector<Point> matOfPoint = contours[i];
vector<Point> approxPoly;
approxPolyDP(matOfPoint, approxPoly, arcLength(matOfPoint, true) * .02, true);
Rect rect = boundingRect(contours[i]);
int height = rect.br().y - rect.tl().y;
int width = rect.br().x - rect. tl().x;
//the first filter just aproximates things with 4 sides
if(approxPoly.size() > 2 && approxPoly.size() < 10 ){
//Second filter is the area of the box. We can assume it is bigger than 500 pixels. That number is complelty made up and should be tuned
if((height*width)>100){
rectangle(rgbImg, rect.br(), rect.tl(), RED);
}
}
}
}
int main(){
cap.open(0);
while(true){
Mat bgrImg;
cap.read(rgbImg);
//convert the BGR Image to RGB off the bat to avoid confusion
//cvtColor(bgrImg, rgbImg, CV_BGR2RGB);
GaussianBlur(rgbImg, rgbImg, Size(5,5), 0, 0);
threshImageRGB();
findTargets();
imshow("Input", rgbImg);
imshow("Output", binaryImg);
if(setExposure){
system("v4l2-ctl -c exposure_auto=1 && v4l2-ctl --set-ctrl exposure_absolute=88 && v4l&& v4l2-ctl --set-ctrl contrast=255");
setExposure = false;
}
if (waitKey(66) >= 0){
break;
}
}
return 0;
}