Exemplo n.º 1
0
 @Override
 public Result process(CvPipeline pipeline) throws Exception {
   Mat mat = pipeline.getWorkingImage();
   Mat mask = mat.clone();
   Mat masked = mat.clone();
   Scalar color = FluentCv.colorToScalar(Color.black);
   mask.setTo(color);
   masked.setTo(color);
   Core.inRange(
       mat,
       new Scalar(hueMin, saturationMin, valueMin),
       new Scalar(hueMax, saturationMax, valueMax),
       mask);
   Core.bitwise_not(mask, mask);
   mat.copyTo(masked, mask);
   return new Result(masked);
 }
  /**
   * calculates the center of mass using <code>colorForTrackingHSV</code> and <code>colorRadius
   * </code> as radius (in HSV-color space)
   *
   * @param hsv the frame of which the center of mass should be calculated off
   * @return the center of mass as a point in pixel coordinates (i.e. integer)
   */
  public Point calcCenterOfMass(Mat hsv) {
    blackWhiteMask = new Mat();
    Core.inRange(hsv, lowerBound, upperBound, blackWhiteMask);

    dilatedMask = new Mat();
    Imgproc.dilate(blackWhiteMask, dilatedMask, new Mat());

    contour = new ArrayList<MatOfPoint>();
    Mat mHierarchy = new Mat();
    Mat tempDilatedMask = dilatedMask.clone();
    Imgproc.findContours(
        tempDilatedMask, contour, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    boundingRect = Imgproc.boundingRect(findLargestContour(contour));

    int centerOfMassX = boundingRect.x + boundingRect.width / 2;
    int centerOfMassY = boundingRect.y + boundingRect.height / 2;

    Point centerOfMass = new Point(centerOfMassX, centerOfMassY);
    trackPath.add(centerOfMass);
    return centerOfMass;
  }
Exemplo n.º 3
0
  public Point findLaser(Mat inputFrame) {
    Mat mHsv = new Mat();

    Imgproc.cvtColor(inputFrame, mHsv, Imgproc.COLOR_RGB2HSV);

    // Find laser center
    Mat center = new Mat();
    Core.inRange(mHsv, new Scalar(0, 0, 250), new Scalar(180, 16, 255), center);

    Mat h = new Mat();
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(center, contours, h, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
    center.release();

    Mat center_mask = Mat.zeros(inputFrame.rows(), inputFrame.cols(), CvType.CV_8U);
    if (contours.size() > 0) {
      for (int i = 0; i < contours.size(); i++) {
        int radius = 10;
        // Point[] cont_pos = contours.get(i).toArray();
        Moments m = Imgproc.moments(contours.get(i));
        Point p = new Point();
        p.x = m.get_m10() / m.get_m00();
        p.y = m.get_m01() / m.get_m00();
        Core.circle(center_mask, p, radius * 2, new Scalar(255), -1);
      }
    }

    // Find halo
    Mat ranged = new Mat();
    Core.inRange(mHsv, new Scalar(100, 32, 225), new Scalar(150, 255, 255), ranged);
    mHsv.release();
    // Mat f_frame =ranged.clone();

    // Find halo around bright dot
    Core.bitwise_and(ranged, center_mask, ranged);
    center_mask.release();

    // Find biggest resulting contour
    for (int i = 1; i < contours.size(); i++) {
      contours.get(i).release();
    }
    contours.clear();
    Imgproc.findContours(ranged, contours, h, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
    h.release();
    ranged.release();

    if (contours.size() > 0) {
      MatOfPoint biggest_cont = contours.get(0);
      double cont_size = Imgproc.contourArea(biggest_cont);
      for (int i = 1; i < contours.size(); i++) {
        MatOfPoint cur = contours.get(i);
        if (Imgproc.contourArea(cur) > cont_size) {
          biggest_cont = cur;
          cont_size = Imgproc.contourArea(cur);
        }
      }
      Moments m = Imgproc.moments(biggest_cont);
      Point p = new Point();
      p.x = m.get_m10() / m.get_m00();
      p.y = m.get_m01() / m.get_m00();
      for (int i = 1; i < contours.size(); i++) {
        contours.get(i).release();
      }
      biggest_cont.release();

      return p;
    } else {
      return null;
    }
  }
Exemplo n.º 4
0
  public void processImage(Mat imageToProcess) {
    try {
      final Mat processedImage = imageToProcess.clone();
      // red
      final Mat redUpper = new Mat();
      final Mat redLower = new Mat();
      Core.inRange(processedImage, new Scalar(170, 100, 20), new Scalar(180, 255, 255), redUpper);
      Core.inRange(processedImage, new Scalar(0, 100, 20), new Scalar(20, 255, 255), redLower);
      Core.bitwise_or(redLower, redUpper, processedImage);

      // refining the binary image
      Imgproc.erode(processedImage, processedImage, new Mat(), new Point(-1, -1), 1);
      Imgproc.dilate(processedImage, processedImage, new Mat(), new Point(-1, -1), 0);

      // create a clone for the processedImage to be used in finding contours
      final Mat clone = processedImage.clone();
      Imgproc.cvtColor(processedImage, processedImage, Imgproc.COLOR_GRAY2RGB);

      // finds list of contours and draw the biggest on the processedImage
      final Scalar color1 = new Scalar(0, 0, 255);
      final Scalar color2 = new Scalar(255, 255, 0);
      final Scalar color3 = new Scalar(255, 255, 255);
      final List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
      Imgproc.findContours(
          clone, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);

      final List<MatOfPoint> contour = new ArrayList<MatOfPoint>(1);
      double maxArea = 0.0;
      for (int index = 0; index < contours.size(); index++) {
        double area = Imgproc.contourArea(contours.get(index));
        if (area > maxArea && area > 25 && Imgproc.boundingRect(contours.get(index)).y > 40) {
          maxArea = area;
          contour.add(0, contours.get(index));
        }
      }
      // finds bounding Rectangle and draws contours
      Rect boundingRect = new Rect();
      if (contour.size() > 0) {
        Imgproc.drawContours(processedImage, contour, -2, color1);
        boundingRect = Imgproc.boundingRect(contour.get(0));
        final double x = boundingRect.x;
        final double y = boundingRect.y;
        final double width = boundingRect.width;
        final double height = boundingRect.height;
        Core.rectangle(processedImage, new Point(x, y), new Point(x + width, y + height), color3);
      }
      // finding bounding Circle and draws it
      final Point center = new Point();
      final float[] radius = new float[1];
      if (contour.size() > 0) {
        final MatOfPoint2f contour2f = new MatOfPoint2f(contour.get(0).toArray());
        Imgproc.minEnclosingCircle(contour2f, center, radius);
        Core.circle(processedImage, center, (int) radius[0], color2);
      }

      final BallStruct redBallStruct = new BallStruct(boundingRect, center, (double) radius[0]);
      final BallTargeting ballTargeting = new BallTargeting(redBallStruct);

      synchronized (this) {
        distanceToRed = ballTargeting.getDistance();
        angleToRedInDegrees = ballTargeting.getAngle() * (180 / Math.PI);
        this.processedImage = processedImage;
      }
    } catch (Exception e) {

    }
  }
  public void process(Mat rgbaImage) {
    Mat pyrDownMat = new Mat();

    Imgproc.pyrDown(rgbaImage, pyrDownMat);
    Imgproc.pyrDown(pyrDownMat, pyrDownMat);

    Mat hsvMat = new Mat();
    Imgproc.cvtColor(pyrDownMat, hsvMat, Imgproc.COLOR_RGB2HSV_FULL);

    Mat Mask = new Mat();
    Core.inRange(hsvMat, mLowerBound, mUpperBound, Mask);
    Mat dilatedMask = new Mat();
    Imgproc.dilate(Mask, dilatedMask, new Mat());

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();

    Imgproc.findContours(
        dilatedMask, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    blobCenters[0] = new Point();
    blobCenters[1] = new Point();

    if (contours.size() > 0) {

      // Find max contour area of the two biggest
      double[] maxArea = new double[2];
      maxArea[0] = 0; // biggest
      maxArea[1] = 0; // 2nd biggest
      Iterator<MatOfPoint> each = contours.iterator();
      int[] contourIndex = new int[2];
      contourIndex[0] = 0;
      contourIndex[1] = 0;
      int index = 0;
      while (each.hasNext()) {
        MatOfPoint wrapper = each.next();
        double area = Imgproc.contourArea(wrapper);
        if (area > maxArea[0]) { // area bigger than the maximum found
          maxArea[1] = maxArea[0];
          contourIndex[1] = contourIndex[0];
          maxArea[0] = area;
          contourIndex[0] = index;
        } else if (area > maxArea[1]) { // area bigger than the second maximum found
          maxArea[1] = area;
          contourIndex[1] = index;
        }
        index++;
      }

      //        // another possibility to iterate through contours
      //        for (index = 0; index < contours.size(); index++) {
      //            d = Imgproc.contourArea (contours.get(index));

      Log.d("index0", Integer.toString(contourIndex[0]));
      Log.d("index1", Integer.toString(contourIndex[1]));

      MatOfPoint2f mMOP2f1 = new MatOfPoint2f();
      float[] radius = new float[contours.size()];

      // contours is a List<MatOfPoint>
      // so contours.get(x) is a single MatOfPoint
      // but to use approxPolyDP we need to pass a MatOfPoint2f
      // so we need to do a conversion

      contours.get(contourIndex[0]).convertTo(mMOP2f1, CvType.CV_32FC2);
      // get the center of the bigger contour found
      Point tempCenter = new Point();
      Imgproc.minEnclosingCircle(mMOP2f1, tempCenter, radius);
      blobCenters[0].x = tempCenter.x;
      blobCenters[0].y = tempCenter.y;
      blobRadius[0] = radius[0];

      Log.d("center0.x", Double.toString(blobCenters[0].x));
      Log.d("center0.y", Double.toString(blobCenters[0].y));
      Log.d("radius0", Double.toString(blobRadius[0]));

      contours.get(contourIndex[1]).convertTo(mMOP2f1, CvType.CV_32FC2);
      Imgproc.minEnclosingCircle(mMOP2f1, tempCenter, radius);
      blobCenters[1].x = tempCenter.x;
      blobCenters[1].y = tempCenter.y;
      blobRadius[1] = radius[0];

      Log.d("center1.x", Double.toString(blobCenters[1].x));
      Log.d("center1.y", Double.toString(blobCenters[1].y));
      Log.d("radius1", Double.toString(blobRadius[1]));

      // Filter contours by area and resize to fit the original image size
      mContours.clear();
      each = contours.iterator();
      while (each.hasNext()) {
        MatOfPoint contour = each.next();
        if (Imgproc.contourArea(contour) > mMinContourArea * maxArea[1]) {
          Core.multiply(contour, new Scalar(4, 4), contour);
          mContours.add(contour);
        }
      }

    } else {

      blobCenters[0].x = 0;
      blobCenters[0].y = 0;
      blobCenters[1].x = 0;
      blobCenters[1].y = 0;
    }
  }
Exemplo n.º 6
0
 private Mat InRangeCircles(Mat src) {
   Core.inRange(src, new Scalar(148, 40, 50), new Scalar(179, 255, 255), img_bw1);
   Core.inRange(src, new Scalar(0, 50, 50), new Scalar(10, 255, 255), img_bw2);
   Core.bitwise_or(img_bw1, img_bw2, temp);
   return temp;
 }