-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision.cpp.save.5
144 lines (86 loc) · 3.7 KB
/
vision.cpp.save.5
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
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <cmath>
#include <stdio.h>
#include "include/networktables/NetworkTable.h"
#define real true
//This is here for later cmake -DCUDA_USE_STATIC_CUDA_RUNTIME=OFF .
using namespace cv;
cv::Mat matOriginal, matThresh, matGray, matThreshCopy;
cv::Mat matColor, matBlurred, matOrigCopy, matTest;
VideoCapture cap;
const Scalar YELLOW(0, 255, 255),
RED(0, 0, 255),
GREEN(33, 255, 0),
MIDDLEGROUND(255, 0, 0),
LOWER_BOUNDS(0, 40, 0),
UPPER_BOUNDS(40, 255, 40);
double normalize360(double angle) {
while (angle >= 360.0) {
angle -= 360.0;
}
while (angle < 0.0) {
angle += 360.0;
}
return angle;
}
void NetworkTables(double azi,double dist) {
NetworkTable::SetClientMode();
NetworkTable::SetTeam(668);
auto table = NetworkTable::GetTable("SmartDashboard");
table->PutNumber("Azimuth",azi);
table->PutNumber("Distance",dist);
}
void processImage(){
vector<vector<Point> > contours;
while(true){
contours.clear();
cap.read(matOriginal);
cvtColor(matOriginal, matGray, COLOR_BGR2GRAY);
// GaussianBlur(matOriginal, matBlurred, Size(7,7), 0, 0);
// inRange(matBlurred, LOWER_BOUNDS, UPPER_BOUNDS, matGray);
threshold(matGray, matThresh, 16.0, 255, THRESH_BINARY);
threshold(matGray, matThreshCopy, 16.0, 255, THRESH_BINARY);
findContours(matThreshCopy, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
int xVals [2];
int yVals [2];
int side1 [2];
int side2 [2];
int index = 0;
for(int i = 0; i < contours.size(); i++){
vector<Point> matOfPoint = contours[i];
vector<Point> approxPoly;
approxPolyDP(matOfPoint, approxPoly, arcLength(matOfPoint, true) * .02, true);
if(approxPoly.size() == 4 ){
Rect rec = boundingRect(contours[i]);
double ratio = (double)rec.height/(double)rec.width;
if(ratio<3&&ratio>1.7){
int G = (int)matOriginal.at<Vec3b>((int)((rec.tl().y + rec.br().y)/2), (int)((rec.br().x + rec.tl().x)/2))[1];
int B = (int)matOriginal.at<Vec3b>((int)((rec.tl().y + rec.br().y)/2), (int)((rec.br().x + rec.tl().x)/2))[0];
int R = (int)matOriginal.at<Vec3b>((int)((rec.tl().y + rec.br().y)/2), (int)((rec.br().x + rec.tl().x)/2))[2];
//This code prints out the BGR values for the center pixel that we reference for
if(G>200&&R<10){
//std::cout <<" B:"<< (int)matOriginal.at<Vec3b>((int)((rec.tl().y + rec.br().y)/2), (int)((rec.br().x + rec.tl().x)/2))[0];
//std::cout <<" G:"<< (int)matOriginal.at<Vec3b>((int)((rec.tl().y + rec.br().y)/2), (int)((rec.br().x + rec.tl().x)/2))[1];
//std::cout <<" R:"<< (int)matOriginal.at<Vec3b>((int)((rec.tl().y + rec.br().y)/2), (int)((rec.br().x + rec.tl().x)/2))[2];
//std::cout <<" Ratio: "<< ratio<<std::endl;
rectangle(matOriginal, rec.br(), rec.tl(), RED);
circle(matOriginal, Point((int)((rec.br().x + rec.tl().x)/2), (int)((rec.tl().y + rec.br().y)/2)), 10, RED, 2, 8, 0);
if (index == 0){
xVals[index] = rec.br().x;
yVals[index] = rec.br().y;
side1[0] = rec.br().y;
side1[1] = rec.tl().y;
}else if (index == 1){
xVals[index] = rec.tl().x;
yVals[index] = rec.tl().y;
side2[0] = rec.br().y;
side2[1] = rec.tl().y;
}
} // if g>100
index++;
} // aspect ratio?
} // poly size?
} // for contours.com
//Drawing Center Point