-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP.java
184 lines (161 loc) · 5.35 KB
/
P.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
package org.usfirst.frc.team3734.robot;
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 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.*;
import edu.wpi.first.wpilibj.vision.VisionPipeline;
/**
* GripPipeline class.
*
* <p>An OpenCV pipeline generated by GRIP.
*
* @author GRIP
*/
/** This is useless at the moment
* Supposed to be a vision pipeline
* @author Aaron Li, William Li
*
*/
public class P implements VisionPipeline{
//Outputs
private Mat cvResizeOutput = new Mat();
private Mat hsvThresholdOutput = new Mat();
private Mat cvErodeOutput = new Mat();
private Mat maskOutput = new Mat();
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
/**
* This is the primary method that runs the entire pipeline and updates the outputs.
*/
public void process(Mat source0) {
// Step CV_resize0:
Mat cvResizeSrc = source0;
Size cvResizeDsize = new Size(0, 0);
double cvResizeFx = 0.5;
double cvResizeFy = 0.5;
int cvResizeInterpolation = Imgproc.INTER_LINEAR;
cvResize(cvResizeSrc, cvResizeDsize, cvResizeFx, cvResizeFy, cvResizeInterpolation, cvResizeOutput);
// Step HSV_Threshold0:
Mat hsvThresholdInput = cvResizeOutput;
double[] hsvThresholdHue = {96.05882534152929, 132.94430671752082};
double[] hsvThresholdSaturation = {77.96759592879947, 188.255549865991};
double[] hsvThresholdValue = {72.32338192969354, 202.60022530141256};
hsvThreshold(hsvThresholdInput, hsvThresholdHue, hsvThresholdSaturation, hsvThresholdValue, hsvThresholdOutput);
// Step CV_erode0:
Mat cvErodeSrc = hsvThresholdOutput;
Mat cvErodeKernel = new Mat();
Point cvErodeAnchor = new Point(-1, -1);
double cvErodeIterations = 1;
int cvErodeBordertype = 0;//BORDER_CONSTANT is 0
Scalar cvErodeBordervalue = new Scalar(-1);
cvErode(cvErodeSrc, cvErodeKernel, cvErodeAnchor, cvErodeIterations, cvErodeBordertype, cvErodeBordervalue, cvErodeOutput);
// Step Mask0:
Mat maskInput = cvResizeOutput;
Mat maskMask = cvErodeOutput;
mask(maskInput, maskMask, maskOutput);
}
/**
* This method is a generated getter for the output of a CV_resize.
* @return Mat output from CV_resize.
*/
public Mat cvResizeOutput() {
return cvResizeOutput;
}
/**
* This method is a generated getter for the output of a HSV_Threshold.
* @return Mat output from HSV_Threshold.
*/
public Mat hsvThresholdOutput() {
return hsvThresholdOutput;
}
/**
* This method is a generated getter for the output of a CV_erode.
* @return Mat output from CV_erode.
*/
public Mat cvErodeOutput() {
return cvErodeOutput;
}
/**
* This method is a generated getter for the output of a Mask.
* @return Mat output from Mask.
*/
public Mat maskOutput() {
return maskOutput;
}
/**
* Resizes an image.
* @param src The image to resize.
* @param dSize size to set the image.
* @param fx scale factor along X axis.
* @param fy scale factor along Y axis.
* @param interpolation type of interpolation to use.
* @param dst output image.
*/
private void cvResize(Mat src, Size dSize, double fx, double fy, int interpolation,
Mat dst) {
if (dSize==null) {
dSize = new Size(0,0);
}
Imgproc.resize(src, dst, dSize, fx, fy, interpolation);
}
/**
* Segment an image based on hue, saturation, and value ranges.
*
* @param input The image on which to perform the HSL threshold.
* @param hue The min and max hue
* @param sat The min and max saturation
* @param val The min and max value
* @param output The image in which to store the output.
*/
private void hsvThreshold(Mat input, double[] hue, double[] sat, double[] val,
Mat out) {
Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2HSV);
Core.inRange(out, new Scalar(hue[0], sat[0], val[0]),
new Scalar(hue[1], sat[1], val[1]), out);
}
/**
* Expands area of lower value in an image.
* @param src the Image to erode.
* @param kernel the kernel for erosion.
* @param anchor the center of the kernel.
* @param iterations the number of times to perform the erosion.
* @param borderType pixel extrapolation method.
* @param borderValue value to be used for a constant border.
* @param dst Output Image.
*/
private void cvErode(Mat src, Mat kernel, Point anchor, double iterations,
int borderType, Scalar borderValue, Mat dst) {
if (kernel == null) {
kernel = new Mat();
}
if (anchor == null) {
anchor = new Point(-1,-1);
}
if (borderValue == null) {
borderValue = new Scalar(-1);
}
Imgproc.erode(src, dst, kernel, anchor, (int)iterations, borderType, borderValue);
}
/**
* Filter out an area of an image using a binary mask.
* @param input The image on which the mask filters.
* @param mask The binary image that is used to filter.
* @param output The image in which to store the output.
*/
private void mask(Mat input, Mat mask, Mat output) {
mask.convertTo(mask, CvType.CV_8UC1);
Core.bitwise_xor(output, output, output);
input.copyTo(output, mask);
}
}