-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaGripPipeline.java
244 lines (214 loc) · 6.58 KB
/
JavaGripPipeline.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package grip;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.HashMap;
import edu.wpi.first.wpilibj.vision.VisionPipeline;
import org.opencv.core.*;
import org.opencv.core.Core.*;
import org.opencv.features2d.FeatureDetector;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.*;
import org.opencv.objdetect.*;
/**
* JavaGripPipeline class.
*
* <p>An OpenCV pipeline generated by GRIP.
*
* @author GRIP
*/
public class JavaGripPipeline implements VisionPipeline {
//Outputs
private Mat blurOutput = new Mat();
private Mat rgbThresholdOutput = new Mat();
private ArrayList<Line> findLinesOutput = new ArrayList<Line>();
private ArrayList<Line> filterLinesOutput = new ArrayList<Line>();
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
/**
* This is the primary method that runs the entire pipeline and updates the outputs.
*/
@Override public void process(Mat source0) {
// Step Blur0:
Mat blurInput = source0;
BlurType blurType = BlurType.get("Median Filter");
double blurRadius = 0.0;
blur(blurInput, blurType, blurRadius, blurOutput);
// Step RGB_Threshold0:
Mat rgbThresholdInput = blurOutput;
double[] rgbThresholdRed = {0.0, 255.0};
double[] rgbThresholdGreen = {208.67805755395685, 255.0};
double[] rgbThresholdBlue = {0.0, 255.0};
rgbThreshold(rgbThresholdInput, rgbThresholdRed, rgbThresholdGreen, rgbThresholdBlue, rgbThresholdOutput);
// Step Find_Lines0:
Mat findLinesInput = rgbThresholdOutput;
findLines(findLinesInput, findLinesOutput);
// Step Filter_Lines0:
ArrayList<Line> filterLinesLines = findLinesOutput;
double filterLinesMinLength = 0.0;
double[] filterLinesAngle = {0.0, 360};
filterLines(filterLinesLines, filterLinesMinLength, filterLinesAngle, filterLinesOutput);
}
/**
* This method is a generated getter for the output of a Blur.
* @return Mat output from Blur.
*/
public Mat blurOutput() {
return blurOutput;
}
/**
* This method is a generated getter for the output of a RGB_Threshold.
* @return Mat output from RGB_Threshold.
*/
public Mat rgbThresholdOutput() {
return rgbThresholdOutput;
}
/**
* This method is a generated getter for the output of a Find_Lines.
* @return ArrayList<Line> output from Find_Lines.
*/
public ArrayList<Line> findLinesOutput() {
return findLinesOutput;
}
/**
* This method is a generated getter for the output of a Filter_Lines.
* @return ArrayList<Line> output from Filter_Lines.
*/
public ArrayList<Line> filterLinesOutput() {
return filterLinesOutput;
}
/**
* An indication of which type of filter to use for a blur.
* Choices are BOX, GAUSSIAN, MEDIAN, and BILATERAL
*/
enum BlurType{
BOX("Box Blur"), GAUSSIAN("Gaussian Blur"), MEDIAN("Median Filter"),
BILATERAL("Bilateral Filter");
private final String label;
BlurType(String label) {
this.label = label;
}
public static BlurType get(String type) {
if (BILATERAL.label.equals(type)) {
return BILATERAL;
}
else if (GAUSSIAN.label.equals(type)) {
return GAUSSIAN;
}
else if (MEDIAN.label.equals(type)) {
return MEDIAN;
}
else {
return BOX;
}
}
@Override
public String toString() {
return this.label;
}
}
/**
* Softens an image using one of several filters.
* @param input The image on which to perform the blur.
* @param type The blurType to perform.
* @param doubleRadius The radius for the blur.
* @param output The image in which to store the output.
*/
private void blur(Mat input, BlurType type, double doubleRadius,
Mat output) {
int radius = (int)(doubleRadius + 0.5);
int kernelSize;
switch(type){
case BOX:
kernelSize = 2 * radius + 1;
Imgproc.blur(input, output, new Size(kernelSize, kernelSize));
break;
case GAUSSIAN:
kernelSize = 6 * radius + 1;
Imgproc.GaussianBlur(input,output, new Size(kernelSize, kernelSize), radius);
break;
case MEDIAN:
kernelSize = 2 * radius + 1;
Imgproc.medianBlur(input, output, kernelSize);
break;
case BILATERAL:
Imgproc.bilateralFilter(input, output, -1, radius, radius);
break;
}
}
/**
* Segment an image based on color ranges.
* @param input The image on which to perform the RGB threshold.
* @param red The min and max red.
* @param green The min and max green.
* @param blue The min and max blue.
* @param output The image in which to store the output.
*/
private void rgbThreshold(Mat input, double[] red, double[] green, double[] blue,
Mat out) {
Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2RGB);
Core.inRange(out, new Scalar(red[0], green[0], blue[0]),
new Scalar(red[1], green[1], blue[1]), out);
}
public static class Line {
public final double x1, y1, x2, y2;
public Line(double x1, double y1, double x2, double y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public double lengthSquared() {
return Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2);
}
public double length() {
return Math.sqrt(lengthSquared());
}
public double angle() {
return Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));
}
}
/**
* Finds all line segments in an image.
* @param input The image on which to perform the find lines.
* @param lineList The output where the lines are stored.
*/
private void findLines(Mat input, ArrayList<Line> lineList) {
final LineSegmentDetector lsd = Imgproc.createLineSegmentDetector();
final Mat lines = new Mat();
lineList.clear();
if (input.channels() == 1) {
lsd.detect(input, lines);
} else {
final Mat tmp = new Mat();
Imgproc.cvtColor(input, tmp, Imgproc.COLOR_BGR2GRAY);
lsd.detect(tmp, lines);
}
if (!lines.empty()) {
for (int i = 0; i < lines.rows(); i++) {
lineList.add(new Line(lines.get(i, 0)[0], lines.get(i, 0)[1],
lines.get(i, 0)[2], lines.get(i, 0)[3]));
}
}
}
/**
* Filters out lines that do not meet certain criteria.
* @param inputs The lines that will be filtered.
* @param minLength The minimum length of a line to be kept.
* @param angle The minimum and maximum angle of a line to be kept.
* @param outputs The output lines after the filter.
*/
private void filterLines(List<Line> inputs,double minLength,double[] angle,
List<Line> outputs) {
outputs = inputs.stream()
.filter(line -> line.lengthSquared() >= Math.pow(minLength,2))
.filter(line -> (line.angle() >= angle[0] && line.angle() <= angle[1])
|| (line.angle() + 180.0 >= angle[0] && line.angle() + 180.0 <= angle[1]))
.collect(Collectors.toList());
}
}