-
Notifications
You must be signed in to change notification settings - Fork 4
/
SleeveDetection.java
95 lines (83 loc) · 2.97 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
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;
// Color definitions
private final Scalar
YELLOW = new Scalar(255, 255, 0),
CYAN = new Scalar(0, 255, 255),
MAGENTA = new Scalar(255, 0, 255);
// 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) {
// Get the submat frame, and then sum all the values
Mat areaMat = input.submat(new Rect(sleeve_pointA, sleeve_pointB));
Scalar sumColors = Core.sumElems(areaMat);
// Get the minimum RGB value from every single channel
double minColor = Math.min(sumColors.val[0], Math.min(sumColors.val[1], sumColors.val[2]));
// Change the bounding box color based on the sleeve color
if (sumColors.val[0] == minColor) {
position = ParkingPosition.CENTER;
Imgproc.rectangle(
input,
sleeve_pointA,
sleeve_pointB,
CYAN,
2
);
} else if (sumColors.val[1] == minColor) {
position = ParkingPosition.RIGHT;
Imgproc.rectangle(
input,
sleeve_pointA,
sleeve_pointB,
MAGENTA,
2
);
} else {
position = ParkingPosition.LEFT;
Imgproc.rectangle(
input,
sleeve_pointA,
sleeve_pointB,
YELLOW,
2
);
}
// Release and return input
areaMat.release();
return input;
}
// Returns an enum being the current position where the robot will park
public ParkingPosition getPosition() {
return position;
}
}