jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 1 | package org.frc971;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 4 | import java.util.logging.Logger;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 5 |
|
| 6 | import com.googlecode.javacv.cpp.opencv_core;
|
| 7 | import com.googlecode.javacv.cpp.opencv_core.CvSize;
|
| 8 | import com.googlecode.javacv.cpp.opencv_core.IplImage;
|
| 9 | import com.googlecode.javacv.cpp.opencv_imgproc;
|
| 10 | import com.googlecode.javacv.cpp.opencv_imgproc.IplConvKernel;
|
| 11 |
|
| 12 | import edu.wpi.first.wpijavacv.DaisyExtensions;
|
| 13 | import edu.wpi.first.wpijavacv.WPIBinaryImage;
|
| 14 | import edu.wpi.first.wpijavacv.WPIColor;
|
| 15 | import edu.wpi.first.wpijavacv.WPIColorImage;
|
| 16 | import edu.wpi.first.wpijavacv.WPIContour;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 17 | import edu.wpi.first.wpijavacv.WPIPoint;
|
| 18 | import edu.wpi.first.wpijavacv.WPIPolygon;
|
| 19 |
|
| 20 | /**
|
| 21 | * Vision target recognizer for FRC 2013.
|
| 22 | *
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 23 | * @author jrussell
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 24 | * @author jerry
|
| 25 | */
|
| 26 | public class Recognizer2013 implements Recognizer {
|
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 27 |
|
| 28 | private final static Logger LOG = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 29 |
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 30 | // --- Tunable recognizer constants.
|
jerrym | cd2c332 | 2013-02-18 08:49:01 +0000 | [diff] [blame] | 31 | static final double kRoughlyHorizontalSlope = Math.tan(Math.toRadians(30));
|
| 32 | static final double kRoughlyVerticalSlope = Math.tan(Math.toRadians(90 - 30));
|
jerrym | cd2c332 | 2013-02-18 08:49:01 +0000 | [diff] [blame] | 33 | static final int kHoleClosingIterations = 2;
|
| 34 | static final double kPolygonPercentFit = 12;
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 35 | static final int kMinWidthAt320 = 35; // for high goal and middle goals
|
| 36 |
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 37 | // --- Field dimensions.
|
| 38 | // The target aspect ratios are for the midlines of the vision target tape.
|
| 39 | static final double kGoalWidthIn = 54; // of the high and middle targets
|
| 40 | static final double kTargetWidthIn = kGoalWidthIn + 4;
|
| 41 | static final double kHighGoalAspect = (21 + 4) / kTargetWidthIn;
|
| 42 | static final double kMiddleGoalAspect = (24 + 4) / kTargetWidthIn;
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 43 | static final double kMinAspect = kHighGoalAspect * 0.6;
|
| 44 | static final double kMaxAspect = kMiddleGoalAspect * 1.4;
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 45 | static final double kTopTargetHeightIn = 104.125 + 21.0/2; // center of target
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 46 |
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 47 | // --- Robot and camera dimensions.
|
| 48 | static final double kShooterOffsetDeg = 0; // azimuth offset from camera to shooter
|
| 49 | static final double kHorizontalFOVDeg = 44.0; // Logitech C210 camera
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 50 | static final double kVerticalFOVDeg = 480.0 / 640.0 * kHorizontalFOVDeg;
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 51 | static final double kCameraHeightIn = 24.0; // TODO
|
| 52 | static final double kCameraPitchDeg = 21.0; // TODO
|
| 53 | static final double kTanHFOV2 = Math.tan(Math.toRadians(kHorizontalFOVDeg / 2));
|
| 54 | static final double kTanVFOV2 = Math.tan(Math.toRadians(kVerticalFOVDeg / 2));
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 55 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 56 | // --- Colors for drawing indicators on the image.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 57 | private static final WPIColor reject1Color = WPIColor.GRAY;
|
| 58 | private static final WPIColor reject2Color = WPIColor.YELLOW;
|
| 59 | private static final WPIColor candidateColor = WPIColor.BLUE;
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 60 | private static final WPIColor targetColor = WPIColor.RED;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 61 |
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 62 | // --- Color thresholds, initialized in the constructor.
|
jerrym | cd2469c | 2013-02-18 20:15:28 +0000 | [diff] [blame] | 63 | private int min1Hue, max1Hue, min1Sat, min1Val;
|
| 64 |
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 65 | // Show intermediate images for parameter tuning.
|
| 66 | private final DebugCanvas thresholdedCanvas = new DebugCanvas("thresholded");
|
| 67 | private final DebugCanvas morphedCanvas = new DebugCanvas("morphed");
|
| 68 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 69 | // Data to reuse for each frame.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 70 | private final DaisyExtensions daisyExtensions = new DaisyExtensions();
|
| 71 | private final IplConvKernel morphKernel = IplConvKernel.create(3, 3, 1, 1,
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 72 | opencv_imgproc.CV_SHAPE_RECT, null);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 73 | private final ArrayList<WPIPolygon> polygons = new ArrayList<WPIPolygon>();
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 74 |
|
| 75 | // Frame-size-dependent data to reuse for each frame.
|
| 76 | private CvSize size = null;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 77 | private WPIColorImage rawImage;
|
| 78 | private IplImage bin;
|
| 79 | private IplImage hsv;
|
| 80 | private IplImage hue;
|
| 81 | private IplImage sat;
|
| 82 | private IplImage val;
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 83 | private int minWidth;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 84 | private WPIPoint linePt1, linePt2; // crosshair endpoints
|
| 85 |
|
| 86 | public Recognizer2013() {
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 87 | setHSVRange(70, 106, 137, 27);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 88 | }
|
| 89 |
|
| 90 | @Override
|
jerrym | cd2c332 | 2013-02-18 08:49:01 +0000 | [diff] [blame] | 91 | public void setHSVRange(int minHue, int maxHue, int minSat, int minVal) {
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 92 | min1Hue = minHue - 1; // - 1 because cvThreshold() does > instead of >=
|
| 93 | max1Hue = maxHue + 1;
|
| 94 | min1Sat = minSat - 1;
|
| 95 | min1Val = minVal - 1;
|
jerrym | cd2c332 | 2013-02-18 08:49:01 +0000 | [diff] [blame] | 96 | }
|
| 97 | @Override
|
| 98 | public int getHueMin() { return min1Hue + 1; }
|
| 99 | @Override
|
| 100 | public int getHueMax() { return max1Hue - 1; }
|
| 101 | @Override
|
jerrym | cd2469c | 2013-02-18 20:15:28 +0000 | [diff] [blame] | 102 | public int getSatMin() { return min1Sat + 1; }
|
jerrym | cd2c332 | 2013-02-18 08:49:01 +0000 | [diff] [blame] | 103 | @Override
|
jerrym | cd2469c | 2013-02-18 20:15:28 +0000 | [diff] [blame] | 104 | public int getValMin() { return min1Val + 1; }
|
jerrym | cd2c332 | 2013-02-18 08:49:01 +0000 | [diff] [blame] | 105 |
|
| 106 | @Override
|
jerrym | f96c32c | 2013-02-18 19:30:45 +0000 | [diff] [blame] | 107 | public void showIntermediateStages(boolean enable) {
|
| 108 | thresholdedCanvas.show = enable;
|
| 109 | morphedCanvas.show = enable;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
danielp | 3c598e5 | 2013-02-24 06:12:54 +0000 | [diff] [blame] | 113 | public Target processImage(WPIColorImage cameraImage) {
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 114 | // (Re)allocate the intermediate images if the input is a different
|
| 115 | // size than the previous image.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 116 | if (size == null || size.width() != cameraImage.getWidth()
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 117 | || size.height() != cameraImage.getHeight()) {
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 118 | size = opencv_core.cvSize(cameraImage.getWidth(),
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 119 | cameraImage.getHeight());
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 120 | rawImage = DaisyExtensions.makeWPIColorImage(
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 121 | DaisyExtensions.getIplImage(cameraImage));
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 122 | bin = IplImage.create(size, 8, 1);
|
| 123 | hsv = IplImage.create(size, 8, 3);
|
| 124 | hue = IplImage.create(size, 8, 1);
|
| 125 | sat = IplImage.create(size, 8, 1);
|
| 126 | val = IplImage.create(size, 8, 1);
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 127 | minWidth = (kMinWidthAt320 * cameraImage.getWidth() + 319) / 320;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 128 |
|
| 129 | int horizontalOffsetPixels = (int)Math.round(
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 130 | kShooterOffsetDeg * size.width() / kHorizontalFOVDeg);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 131 | int x = size.width() / 2 + horizontalOffsetPixels;
|
| 132 | linePt1 = new WPIPoint(x, size.height() - 1);
|
| 133 | linePt2 = new WPIPoint(x, 0);
|
| 134 | } else {
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 135 | // Copy the camera image so it's safe to draw on.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 136 | opencv_core.cvCopy(DaisyExtensions.getIplImage(cameraImage),
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 137 | DaisyExtensions.getIplImage(rawImage));
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 138 | }
|
| 139 |
|
| 140 | IplImage input = DaisyExtensions.getIplImage(rawImage);
|
| 141 |
|
| 142 | // Threshold the pixels in HSV color space.
|
| 143 | // TODO(jerry): Do this in one pass of a pixel-processing loop.
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 144 | opencv_imgproc.cvCvtColor(input, hsv, opencv_imgproc.CV_BGR2HSV_FULL);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 145 | opencv_core.cvSplit(hsv, hue, sat, val, null);
|
| 146 |
|
| 147 | // NOTE: Since red is at the end of the cyclic color space, you can OR
|
| 148 | // a threshold and an inverted threshold to match red pixels.
|
jerrym | cd2c332 | 2013-02-18 08:49:01 +0000 | [diff] [blame] | 149 | opencv_imgproc.cvThreshold(hue, bin, min1Hue, 255, opencv_imgproc.CV_THRESH_BINARY);
|
| 150 | opencv_imgproc.cvThreshold(hue, hue, max1Hue, 255, opencv_imgproc.CV_THRESH_BINARY_INV);
|
| 151 | opencv_imgproc.cvThreshold(sat, sat, min1Sat, 255, opencv_imgproc.CV_THRESH_BINARY);
|
| 152 | opencv_imgproc.cvThreshold(val, val, min1Val, 255, opencv_imgproc.CV_THRESH_BINARY);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 153 |
|
| 154 | // Combine the results to obtain a binary image which is mostly the
|
| 155 | // interesting pixels.
|
| 156 | opencv_core.cvAnd(hue, bin, bin, null);
|
| 157 | opencv_core.cvAnd(bin, sat, bin, null);
|
| 158 | opencv_core.cvAnd(bin, val, bin, null);
|
| 159 |
|
| 160 | thresholdedCanvas.showImage(bin);
|
| 161 |
|
| 162 | // Fill in gaps using binary morphology.
|
| 163 | opencv_imgproc.cvMorphologyEx(bin, bin, null, morphKernel,
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 164 | opencv_imgproc.CV_MOP_CLOSE, kHoleClosingIterations);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 165 |
|
| 166 | morphedCanvas.showImage(bin);
|
| 167 |
|
| 168 | // Find contours.
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 169 | //
|
| 170 | // NOTE: If we distinguished between the inner and outer boundaries of
|
| 171 | // the vision target rectangles, we could apply a more accurate width
|
| 172 | // filter and more accurately compute the target range.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 173 | WPIBinaryImage binWpi = DaisyExtensions.makeWPIBinaryImage(bin);
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 174 | WPIContour[] contours = daisyExtensions.findConvexContours(binWpi);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 175 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 176 | // Simplify the contours to polygons and filter by size and aspect ratio.
|
| 177 | //
|
| 178 | // TODO(jerry): Also look for the two vertical stripe vision targets.
|
| 179 | // They'll greatly increase the precision of measuring the distance. If
|
| 180 | // both stripes are visible, they'll increase the accuracy for
|
| 181 | // identifying the high goal.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 182 | polygons.clear();
|
| 183 | for (WPIContour c : contours) {
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 184 | if (c.getWidth() >= minWidth) {
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 185 | double ratio = ((double) c.getHeight()) / c.getWidth();
|
| 186 | if (ratio >= kMinAspect && ratio <= kMaxAspect) {
|
| 187 | polygons.add(c.approxPolygon(kPolygonPercentFit));
|
| 188 | // System.out.println(" Accepted aspect ratio " + ratio);
|
| 189 | } else {
|
| 190 | // System.out.println(" Rejected aspect ratio " + ratio);
|
| 191 | }
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 192 | }
|
| 193 | }
|
| 194 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 195 | // Pick the target with the highest center-point that matches yet more
|
| 196 | // filter criteria.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 197 | WPIPolygon bestTarget = null;
|
| 198 | int highestY = Integer.MAX_VALUE;
|
| 199 |
|
| 200 | for (WPIPolygon p : polygons) {
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 201 | // TODO(jerry): Replace boolean filters with a scoring function?
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 202 | if (p.isConvex() && p.getNumVertices() == 4) { // quadrilateral
|
| 203 | WPIPoint[] points = p.getPoints();
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 204 | // Filter for polygons with 2 ~horizontal and 2 ~vertical sides.
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 205 | int numRoughlyHorizontal = 0;
|
| 206 | int numRoughlyVertical = 0;
|
| 207 | for (int i = 0; i < 4; ++i) {
|
| 208 | double dy = points[i].getY() - points[(i + 1) % 4].getY();
|
| 209 | double dx = points[i].getX() - points[(i + 1) % 4].getX();
|
| 210 | double slope = Double.MAX_VALUE;
|
| 211 | if (dx != 0) {
|
| 212 | slope = Math.abs(dy / dx);
|
| 213 | }
|
| 214 |
|
| 215 | if (slope < kRoughlyHorizontalSlope) {
|
| 216 | ++numRoughlyHorizontal;
|
| 217 | } else if (slope > kRoughlyVerticalSlope) {
|
| 218 | ++numRoughlyVertical;
|
| 219 | }
|
| 220 | }
|
| 221 |
|
jerrym | aa7a63b | 2013-02-18 06:31:22 +0000 | [diff] [blame] | 222 | if (numRoughlyHorizontal >= 2 && numRoughlyVertical == 2) {
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 223 | int pCenterX = p.getX() + p.getWidth() / 2;
|
| 224 | int pCenterY = p.getY() + p.getHeight() / 2;
|
| 225 |
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 226 | rawImage.drawPolygon(p, candidateColor, 2);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 227 | rawImage.drawPoint(new WPIPoint(pCenterX, pCenterY),
|
jerrym | dda6013 | 2013-02-18 09:25:03 +0000 | [diff] [blame] | 228 | targetColor, 2);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 229 | if (pCenterY < highestY) {
|
| 230 | bestTarget = p;
|
| 231 | highestY = pCenterY;
|
| 232 | }
|
| 233 | } else {
|
| 234 | rawImage.drawPolygon(p, reject2Color, 1);
|
| 235 | }
|
| 236 | } else {
|
| 237 | rawImage.drawPolygon(p, reject1Color, 1);
|
| 238 | }
|
| 239 | }
|
| 240 |
|
danielp | 3c598e5 | 2013-02-24 06:12:54 +0000 | [diff] [blame] | 241 | Target found = null;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 242 | if (bestTarget != null) {
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 243 | rawImage.drawPolygon(bestTarget, targetColor, 2);
|
danielp | 3c598e5 | 2013-02-24 06:12:54 +0000 | [diff] [blame] | 244 | found = measureTarget(bestTarget);
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 245 | } else {
|
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 246 | LOG.fine("No target found");
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 247 | }
|
| 248 |
|
| 249 | // Draw a crosshair
|
| 250 | rawImage.drawLine(linePt1, linePt2, targetColor, 1);
|
danielp | 3c598e5 | 2013-02-24 06:12:54 +0000 | [diff] [blame] | 251 |
|
| 252 | if (found == null) {
|
| 253 | found = new Target();
|
| 254 | }
|
| 255 | found.editedPicture = rawImage;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 256 |
|
| 257 | daisyExtensions.releaseMemory();
|
| 258 | //System.gc();
|
danielp | 3c598e5 | 2013-02-24 06:12:54 +0000 | [diff] [blame] | 259 |
|
| 260 | return found;
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 261 | }
|
| 262 |
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 263 | /**
|
| 264 | * Uses the camera, field, and robot dimensions to compute targeting info.
|
| 265 | */
|
danielp | 3c598e5 | 2013-02-24 06:12:54 +0000 | [diff] [blame] | 266 | private Target measureTarget(WPIPolygon target) {
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 267 | double w = target.getWidth();
|
| 268 | double h = target.getHeight();
|
| 269 | double x = target.getX() + w / 2; // target center in view coords
|
| 270 | double y = target.getY() + h / 2;
|
| 271 |
|
| 272 | double vw = size.width();
|
| 273 | double vh = size.height();
|
| 274 | double xc = x - vw / 2; // target center pos'n ±from view center
|
| 275 | double yc = vh / 2 - y; // ... in world coords on the viewing plane
|
| 276 |
|
| 277 | // Target angles relative to the camera.
|
| 278 | double azimuthCam = Math.atan2(xc * 2 * kTanHFOV2, vw);
|
| 279 | double elevationCam = Math.atan2(yc * 2 * kTanVFOV2, vh);
|
| 280 | double rangeIn = kTargetWidthIn * vw / (w * 2 * kTanHFOV2);
|
| 281 |
|
danielp | 3c598e5 | 2013-02-24 06:12:54 +0000 | [diff] [blame] | 282 | //Put results in target
|
| 283 | Target data = new Target();
|
| 284 | data.azimuth = (Math.toDegrees(azimuthCam) - kShooterOffsetDeg);
|
| 285 | data.elevation = (Math.toDegrees(elevationCam));
|
| 286 | data.range = (rangeIn / 12);
|
| 287 |
|
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 288 | LOG.fine("Best target at (" + x + ", " + y + ") " + w +" x " + h
|
| 289 | + ", shot azimuth=" + (Math.toDegrees(azimuthCam) - kShooterOffsetDeg) +
|
| 290 | " elevation=" + (Math.toDegrees(elevationCam) + kCameraPitchDeg) +
|
| 291 | " range=" + (rangeIn / 12));
|
danielp | 3c598e5 | 2013-02-24 06:12:54 +0000 | [diff] [blame] | 292 |
|
| 293 | return data;
|
jerrym | f0c8455 | 2013-02-19 00:51:20 +0000 | [diff] [blame] | 294 | }
|
| 295 |
|
jerrym | 6ebe645 | 2013-02-18 03:00:31 +0000 | [diff] [blame] | 296 | }
|