forked from QuinnThomas/grip-vision-2017
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGripPipeline.java
184 lines (160 loc) · 5.43 KB
/
GripPipeline.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
package gripcode1;
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.*;
/**
* GripPipeline class.
*
* <p>An OpenCV pipeline generated by GRIP.
*
* @author GRIP
*/
public class GripPipeline {
//Outputs
private Mat resizeImageOutput = new Mat();
private Mat hsvThresholdOutput = new Mat();
private Mat cvErodeOutput = new Mat();
private ArrayList<MatOfPoint> findContoursOutput = new ArrayList<MatOfPoint>();
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 Resize_Image0:
Mat resizeImageInput = source0;
double resizeImageWidth = 640;
double resizeImageHeight = 480;
int resizeImageInterpolation = Imgproc.INTER_CUBIC;
resizeImage(resizeImageInput, resizeImageWidth, resizeImageHeight, resizeImageInterpolation, resizeImageOutput);
// Step HSV_Threshold0:
Mat hsvThresholdInput = resizeImageOutput;
double[] hsvThresholdHue = {84.17266187050359, 91.37521222410864};
double[] hsvThresholdSaturation = {0.0, 255.0};
double[] hsvThresholdValue = {75.67446043165468, 140.27164685908318};
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 = Core.BORDER_CONSTANT;
Scalar cvErodeBordervalue = new Scalar(-1);
cvErode(cvErodeSrc, cvErodeKernel, cvErodeAnchor, cvErodeIterations, cvErodeBordertype, cvErodeBordervalue, cvErodeOutput);
// Step Find_Contours0:
Mat findContoursInput = cvErodeOutput;
boolean findContoursExternalOnly = false;
findContours(findContoursInput, findContoursExternalOnly, findContoursOutput);
}
/**
* This method is a generated getter for the output of a Resize_Image.
* @return Mat output from Resize_Image.
*/
public Mat resizeImageOutput() {
return resizeImageOutput;
}
/**
* 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 Find_Contours.
* @return ArrayList<MatOfPoint> output from Find_Contours.
*/
public ArrayList<MatOfPoint> findContoursOutput() {
return findContoursOutput;
}
/**
* Scales and image to an exact size.
* @param input The image on which to perform the Resize.
* @param width The width of the output in pixels.
* @param height The height of the output in pixels.
* @param interpolation The type of interpolation.
* @param output The image in which to store the output.
*/
private void resizeImage(Mat input, double width, double height,
int interpolation, Mat output) {
Imgproc.resize(input, output, new Size(width, height), 0.0, 0.0, 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);
}
/**
* Sets the values of pixels in a binary image to their distance to the nearest black pixel.
* @param input The image on which to perform the Distance Transform.
* @param type The Transform.
* @param maskSize the size of the mask.
* @param output The image in which to store the output.
*/
private void findContours(Mat input, boolean externalOnly,
List<MatOfPoint> contours) {
Mat hierarchy = new Mat();
contours.clear();
int mode;
if (externalOnly) {
mode = Imgproc.RETR_EXTERNAL;
}
else {
mode = Imgproc.RETR_LIST;
}
int method = Imgproc.CHAIN_APPROX_SIMPLE;
Imgproc.findContours(input, contours, hierarchy, mode, method);
}
}