jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 1 | package org.frc971;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 |
|
| 5 | import com.googlecode.javacv.cpp.opencv_core;
|
| 6 | import com.googlecode.javacv.cpp.opencv_core.CvSize;
|
| 7 | import com.googlecode.javacv.cpp.opencv_core.IplImage;
|
| 8 | import com.googlecode.javacv.cpp.opencv_imgproc;
|
| 9 | import com.googlecode.javacv.cpp.opencv_imgproc.IplConvKernel;
|
| 10 |
|
| 11 | import edu.wpi.first.wpijavacv.DaisyExtensions;
|
| 12 | import edu.wpi.first.wpijavacv.WPIBinaryImage;
|
| 13 | import edu.wpi.first.wpijavacv.WPIColor;
|
| 14 | import edu.wpi.first.wpijavacv.WPIColorImage;
|
| 15 | import edu.wpi.first.wpijavacv.WPIContour;
|
| 16 | import edu.wpi.first.wpijavacv.WPIImage;
|
| 17 | import edu.wpi.first.wpijavacv.WPIPoint;
|
| 18 | import edu.wpi.first.wpijavacv.WPIPolygon;
|
| 19 |
|
| 20 | /**
|
| 21 | * Vision target recognizer for FRC 2013.
|
| 22 | *
|
| 23 | * @author jerry
|
| 24 | */
|
| 25 | public class Recognizer2013 implements Recognizer {
|
| 26 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 27 | // --- Constants that need to be tuned.
|
| 28 | static final double kRoughlyHorizontalSlope = Math.tan(Math.toRadians(25));
|
| 29 | static final double kRoughlyVerticalSlope = Math.tan(Math.toRadians(90 - 25));
|
| 30 | static final double kMin1Hue = 55 - 1; // - 1 because cvThreshold() does > not >=
|
| 31 | static final double kMax1Hue = 118 + 1;
|
| 32 | static final double kMin1Sat = 80 - 1;
|
| 33 | static final double kMin1Val = 69 - 1;
|
| 34 | static final int kHoleClosingIterations = 3;
|
| 35 | static final double kPolygonPercentFit = 12; // was 20
|
| 36 |
|
| 37 | static final int kMinWidthAt320 = 35; // for high goal and middle goals
|
| 38 |
|
| 39 | // These aspect ratios include the outside edges of the vision target tape.
|
| 40 | static final double kHighGoalAspect = (21 + 8.0) / (54 + 8);
|
| 41 | static final double kMiddleGoalAspect = (24 + 8.0) / (54 + 8);
|
| 42 | static final double kMinAspect = kHighGoalAspect * 0.6;
|
| 43 | static final double kMaxAspect = kMiddleGoalAspect * 1.4;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 44 |
|
| 45 | static final double kShooterOffsetDeg = 0;
|
| 46 | static final double kHorizontalFOVDeg = 47.0;
|
| 47 | static final double kVerticalFOVDeg = 480.0 / 640.0 * kHorizontalFOVDeg;
|
| 48 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 49 | // --- Colors for drawing indicators on the image.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 50 | private static final WPIColor reject1Color = WPIColor.GRAY;
|
| 51 | private static final WPIColor reject2Color = WPIColor.YELLOW;
|
| 52 | private static final WPIColor candidateColor = WPIColor.BLUE;
|
| 53 | private static final WPIColor targetColor = new WPIColor(255, 0, 0);
|
| 54 |
|
| 55 | // Show intermediate images for parameter tuning.
|
| 56 | private final DebugCanvas thresholdedCanvas = new DebugCanvas("thresholded");
|
| 57 | private final DebugCanvas morphedCanvas = new DebugCanvas("morphed");
|
| 58 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 59 | // Data to reuse for each frame.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 60 | private final DaisyExtensions daisyExtensions = new DaisyExtensions();
|
| 61 | private final IplConvKernel morphKernel = IplConvKernel.create(3, 3, 1, 1,
|
| 62 | opencv_imgproc.CV_SHAPE_RECT, null);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 63 | private final ArrayList<WPIPolygon> polygons = new ArrayList<WPIPolygon>();
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 64 |
|
| 65 | // Frame-size-dependent data to reuse for each frame.
|
| 66 | private CvSize size = null;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 67 | private WPIColorImage rawImage;
|
| 68 | private IplImage bin;
|
| 69 | private IplImage hsv;
|
| 70 | private IplImage hue;
|
| 71 | private IplImage sat;
|
| 72 | private IplImage val;
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 73 | private int minWidth;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 74 | private WPIPoint linePt1, linePt2; // crosshair endpoints
|
| 75 |
|
| 76 | public Recognizer2013() {
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public WPIImage processImage(WPIColorImage cameraImage) {
|
| 81 | // (Re)allocate the intermediate images if the input is a different
|
| 82 | // size than the previous image.
|
| 83 | if (size == null || size.width() != cameraImage.getWidth()
|
| 84 | || size.height() != cameraImage.getHeight()) {
|
| 85 | size = opencv_core.cvSize(cameraImage.getWidth(),
|
| 86 | cameraImage.getHeight());
|
| 87 | rawImage = DaisyExtensions.makeWPIColorImage(
|
| 88 | DaisyExtensions.getIplImage(cameraImage));
|
| 89 | bin = IplImage.create(size, 8, 1);
|
| 90 | hsv = IplImage.create(size, 8, 3);
|
| 91 | hue = IplImage.create(size, 8, 1);
|
| 92 | sat = IplImage.create(size, 8, 1);
|
| 93 | val = IplImage.create(size, 8, 1);
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 94 | minWidth = (kMinWidthAt320 * cameraImage.getWidth() + 319) / 320;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 95 |
|
| 96 | int horizontalOffsetPixels = (int)Math.round(
|
| 97 | kShooterOffsetDeg * size.width() / kHorizontalFOVDeg);
|
| 98 | int x = size.width() / 2 + horizontalOffsetPixels;
|
| 99 | linePt1 = new WPIPoint(x, size.height() - 1);
|
| 100 | linePt2 = new WPIPoint(x, 0);
|
| 101 | } else {
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 102 | // Copy the camera image so it's safe to draw on.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 103 | opencv_core.cvCopy(DaisyExtensions.getIplImage(cameraImage),
|
| 104 | DaisyExtensions.getIplImage(rawImage));
|
| 105 | }
|
| 106 |
|
| 107 | IplImage input = DaisyExtensions.getIplImage(rawImage);
|
| 108 |
|
| 109 | // Threshold the pixels in HSV color space.
|
| 110 | // TODO(jerry): Do this in one pass of a pixel-processing loop.
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 111 | opencv_imgproc.cvCvtColor(input, hsv, opencv_imgproc.CV_BGR2HSV_FULL);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 112 | opencv_core.cvSplit(hsv, hue, sat, val, null);
|
| 113 |
|
| 114 | // NOTE: Since red is at the end of the cyclic color space, you can OR
|
| 115 | // a threshold and an inverted threshold to match red pixels.
|
| 116 | // TODO(jerry): Use tunable constants instead of literals.
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 117 | opencv_imgproc.cvThreshold(hue, bin, kMin1Hue, 255, opencv_imgproc.CV_THRESH_BINARY);
|
| 118 | opencv_imgproc.cvThreshold(hue, hue, kMax1Hue, 255, opencv_imgproc.CV_THRESH_BINARY_INV);
|
| 119 | opencv_imgproc.cvThreshold(sat, sat, kMin1Sat, 255, opencv_imgproc.CV_THRESH_BINARY);
|
| 120 | opencv_imgproc.cvThreshold(val, val, kMin1Val, 255, opencv_imgproc.CV_THRESH_BINARY);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 121 |
|
| 122 | // Combine the results to obtain a binary image which is mostly the
|
| 123 | // interesting pixels.
|
| 124 | opencv_core.cvAnd(hue, bin, bin, null);
|
| 125 | opencv_core.cvAnd(bin, sat, bin, null);
|
| 126 | opencv_core.cvAnd(bin, val, bin, null);
|
| 127 |
|
| 128 | thresholdedCanvas.showImage(bin);
|
| 129 |
|
| 130 | // Fill in gaps using binary morphology.
|
| 131 | opencv_imgproc.cvMorphologyEx(bin, bin, null, morphKernel,
|
| 132 | opencv_imgproc.CV_MOP_CLOSE, kHoleClosingIterations);
|
| 133 |
|
| 134 | morphedCanvas.showImage(bin);
|
| 135 |
|
| 136 | // Find contours.
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 137 | //
|
| 138 | // TODO(jerry): Request contours as a two-level hierarchy (blobs and
|
| 139 | // holes)? The targets have known sizes and their holes have known,
|
| 140 | // smaller sizes. This matters for distance measurement. OTOH it's moot
|
| 141 | // if/when we use the vertical stripes for distance measurement.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 142 | WPIBinaryImage binWpi = DaisyExtensions.makeWPIBinaryImage(bin);
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 143 | WPIContour[] contours = daisyExtensions.findConvexContours(binWpi);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 144 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 145 | // Simplify the contours to polygons and filter by size and aspect ratio.
|
| 146 | //
|
| 147 | // TODO(jerry): Also look for the two vertical stripe vision targets.
|
| 148 | // They'll greatly increase the precision of measuring the distance. If
|
| 149 | // both stripes are visible, they'll increase the accuracy for
|
| 150 | // identifying the high goal.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 151 | polygons.clear();
|
| 152 | for (WPIContour c : contours) {
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 153 | if (c.getWidth() >= minWidth) {
|
| 154 | double ratio = ((double) c.getHeight()) / c.getWidth();
|
| 155 | if (ratio >= kMinAspect && ratio <= kMaxAspect) {
|
| 156 | polygons.add(c.approxPolygon(kPolygonPercentFit));
|
| 157 | // System.out.println(" Accepted aspect ratio " + ratio);
|
| 158 | } else {
|
| 159 | // System.out.println(" Rejected aspect ratio " + ratio);
|
| 160 | }
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 161 | }
|
| 162 | }
|
| 163 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 164 | // Pick the target with the highest center-point that matches yet more
|
| 165 | // filter criteria.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 166 | WPIPolygon bestTarget = null;
|
| 167 | int highestY = Integer.MAX_VALUE;
|
| 168 |
|
| 169 | for (WPIPolygon p : polygons) {
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 170 | // TODO(jerry): Replace boolean filters with a scoring function?
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 171 | if (p.isConvex() && p.getNumVertices() == 4) { // quadrilateral
|
| 172 | WPIPoint[] points = p.getPoints();
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 173 | // Filter for polygons with 2 ~horizontal and 2 ~vertical sides.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 174 | int numRoughlyHorizontal = 0;
|
| 175 | int numRoughlyVertical = 0;
|
| 176 | for (int i = 0; i < 4; ++i) {
|
| 177 | double dy = points[i].getY() - points[(i + 1) % 4].getY();
|
| 178 | double dx = points[i].getX() - points[(i + 1) % 4].getX();
|
| 179 | double slope = Double.MAX_VALUE;
|
| 180 | if (dx != 0) {
|
| 181 | slope = Math.abs(dy / dx);
|
| 182 | }
|
| 183 |
|
| 184 | if (slope < kRoughlyHorizontalSlope) {
|
| 185 | ++numRoughlyHorizontal;
|
| 186 | } else if (slope > kRoughlyVerticalSlope) {
|
| 187 | ++numRoughlyVertical;
|
| 188 | }
|
| 189 | }
|
| 190 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 191 | if (numRoughlyHorizontal >= 2 && numRoughlyVertical == 2) {
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 192 | rawImage.drawPolygon(p, candidateColor, 2);
|
| 193 |
|
| 194 | int pCenterX = p.getX() + p.getWidth() / 2;
|
| 195 | int pCenterY = p.getY() + p.getHeight() / 2;
|
| 196 |
|
| 197 | rawImage.drawPoint(new WPIPoint(pCenterX, pCenterY),
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 198 | targetColor, 2);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 199 | if (pCenterY < highestY) {
|
| 200 | bestTarget = p;
|
| 201 | highestY = pCenterY;
|
| 202 | }
|
| 203 | } else {
|
| 204 | rawImage.drawPolygon(p, reject2Color, 1);
|
| 205 | }
|
| 206 | } else {
|
| 207 | rawImage.drawPolygon(p, reject1Color, 1);
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | if (bestTarget != null) {
|
| 212 | double w = bestTarget.getWidth();
|
| 213 | double h = bestTarget.getHeight();
|
| 214 | double x = bestTarget.getX() + w / 2;
|
| 215 | double y = bestTarget.getY() + h / 2;
|
| 216 |
|
| 217 | rawImage.drawPolygon(bestTarget, targetColor, 2);
|
| 218 |
|
| 219 | System.out.println("Best target at (" + x + ", " + y + ") size "
|
| 220 | + w + " x " + h);
|
| 221 | } else {
|
| 222 | System.out.println("No target found");
|
| 223 | }
|
| 224 |
|
| 225 | // Draw a crosshair
|
| 226 | rawImage.drawLine(linePt1, linePt2, targetColor, 1);
|
| 227 |
|
| 228 | daisyExtensions.releaseMemory();
|
| 229 | //System.gc();
|
| 230 |
|
| 231 | return rawImage;
|
| 232 | }
|
| 233 |
|
| 234 | }
|