Example usage for org.opencv.imgproc Imgproc isContourConvex

List of usage examples for org.opencv.imgproc Imgproc isContourConvex

Introduction

In this page you can find the example usage for org.opencv.imgproc Imgproc isContourConvex.

Prototype

public static boolean isContourConvex(MatOfPoint contour) 

Source Link

Usage

From source file:org.ar.rubik.Rhombus.java

License:Open Source License

/**
 * Determine is polygon is a value Rubik Face Parallelogram
 *//*  w  ww.ja  va2  s .  co m*/
public void qualify() {

    // Calculate center
    double x = 0;
    double y = 0;
    for (Point point : polygonPointList) {
        x += point.x;
        y += point.y;
    }
    center.x = x / polygonPointList.size();
    center.y = y / polygonPointList.size();

    // Check if has four sizes and endpoints.
    if (polygonPointList.size() != 4) {
        status = StatusEnum.NOT_4_POINTS;
        return;
    }

    // Check if convex
    // =+= I don't believe this is working.  result should be either true or 
    // =+= false indicating clockwise or counter-clockwise depending if image 
    // =+= is a "hole" or a "blob".
    if (Imgproc.isContourConvex(polygonMatrix) == false) {
        status = StatusEnum.NOT_CONVEX;
        return;
    }

    // Compute area; check if it is reasonable.
    area = areaOfConvexQuadrilateral(polygonePointArray);
    if ((area < MenuAndParams.minimumRhombusAreaParam.value)
            || (area > MenuAndParams.maximumRhombusAreaParam.value)) {
        status = StatusEnum.AREA;
        return;
    }

    // Adjust vertices such that element 0 is at bottom and order is counter clockwise.
    // =+= return true here if points are counter-clockwise.
    // =+= sometimes both rotations are provided.
    if (adjustQuadrilaterVertices() == true) {
        status = StatusEnum.CLOCKWISE;
        return;
    }

    // =+= beta calculation is failing when close to horizontal.
    // =+= Can vertices be chooses so that we do not encounter the roll over problem at +180?
    // =+= Or can math be performed differently?

    /*
     * Calculate angles to X axis of Parallelogram sides.  Take average of both sides.
     * =+= To Do:
     *   1) Move to radians.
     *   2) Move to +/- PIE representation.
     */
    alphaAngle = 180.0 / Math.PI
            * Math.atan2(
                    (polygonePointArray[1].y - polygonePointArray[0].y)
                            + (polygonePointArray[2].y - polygonePointArray[3].y),
                    (polygonePointArray[1].x - polygonePointArray[0].x)
                            + (polygonePointArray[2].x - polygonePointArray[3].x));

    betaAngle = 180.0 / Math.PI
            * Math.atan2(
                    (polygonePointArray[2].y - polygonePointArray[1].y)
                            + (polygonePointArray[3].y - polygonePointArray[0].y),
                    (polygonePointArray[2].x - polygonePointArray[1].x)
                            + (polygonePointArray[3].x - polygonePointArray[0].x));

    alphaLength = (lineLength(polygonePointArray[0], polygonePointArray[1])
            + lineLength(polygonePointArray[3], polygonePointArray[2])) / 2;
    betaLength = (lineLength(polygonePointArray[0], polygonePointArray[3])
            + lineLength(polygonePointArray[1], polygonePointArray[2])) / 2;

    gammaRatio = betaLength / alphaLength;

    status = StatusEnum.VALID;

    Log.d(Constants.TAG, String.format(
            "Rhombus: %4.0f %4.0f %6.0f %4.0f %4.0f %3.0f %3.0f %5.2f {%4.0f,%4.0f} {%4.0f,%4.0f} {%4.0f,%4.0f} {%4.0f,%4.0f}",
            center.x, center.y, area, alphaAngle, betaAngle, alphaLength, betaLength, gammaRatio,
            polygonePointArray[0].x, polygonePointArray[0].y, polygonePointArray[1].x, polygonePointArray[1].y,
            polygonePointArray[2].x, polygonePointArray[2].y, polygonePointArray[3].x, polygonePointArray[3].y)
            + " " + status);
}

From source file:org.lasarobotics.vision.detection.objects.Contour.java

License:Open Source License

/**
 * Tests if the contour is closed (convex)
 *
 * @return True if closed (convex), false otherwise
 *//*from w w w.j av  a  2 s .c  om*/
public boolean isClosed() {
    return Imgproc.isContourConvex(mat);
}