|
| 1 | +package org.firstinspires.ftc.teamcode.vision; |
| 2 | + |
| 3 | +import org.firstinspires.ftc.robotcore.external.Telemetry; |
| 4 | +import org.opencv.core.Core; |
| 5 | +import org.opencv.core.Mat; |
| 6 | +import org.opencv.core.Point; |
| 7 | +import org.opencv.core.Rect; |
| 8 | +import org.opencv.core.Scalar; |
| 9 | +import org.opencv.imgproc.Imgproc; |
| 10 | +import org.openftc.easyopencv.OpenCvPipeline; |
| 11 | + |
| 12 | +public class VisionTesting extends OpenCvPipeline { |
| 13 | + |
| 14 | + Telemetry telemetry; |
| 15 | + Mat mat = new Mat(); |
| 16 | + |
| 17 | + PiecePosition piecePosition; |
| 18 | + |
| 19 | + |
| 20 | + public enum PiecePosition { |
| 21 | + LEFT, |
| 22 | + RIGHT, |
| 23 | + MIDDLE, |
| 24 | + NOT_FOUND |
| 25 | + } |
| 26 | + |
| 27 | + static final Rect leftPos = new Rect( |
| 28 | + new Point(0, 0), |
| 29 | + new Point(426, 720)); |
| 30 | + static final Rect midPos = new Rect( |
| 31 | + new Point(426, 0), |
| 32 | + new Point(852, 720)); |
| 33 | + static final Rect rightPos = new Rect( |
| 34 | + new Point(852, 0), |
| 35 | + new Point(1278, 720)); |
| 36 | + |
| 37 | + public VisionTesting(Telemetry t) { |
| 38 | + telemetry = t; |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + public Mat processFrame(Mat input, String type) { |
| 43 | + |
| 44 | + Scalar colorLowerBound; |
| 45 | + Scalar colorUpperBound; |
| 46 | + |
| 47 | + Imgproc.cvtColor(input, mat, Imgproc.COLOR_RGB2HSV); |
| 48 | + if (type.equals("Red")) { |
| 49 | + colorLowerBound = new Scalar(0, 40, 40); |
| 50 | + colorUpperBound = new Scalar(20, 255, 255); |
| 51 | + } else { |
| 52 | + colorLowerBound = new Scalar(200, 40, 40); |
| 53 | + colorUpperBound = new Scalar(210, 255, 255); |
| 54 | + } |
| 55 | + final double percentColorThreshold = 0.02; |
| 56 | + Core.inRange(mat, colorLowerBound, colorUpperBound, mat); |
| 57 | + |
| 58 | + Mat left = mat.submat(leftPos); |
| 59 | + Mat middle = mat.submat(midPos); |
| 60 | + Mat right = mat.submat(rightPos); |
| 61 | + |
| 62 | + double leftValue = Core.sumElems(left).val[0] / leftPos.area() / 255; |
| 63 | + left.release(); |
| 64 | + |
| 65 | + if (leftValue > percentColorThreshold) { |
| 66 | + piecePosition = PiecePosition.LEFT; |
| 67 | + } else { |
| 68 | + |
| 69 | + double middleValue = Core.sumElems(middle).val[0] / midPos.area() / 255; |
| 70 | + middle.release(); |
| 71 | + |
| 72 | + if (middleValue > percentColorThreshold) { |
| 73 | + piecePosition = PiecePosition.MIDDLE; |
| 74 | + } else { |
| 75 | + |
| 76 | + double rightValue = Core.sumElems(right).val[0] / rightPos.area() / 255; |
| 77 | + right.release(); |
| 78 | + |
| 79 | + if (rightValue > percentColorThreshold) { |
| 80 | + piecePosition = PiecePosition.RIGHT; |
| 81 | + } else { |
| 82 | + piecePosition = PiecePosition.NOT_FOUND; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + return null; |
| 89 | + } |
| 90 | + @Override |
| 91 | + public Mat processFrame(Mat input) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + } |
0 commit comments