Guest User

SmartDashboard Square Tracking Code FRC

a guest
Jan 20th, 2012
2,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. package edu.wpi.first.wpilibj.examples;
  2.  
  3. import edu.wpi.first.smartdashboard.camera.WPILaptopCameraExtension;
  4. import edu.wpi.first.smartdashboard.robot.Robot;
  5. import edu.wpi.first.wpijavacv.WPIBinaryImage;
  6. import edu.wpi.first.wpijavacv.WPIColor;
  7. import edu.wpi.first.wpijavacv.WPIColorImage;
  8. import edu.wpi.first.wpijavacv.WPIContour;
  9. import edu.wpi.first.wpijavacv.WPIImage;
  10. import edu.wpi.first.wpijavacv.WPIPoint;
  11. import edu.wpi.first.wpijavacv.WPIPolygon;
  12. import edu.wpi.first.wpilibj.networking.NetworkTable;
  13. import java.util.ArrayList;
  14.  
  15. /**
  16.  *
  17.  * @author Greg Granito
  18.  */
  19. public class SquareTrackerExtension extends WPILaptopCameraExtension {
  20.     public static final String NAME = "Laptop Camera Square Tracker";
  21.  
  22.     NetworkTable table = NetworkTable.getTable("camera");
  23.     WPIColor targetColor = new WPIColor(255, 0, 0);
  24.  
  25.     @Override
  26.     public WPIImage processImage(WPIColorImage rawImage) {
  27.  
  28.         WPIBinaryImage blueBin = rawImage.getBlueChannel().getThresholdInverted(60);
  29.         WPIBinaryImage greenBin = rawImage.getGreenChannel().getThresholdInverted(60);
  30.         WPIBinaryImage redBin = rawImage.getRedChannel().getThresholdInverted(60);
  31.  
  32.         WPIBinaryImage finalBin = blueBin.getAnd(redBin).getAnd(greenBin);
  33.  
  34.         finalBin.erode(2);
  35.         finalBin.dilate(6);
  36.  
  37.         WPIContour[] contours = finalBin.findContours();
  38.  
  39.         ArrayList<WPIPolygon> polygons = new ArrayList<WPIPolygon>();
  40.  
  41.         for(WPIContour c : contours){
  42.             double ratio = ((double)c.getHeight()) / ((double)c.getWidth());
  43.             if(ratio < 1.5 && ratio> 0.75){
  44.                 polygons.add(c.approxPolygon(45));
  45.             }
  46.         }
  47.  
  48.         ArrayList<WPIPolygon> possiblePolygons = new ArrayList<WPIPolygon>();
  49.  
  50.         for(WPIPolygon p : polygons){
  51.             if(p.isConvex() && p.getNumVertices() == 4){
  52.                 possiblePolygons.add(p);
  53.             }else{
  54.                 rawImage.drawPolygon(p, WPIColor.CYAN, 5);
  55.             }
  56.         }
  57.  
  58.         WPIPolygon square = null;
  59.         int squareArea = 0;
  60.  
  61.         for(WPIPolygon p : possiblePolygons){
  62.             rawImage.drawPolygon(p, WPIColor.GREEN, 5);
  63.             for(WPIPolygon q : possiblePolygons){
  64.                 if(p == q) continue;
  65.  
  66.                int pCenterX = (p.getX() + (p.getWidth()/2));
  67.                
  68.                int qCenterX = q.getX() + (q.getWidth()/2);
  69.                
  70.                int pCenterY = (p.getY() + (p.getHeight()/2));
  71.  
  72.                int qCenterY = q.getY() + (q.getHeight()/2);
  73.  
  74.                rawImage.drawPoint(new WPIPoint(pCenterX, pCenterY), targetColor, 5);
  75.                rawImage.drawPoint(new WPIPoint(qCenterX, qCenterY), targetColor, 5);
  76.  
  77.                 if(Math.abs(pCenterX - qCenterX) < 20 &&
  78.                     Math.abs(pCenterY - qCenterY) < 20){
  79.                     int pArea = Math.abs(p.getArea());
  80.                     int qArea = Math.abs(q.getArea());
  81.                     if(pArea > qArea){
  82.                         square = p;
  83.                         squareArea = pArea;
  84.                     }else{
  85.                         square = q;
  86.                         squareArea = qArea;
  87.                     }
  88.                     break;
  89.                 }
  90.             }
  91.         }
  92.  
  93.         if(square != null){
  94.             double x = square.getX() + (square.getWidth()/2);
  95.             x = (2 * (x/rawImage.getWidth())) - 1;
  96.            
  97.             double area = ((double)squareArea) /  ((double)(rawImage.getWidth() * rawImage.getHeight()));
  98.            
  99.             synchronized(table) {
  100.             table.beginTransaction();
  101.                 table.putBoolean("found", true);
  102.                 table.putDouble("x", x);
  103.                 table.putDouble("area", area);
  104.             table.endTransaction();
  105.             }
  106.  
  107.             Robot.getTable().putBoolean("found", true);
  108.             Robot.getTable().putDouble("X", x);
  109.             Robot.getTable().putDouble("Area", area);
  110.             rawImage.drawPolygon(square, targetColor, 7);
  111.         }else{
  112.             table.putBoolean("found", false);
  113.             Robot.getTable().putBoolean("found", false);
  114.         }
  115.  
  116.         return rawImage;
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment