-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContourPipeline
222 lines (195 loc) · 8.85 KB
/
ContourPipeline
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
package org.firstinspires.ftc.teamcode;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.openftc.easyopencv.OpenCvPipeline;
import java.util.ArrayList;
import java.util.List;
// Credits to team 7303 RoboAvatars, adjusted by team 3954 Pink to the Future
public class ContourPipeline extends OpenCvPipeline {
Scalar HOT_PINK = new Scalar(196, 23, 112);
// Pink, the default color Y Cr Cb (Do not change Y)
public static Scalar scalarLowerYCrCb = new Scalar(0.0, 150.0, 120.0);
public static Scalar scalarUpperYCrCb = new Scalar(255.0, 255.0, 255.0);
// Yellow, freight or ducks!
//public static Scalar scalarLowerYCrCb = new Scalar(0.0, 100.0, 0.0);
//public static Scalar scalarUpperYCrCb = new Scalar(255.0, 170.0, 120.0);
// Green Y Cr Cb
// public static Scalar scalarLowerYCrCb = new Scalar( 0.0, 0.0, 0.0);
// public static Scalar scalarUpperYCrCb = new Scalar(255.0, 120.0, 120.0);
// Use this picture for you own color https://github.com/PinkToTheFuture/OpenCV_FreightFrenzy_2021-2022/blob/main/YCbCr.jpeg
// Note that the Cr and Cb values range between 0-255. this means that the origin of the coordinate system is (128,128)
// Volatile because accessed by OpMode without sync
public volatile boolean error = false;
public volatile Exception debug;
private double borderLeftX; //fraction of pixels from the left side of the cam to skip
private double borderRightX; //fraction of pixels from the right of the cam to skip
private double borderTopY; //fraction of pixels from the top of the cam to skip
private double borderBottomY; //fraction of pixels from the bottom of the cam to skip
private int CAMERA_WIDTH;
private int CAMERA_HEIGHT;
private int loopCounter = 0;
private int pLoopCounter = 0;
private final Mat mat = new Mat();
private final Mat processed = new Mat();
private Rect maxRect = new Rect(600,1,1,1);
private double maxArea = 0;
private boolean first = false;
private final Object sync = new Object();
public ContourPipeline(double borderLeftX, double borderRightX, double borderTopY, double borderBottomY) {
this.borderLeftX = borderLeftX;
this.borderRightX = borderRightX;
this.borderTopY = borderTopY;
this.borderBottomY = borderBottomY;
}
public void configureScalarLower(double y, double cr, double cb) {
scalarLowerYCrCb = new Scalar(y, cr, cb);
}
public void configureScalarUpper(double y, double cr, double cb) {
scalarUpperYCrCb = new Scalar(y, cr, cb);
}
public void configureScalarLower(int y, int cr, int cb) {
scalarLowerYCrCb = new Scalar(y, cr, cb);
}
public void configureScalarUpper(int y, int cr, int cb) {
scalarUpperYCrCb = new Scalar(y, cr, cb);
}
public void configureBorders(double borderLeftX, double borderRightX, double borderTopY, double borderBottomY) {
this.borderLeftX = borderLeftX;
this.borderRightX = borderRightX;
this.borderTopY = borderTopY;
this.borderBottomY = borderBottomY;
}
@Override
public Mat processFrame(Mat input) {
CAMERA_WIDTH = input.width();
CAMERA_HEIGHT = input.height();
try {
// Process Image
Imgproc.cvtColor(input, mat, Imgproc.COLOR_RGB2YCrCb);
Core.inRange(mat, scalarLowerYCrCb, scalarUpperYCrCb, processed);
// Remove Noise
Imgproc.morphologyEx(processed, processed, Imgproc.MORPH_OPEN, new Mat());
Imgproc.morphologyEx(processed, processed, Imgproc.MORPH_CLOSE, new Mat());
// GaussianBlur
Imgproc.GaussianBlur(processed, processed, new Size(5.0, 15.0), 0.00);
// Find Contours
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(processed, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
// Draw Contours
Imgproc.drawContours(input, contours, -1, new Scalar(255, 0, 0));
// Lock this up to prevent errors when outside threads access the max rect property.
synchronized (sync) {
// Loop Through Contours
for (MatOfPoint contour : contours) {
Point[] contourArray = contour.toArray();
// Bound Rectangle if Contour is Large Enough
if (contourArray.length >= 15) {
MatOfPoint2f areaPoints = new MatOfPoint2f(contourArray);
Rect rect = Imgproc.boundingRect(areaPoints);
if ( rect.area() > maxArea
&& rect.x + (rect.width / 2.0) > (borderLeftX * CAMERA_WIDTH)
&& rect.x + (rect.width / 2.0) < CAMERA_WIDTH - (borderRightX * CAMERA_WIDTH)
&& rect.y + (rect.height / 2.0) > (borderTopY * CAMERA_HEIGHT)
&& rect.y + (rect.height / 2.0) < CAMERA_HEIGHT - (borderBottomY * CAMERA_HEIGHT)
|| loopCounter - pLoopCounter > 6
&& rect.x + (rect.width / 2.0) > (borderLeftX * CAMERA_WIDTH)
&& rect.x + (rect.width / 2.0) < CAMERA_WIDTH - (borderRightX * CAMERA_WIDTH)
&& rect.y + (rect.height / 2.0) > (borderTopY * CAMERA_HEIGHT)
&& rect.y + (rect.height / 2.0) < CAMERA_HEIGHT - (borderBottomY * CAMERA_HEIGHT)
){
maxArea = rect.area();
maxRect = rect;
pLoopCounter++;
loopCounter = pLoopCounter;
first = true;
}
else if(loopCounter - pLoopCounter > 10){
maxArea = new Rect().area();
maxRect = new Rect();
}
areaPoints.release();
}
contour.release();
}
if (contours.isEmpty()) {
maxRect = new Rect(600,1,1,1);
}
}
// Draw Rectangles If Area Is At Least 500
if (first && maxRect.area() > 500) {
Imgproc.rectangle(input, maxRect, new Scalar(0, 255, 0), 2);
}
// Draw Borders
Imgproc.rectangle(input, new Rect(
(int) (borderLeftX * CAMERA_WIDTH),
(int) (borderTopY * CAMERA_HEIGHT),
(int) (CAMERA_WIDTH - (borderRightX * CAMERA_WIDTH) - (borderLeftX * CAMERA_WIDTH)),
(int) (CAMERA_HEIGHT - (borderBottomY * CAMERA_HEIGHT) - (borderTopY * CAMERA_HEIGHT))
), HOT_PINK, 2);
// Display Data
Imgproc.putText(input, "Area: " + getRectArea() + " Midpoint: " + getRectMidpointXY().x + " , " + getRectMidpointXY().y, new Point(5, CAMERA_HEIGHT - 5), 0, 0.6, new Scalar(255, 255, 255), 2);
loopCounter++;
} catch (Exception e) {
debug = e;
error = true;
}
return input;
}
/*
Synchronize these operations as the user code could be incorrect otherwise, i.e a property is read
while the same rectangle is being processed in the pipeline, leading to some values being not
synced.
*/
public int getRectHeight() {
synchronized (sync) {
return maxRect.height;
}
}
public int getRectWidth() {
synchronized (sync) {
return maxRect.width;
}
}
public int getRectX() {
synchronized (sync) {
return maxRect.x;
}
}
public int getRectY() {
synchronized (sync) {
return maxRect.y;
}
}
public double getRectMidpointX() {
synchronized (sync) {
return getRectX() + (getRectWidth() / 2.0);
}
}
public double getRectMidpointY() {
synchronized (sync) {
return getRectY() + (getRectHeight() / 2.0);
}
}
public Point getRectMidpointXY() {
synchronized (sync) {
return new Point(getRectMidpointX(), getRectMidpointY());
}
}
public double getAspectRatio() {
synchronized (sync) {
return getRectArea() / (CAMERA_HEIGHT * CAMERA_WIDTH);
}
}
public double getRectArea() {
synchronized (sync) {
return maxRect.area();
}
}
}