-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetectionPipeline.java
137 lines (117 loc) · 3.76 KB
/
DetectionPipeline.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
129
130
131
132
133
134
135
136
137
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import org.firstinspires.ftc.robotcore.external.Telemetry;
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;
import org.opencv.imgproc.Moments;
import org.opencv.core.MatOfPoint;
import java.util.ArrayList;
import java.util.List;
@Disabled
public class DetectionPipeline extends OpenCvPipeline
{
enum ShippingLocation {
LEFT,
RIGHT,
CENTER,
NONE
}
// private int width;
ShippingLocation location;
private Telemetry telemetry;
private Boolean two;
public DetectionPipeline(Telemetry telemetry, Boolean two) {
this.telemetry = telemetry;
this.two = two;
}
@Override
public Mat processFrame(Mat input) {
Mat mat = new Mat();
Imgproc.cvtColor(input, mat, Imgproc.COLOR_RGB2HSV);
Rect crop = new Rect(0, input.height()/2, input.width(), input.height()/2);
mat = new Mat(mat, crop);
Scalar lower = new Scalar(76, 50, 50); //green
Scalar upper = new Scalar(96, 255, 255); //greener
Mat thresh = new Mat();
Core.inRange(mat, lower, upper, thresh);
List<MatOfPoint> contours = new ArrayList();
Mat heirarchy = new Mat();
Imgproc.findContours(thresh, contours, heirarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE);
double maxVal = 0;
int maxValIdx = 0;
if (contours.size() == 0) {
location = ShippingLocation.NONE;
telemetry.addData("Location", location);
telemetry.update();
Mat resized = new Mat();
Imgproc.resize(mat, resized, new Size(mat.width()/4, mat.height()/4));
Imgproc.putText(
resized,
location.toString(),
new Point(50, 75),
Imgproc.FONT_HERSHEY_PLAIN,
5,
new Scalar(255, 255, 255),
5
);
return resized;
}
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
{
double contourArea = Imgproc.contourArea(contours.get(contourIdx));
if (maxVal < contourArea)
{
maxVal = contourArea;
maxValIdx = contourIdx;
}
}
Moments m = Imgproc.moments(contours.get(maxValIdx));
double center = m.m10/m.m00;
int width = input.width();
//reverse if on other side
if(!two) {
//cameras not centered so adjust
if (center <= width/4) {
location = ShippingLocation.LEFT;
} else if (center <= (width/4)*2) {
location = ShippingLocation.CENTER;
} else {
location = ShippingLocation.RIGHT;
}
} else {
if (center <= (width/4)*2) {
location = ShippingLocation.LEFT;
} else if (center <= (width/4)*3) {
location = ShippingLocation.CENTER;
} else {
location = ShippingLocation.RIGHT;
}
}
telemetry.addData("Location", location);
telemetry.update();
Imgproc.circle(mat, new Point(m.m10/m.m00, m.m01/m.m00), 20, new Scalar(255, 0, 0), 40);
Imgproc.line(mat, new Point(width/2, 0), new Point(width/4, 853), new Scalar(255, 0, 0), 5);
Imgproc.line(mat, new Point(width/2, 0), new Point(width/2, 853), new Scalar(255, 0, 0), 5);
// Mat resized = new Mat();
// Imgproc.resize(mat, resized, new Size(mat.width()/4, mat.height()/4));
Imgproc.putText(
mat,
location.toString(),
new Point(50, 75),
Imgproc.FONT_HERSHEY_PLAIN,
5,
new Scalar(255, 255, 255),
5
);
return mat;
}
public ShippingLocation getLocation() {
return location;
}
}