SHARE
TWEET

Untitled

a guest Sep 19th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.usfirst.frc.team6083.robot;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.stream.Collectors;
  10. import java.util.HashMap;
  11.  
  12. import edu.wpi.first.wpilibj.vision.VisionPipeline;
  13.  
  14. import org.opencv.core.*;
  15. import org.opencv.core.Core.*;
  16. import org.opencv.features2d.FeatureDetector;
  17. import org.opencv.imgcodecs.Imgcodecs;
  18. import org.opencv.imgproc.*;
  19. import org.opencv.objdetect.*;
  20.  
  21. /**
  22. * GripPipeline class.
  23. *
  24. * <p>An OpenCV pipeline generated by GRIP.
  25. *
  26. * @author GRIP
  27. */
  28. public class GripPipeline implements VisionPipeline {
  29.  
  30.     //Outputs
  31.     private Mat resizeImageOutput = new Mat();
  32.     private Mat rgbThresholdOutput = new Mat();
  33.     private Mat cvErodeOutput = new Mat();
  34.     private ArrayList<MatOfPoint> findContoursOutput = new ArrayList<MatOfPoint>();
  35.     private ArrayList<MatOfPoint> filterContoursOutput = new ArrayList<MatOfPoint>();
  36.  
  37.     static {
  38.         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  39.     }
  40.  
  41.     /**
  42.      * This is the primary method that runs the entire pipeline and updates the outputs.
  43.      */
  44.     @Override   public void process(Mat source0) {
  45.         // Step Resize_Image0:
  46.         Mat resizeImageInput = source0;
  47.         double resizeImageWidth = 640.0;
  48.         double resizeImageHeight = 480.0;
  49.         int resizeImageInterpolation = Imgproc.INTER_CUBIC;
  50.         resizeImage(resizeImageInput, resizeImageWidth, resizeImageHeight, resizeImageInterpolation, resizeImageOutput);
  51.  
  52.         // Step RGB_Threshold0:
  53.         Mat rgbThresholdInput = resizeImageOutput;
  54.         double[] rgbThresholdRed = {115,254};
  55.         double[] rgbThresholdGreen = {151, 255.0};
  56.         double[] rgbThresholdBlue = {0.0, 221.0};
  57.         rgbThreshold(rgbThresholdInput, rgbThresholdRed, rgbThresholdGreen, rgbThresholdBlue, rgbThresholdOutput);
  58.  
  59.         // Step CV_erode0:
  60.         Mat cvErodeSrc = rgbThresholdOutput;
  61.         Mat cvErodeKernel = new Mat();
  62.         Point cvErodeAnchor = new Point(-1, -1);
  63.         double cvErodeIterations = 2.0;
  64.         int cvErodeBordertype = Core.BORDER_CONSTANT;
  65.         Scalar cvErodeBordervalue = new Scalar(-1);
  66.         cvErode(cvErodeSrc, cvErodeKernel, cvErodeAnchor, cvErodeIterations, cvErodeBordertype, cvErodeBordervalue, cvErodeOutput);
  67.  
  68.         // Step Find_Contours0:
  69.         Mat findContoursInput = cvErodeOutput;
  70.         boolean findContoursExternalOnly = true;
  71.         findContours(findContoursInput, findContoursExternalOnly, findContoursOutput);
  72.  
  73.         // Step Filter_Contours0:
  74.         ArrayList<MatOfPoint> filterContoursContours = findContoursOutput;
  75.         double filterContoursMinArea = 500.0;
  76.         double filterContoursMinPerimeter = 0.0;
  77.         double filterContoursMinWidth = 0.0;
  78.         double filterContoursMaxWidth = 100000.0;
  79.         double filterContoursMinHeight = 0.0;
  80.         double filterContoursMaxHeight = 1000000.0;
  81.         double[] filterContoursSolidity = {0.0, 100.0};
  82.         double filterContoursMaxVertices = 1000.0;
  83.         double filterContoursMinVertices = 0.0;
  84.         double filterContoursMinRatio = 0.0;
  85.         double filterContoursMaxRatio = 1000.0;
  86.         filterContours(filterContoursContours, filterContoursMinArea, filterContoursMinPerimeter, filterContoursMinWidth, filterContoursMaxWidth, filterContoursMinHeight, filterContoursMaxHeight, filterContoursSolidity, filterContoursMaxVertices, filterContoursMinVertices, filterContoursMinRatio, filterContoursMaxRatio, filterContoursOutput);
  87.  
  88.     }
  89.  
  90.     /**
  91.      * This method is a generated getter for the output of a Resize_Image.
  92.      * @return Mat output from Resize_Image.
  93.      */
  94.     public Mat resizeImageOutput() {
  95.         return resizeImageOutput;
  96.     }
  97.  
  98.     /**
  99.      * This method is a generated getter for the output of a RGB_Threshold.
  100.      * @return Mat output from RGB_Threshold.
  101.      */
  102.     public Mat rgbThresholdOutput() {
  103.         return rgbThresholdOutput;
  104.     }
  105.  
  106.     /**
  107.      * This method is a generated getter for the output of a CV_erode.
  108.      * @return Mat output from CV_erode.
  109.      */
  110.     public Mat cvErodeOutput() {
  111.         return cvErodeOutput;
  112.     }
  113.  
  114.     /**
  115.      * This method is a generated getter for the output of a Find_Contours.
  116.      * @return ArrayList<MatOfPoint> output from Find_Contours.
  117.      */
  118.     public ArrayList<MatOfPoint> findContoursOutput() {
  119.         return findContoursOutput;
  120.     }
  121.  
  122.     /**
  123.      * This method is a generated getter for the output of a Filter_Contours.
  124.      * @return ArrayList<MatOfPoint> output from Filter_Contours.
  125.      */
  126.     public ArrayList<MatOfPoint> filterContoursOutput() {
  127.         return filterContoursOutput;
  128.     }
  129.  
  130.  
  131.     /**
  132.      * Scales and image to an exact size.
  133.      * @param input The image on which to perform the Resize.
  134.      * @param width The width of the output in pixels.
  135.      * @param height The height of the output in pixels.
  136.      * @param interpolation The type of interpolation.
  137.      * @param output The image in which to store the output.
  138.      */
  139.     private void resizeImage(Mat input, double width, double height,
  140.         int interpolation, Mat output) {
  141.         Imgproc.resize(input, output, new Size(width, height), 0.0, 0.0, interpolation);
  142.     }
  143.  
  144.     /**
  145.      * Segment an image based on color ranges.
  146.      * @param input The image on which to perform the RGB threshold.
  147.      * @param red The min and max red.
  148.      * @param green The min and max green.
  149.      * @param blue The min and max blue.
  150.      * @param output The image in which to store the output.
  151.      */
  152.     private void rgbThreshold(Mat input, double[] red, double[] green, double[] blue,
  153.         Mat out) {
  154.         Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2RGB);
  155.         Core.inRange(out, new Scalar(red[0], green[0], blue[0]),
  156.             new Scalar(red[1], green[1], blue[1]), out);
  157.     }
  158.  
  159.     /**
  160.      * Expands area of lower value in an image.
  161.      * @param src the Image to erode.
  162.      * @param kernel the kernel for erosion.
  163.      * @param anchor the center of the kernel.
  164.      * @param iterations the number of times to perform the erosion.
  165.      * @param borderType pixel extrapolation method.
  166.      * @param borderValue value to be used for a constant border.
  167.      * @param dst Output Image.
  168.      */
  169.     private void cvErode(Mat src, Mat kernel, Point anchor, double iterations,
  170.         int borderType, Scalar borderValue, Mat dst) {
  171.         if (kernel == null) {
  172.             kernel = new Mat();
  173.         }
  174.         if (anchor == null) {
  175.             anchor = new Point(-1,-1);
  176.         }
  177.         if (borderValue == null) {
  178.             borderValue = new Scalar(-1);
  179.         }
  180.         Imgproc.erode(src, dst, kernel, anchor, (int)iterations, borderType, borderValue);
  181.     }
  182.  
  183.     /**
  184.      * Sets the values of pixels in a binary image to their distance to the nearest black pixel.
  185.      * @param input The image on which to perform the Distance Transform.
  186.      * @param type The Transform.
  187.      * @param maskSize the size of the mask.
  188.      * @param output The image in which to store the output.
  189.      */
  190.     private void findContours(Mat input, boolean externalOnly,
  191.         List<MatOfPoint> contours) {
  192.         Mat hierarchy = new Mat();
  193.         contours.clear();
  194.         int mode;
  195.         if (externalOnly) {
  196.             mode = Imgproc.RETR_EXTERNAL;
  197.         }
  198.         else {
  199.             mode = Imgproc.RETR_LIST;
  200.         }
  201.         int method = Imgproc.CHAIN_APPROX_SIMPLE;
  202.         Imgproc.findContours(input, contours, hierarchy, mode, method);
  203.     }
  204.  
  205.  
  206.     /**
  207.      * Filters out contours that do not meet certain criteria.
  208.      * @param inputContours is the input list of contours
  209.      * @param output is the the output list of contours
  210.      * @param minArea is the minimum area of a contour that will be kept
  211.      * @param minPerimeter is the minimum perimeter of a contour that will be kept
  212.      * @param minWidth minimum width of a contour
  213.      * @param maxWidth maximum width
  214.      * @param minHeight minimum height
  215.      * @param maxHeight maximimum height
  216.      * @param Solidity the minimum and maximum solidity of a contour
  217.      * @param minVertexCount minimum vertex Count of the contours
  218.      * @param maxVertexCount maximum vertex Count
  219.      * @param minRatio minimum ratio of width to height
  220.      * @param maxRatio maximum ratio of width to height
  221.      */
  222.     private void filterContours(List<MatOfPoint> inputContours, double minArea,
  223.         double minPerimeter, double minWidth, double maxWidth, double minHeight, double
  224.         maxHeight, double[] solidity, double maxVertexCount, double minVertexCount, double
  225.         minRatio, double maxRatio, List<MatOfPoint> output) {
  226.         final MatOfInt hull = new MatOfInt();
  227.         output.clear();
  228.         //operation
  229.         for (int i = 0; i < inputContours.size(); i++) {
  230.             final MatOfPoint contour = inputContours.get(i);
  231.             final Rect bb = Imgproc.boundingRect(contour);
  232.             if (bb.width < minWidth || bb.width > maxWidth) continue;
  233.             if (bb.height < minHeight || bb.height > maxHeight) continue;
  234.             final double area = Imgproc.contourArea(contour);
  235.             if (area < minArea) continue;
  236.             if (Imgproc.arcLength(new MatOfPoint2f(contour.toArray()), true) < minPerimeter) continue;
  237.             Imgproc.convexHull(contour, hull);
  238.             MatOfPoint mopHull = new MatOfPoint();
  239.             mopHull.create((int) hull.size().height, 1, CvType.CV_32SC2);
  240.             for (int j = 0; j < hull.size().height; j++) {
  241.                 int index = (int)hull.get(j, 0)[0];
  242.                 double[] point = new double[] { contour.get(index, 0)[0], contour.get(index, 0)[1]};
  243.                 mopHull.put(j, 0, point);
  244.             }
  245.             final double solid = 100 * area / Imgproc.contourArea(mopHull);
  246.             if (solid < solidity[0] || solid > solidity[1]) continue;
  247.             if (contour.rows() < minVertexCount || contour.rows() > maxVertexCount) continue;
  248.             final double ratio = bb.width / (double)bb.height;
  249.             if (ratio < minRatio || ratio > maxRatio) continue;
  250.             output.add(contour);
  251.         }
  252.     }
  253.  
  254.  
  255.  
  256.  
  257. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top