generated from KookyBotz/PowerPlaySleeveDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SleeveDetection.java
128 lines (110 loc) · 4.5 KB
/
SleeveDetection.java
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
import org.opencv.core.Core;
import org.opencv.core.Mat;
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;
public class SleeveDetection extends OpenCvPipeline {
/*
YELLOW = Parking Left
CYAN = Parking Middle
MAGENTA = Parking Right
*/
public enum ParkingPosition {
LEFT,
CENTER,
RIGHT
}
// TOPLEFT anchor point for the bounding box
private static Point SLEEVE_TOPLEFT_ANCHOR_POINT = new Point(145, 168);
// Width and height for the bounding box
public static int REGION_WIDTH = 30;
public static int REGION_HEIGHT = 50;
// Lower and upper boundaries for colors
private static final Scalar
lower_yellow_bounds = new Scalar(200, 200, 0, 255),
upper_yellow_bounds = new Scalar(255, 255, 130, 255),
lower_cyan_bounds = new Scalar(0, 200, 200, 255),
upper_cyan_bounds = new Scalar(150, 255, 255, 255),
lower_magenta_bounds = new Scalar(170, 0, 170, 255),
upper_magenta_bounds = new Scalar(255, 60, 255, 255);
// Color definitions
private final Scalar
YELLOW = new Scalar(255, 255, 0),
CYAN = new Scalar(0, 255, 255),
MAGENTA = new Scalar(255, 0, 255);
// Percent and mat definitions
private double yelPercent, cyaPercent, magPercent;
private Mat yelMat = new Mat(), cyaMat = new Mat(), magMat = new Mat(), blurredMat = new Mat(), kernel = new Mat();
// Anchor point definitions
Point sleeve_pointA = new Point(
SLEEVE_TOPLEFT_ANCHOR_POINT.x,
SLEEVE_TOPLEFT_ANCHOR_POINT.y);
Point sleeve_pointB = new Point(
SLEEVE_TOPLEFT_ANCHOR_POINT.x + REGION_WIDTH,
SLEEVE_TOPLEFT_ANCHOR_POINT.y + REGION_HEIGHT);
// Running variable storing the parking position
private volatile ParkingPosition position = ParkingPosition.LEFT;
@Override
public Mat processFrame(Mat input) {
// Noise reduction
Imgproc.blur(input, blurredMat, new Size(5, 5));
blurredMat = blurredMat.submat(new Rect(sleeve_pointA, sleeve_pointB));
// Apply Morphology
kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
Imgproc.morphologyEx(blurredMat, blurredMat, Imgproc.MORPH_CLOSE, kernel);
// Gets channels from given source mat
Core.inRange(blurredMat, lower_yellow_bounds, upper_yellow_bounds, yelMat);
Core.inRange(blurredMat, lower_cyan_bounds, upper_cyan_bounds, cyaMat);
Core.inRange(blurredMat, lower_magenta_bounds, upper_magenta_bounds, magMat);
// Gets color specific values
yelPercent = Core.countNonZero(yelMat);
cyaPercent = Core.countNonZero(cyaMat);
magPercent = Core.countNonZero(magMat);
// Calculates the highest amount of pixels being covered on each side
double maxPercent = Math.max(yelPercent, Math.max(cyaPercent, magPercent));
// Checks all percentages, will highlight bounding box in camera preview
// based on what color is being detected
if (maxPercent == yelPercent) {
position = ParkingPosition.LEFT;
Imgproc.rectangle(
input,
sleeve_pointA,
sleeve_pointB,
YELLOW,
2
);
} else if (maxPercent == cyaPercent) {
position = ParkingPosition.CENTER;
Imgproc.rectangle(
input,
sleeve_pointA,
sleeve_pointB,
CYAN,
2
);
} else if (maxPercent == magPercent) {
position = ParkingPosition.RIGHT;
Imgproc.rectangle(
input,
sleeve_pointA,
sleeve_pointB,
MAGENTA,
2
);
}
// Memory cleanup
blurredMat.release();
yelMat.release();
cyaMat.release();
magMat.release();
kernel.release();
return input;
}
// Returns an enum being the current position where the robot will park
public ParkingPosition getPosition() {
return position;
}
}