Java tutorial
/* * Copyright (c) 2017 Titan Robotics Club (http://www.titanrobotics.com) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package team492; import java.util.ArrayList; import java.util.List; import org.opencv.core.*; import org.opencv.imgproc.*; /** * GripPipeline class. * * <p> * An OpenCV pipeline generated by GRIP. * * @author GRIP */ public class GripPipeline { // Outputs 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 HSV_Threshold0: Mat hsvThresholdInput = source0; double[] hsvThresholdHue = { 50.17985611510791, 99.69696969696967 }; double[] hsvThresholdSaturation = { 135.29676258992805, 255.0 }; double[] hsvThresholdValue = { 169.69424460431654, 255.0 }; hsvThreshold(hsvThresholdInput, hsvThresholdHue, hsvThresholdSaturation, hsvThresholdValue, hsvThresholdOutput); // Step CV_erode0: Mat cvErodeSrc = hsvThresholdOutput; Mat cvErodeKernel = new Mat(); Point cvErodeAnchor = new Point(-1, -1); double cvErodeIterations = 10.0; 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 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; } /** * 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); } } //class GripPipeline