Example usage for org.opencv.core MatOfPoint clone

List of usage examples for org.opencv.core MatOfPoint clone

Introduction

In this page you can find the example usage for org.opencv.core MatOfPoint clone.

Prototype

public Mat clone() 

Source Link

Usage

From source file:eu.fpetersen.robobrain.behavior.followobject.ColorBlobDetector.java

License:Open Source License

public void process(Mat rgbaImage) {
    Imgproc.pyrDown(rgbaImage, mPyrDownMat);
    Imgproc.pyrDown(mPyrDownMat, mPyrDownMat);

    Imgproc.cvtColor(mPyrDownMat, mHsvMat, Imgproc.COLOR_RGB2HSV_FULL);

    Core.inRange(mHsvMat, mLowerBound, mUpperBound, mMask);
    Imgproc.dilate(mMask, mDilatedMask, new Mat());

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

    Imgproc.findContours(mDilatedMask, contours, mHierarchy, Imgproc.RETR_EXTERNAL,
            Imgproc.CHAIN_APPROX_SIMPLE);

    // Find max contour area
    maxArea = 0;//w  w  w  .  jav a 2 s  .  com
    Iterator<MatOfPoint> each = contours.iterator();
    Mat biggestContour = null;
    while (each.hasNext()) {
        MatOfPoint wrapper = each.next();
        double area = Imgproc.contourArea(wrapper);
        if (area > maxArea) {
            maxArea = area;
            biggestContour = wrapper.clone();
        }
    }
    if (biggestContour != null) {
        Core.multiply(biggestContour, new Scalar(4, 4), biggestContour);
        Moments mo = Imgproc.moments(biggestContour);
        centroidOfMaxArea = new Point(mo.get_m10() / mo.get_m00(), mo.get_m01() / mo.get_m00());
    } else {
        centroidOfMaxArea = null;
    }

    // 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) {
            Core.multiply(contour, new Scalar(4, 4), contour);
            mContours.add(contour);
        }
    }

    Imgproc.drawContours(rgbaImage, mContours, -1, CONTOUR_COLOR);
}