Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Convexity defect not working as expected

First off, I am using the OpenCV 3.1.0 Java wrapper (NOT JavaCV).

I am trying to find the convexity defects on a hand, but it is not giving the expected results. For examples, here is a binary image of a hand:

image description

(The white lines show the convex hull, and the blue lines show the contour that I am working with).

Here is the same image showing the attempted convexity defect detection:

image description

(blue dots show defects, and pink lines show the resulting shape)

As you can see, the defect between the index and thumb is not being detected.

Here is my code:

    ...
    hull = new MatOfInt();

    Imgproc.convexHull(approxContour, hull, false);

    MatOfInt4 defects = new MatOfInt4();
    Imgproc.convexityDefects(approxContour, hull, defects);

    numPoints = defects.total();
    if (numPoints > max_points) {
        System.out.println("Processing " + max_points + " defect pts");
        numPoints = max_points;
    }

    for (int i = 0; i < numPoints; i++) {
        double[] dat = defects.get(i, 0);

        double[] startdat = approxContour.get((int) dat[0], 0);

        Point startPt = new Point(startdat[0], startdat[1]);

        tipPts[i] = startPt;

        double[] enddat = approxContour.get((int) dat[1], 0);
        endPts[i] = new Point(enddat[0], enddat[1]);

        double[] depthdat = approxContour.get((int) dat[2], 0);
        Point depthPt = new Point(depthdat[0], depthdat[1]);
        foldPts[i] = depthPt;

        depths[i] = dat[3];
    }
    ...
    for (int i = 0; i < numPoints; i++) {
        Imgproc.circle(im, tipPts[i], 2, new Scalar(0, 0, 255));
        Imgproc.circle(im, foldPts[i], 2, new Scalar(255, 0, 0));
        Imgproc.circle(im, endPts[i], 2, new Scalar(0, 255, 0));
        Imgproc.line(im, tipPts[i], foldPts[i], new Scalar(255, 0, 255));
        Imgproc.line(im, tipPts[i], endPts[i], new Scalar(255, 255, 255));
        Imgproc.line(im, endPts[i], foldPts[i], new Scalar(255, 0, 255));
    }

Convexity defect not working as expected

First off, I am using the OpenCV 3.1.0 Java wrapper (NOT JavaCV).

I am trying to find the convexity defects on a hand, but it is not giving the expected results. For examples, here is a binary image of a hand:

image description

(The white lines show the convex hull, and the blue lines show the contour that I am working with).

Here is the same image showing the attempted convexity defect detection:

image description

(blue dots show defects, and pink lines show the resulting shape)

As you can see, the defect between the index and thumb is not being detected.

Here is my code:

    ...
    hull = new MatOfInt();

    Imgproc.convexHull(approxContour, hull, false);

    MatOfInt4 defects = new MatOfInt4();
    Imgproc.convexityDefects(approxContour, hull, defects);

    numPoints = defects.total();
    if (numPoints > max_points) {
        System.out.println("Processing " + max_points + " defect pts");
        numPoints = max_points;
    }

    for (int i = 0; i < numPoints; i++) {
        double[] dat = defects.get(i, 0);

        double[] startdat = approxContour.get((int) dat[0], 0);

        Point startPt = new Point(startdat[0], startdat[1]);

        tipPts[i] = startPt;

        double[] enddat = approxContour.get((int) dat[1], 0);
        endPts[i] = new Point(enddat[0], enddat[1]);

        double[] depthdat = approxContour.get((int) dat[2], 0);
        Point depthPt = new Point(depthdat[0], depthdat[1]);
        foldPts[i] = depthPt;

        depths[i] = dat[3];
    }
    ...
    for (int i = 0; i < numPoints; i++) {
        Imgproc.circle(im, tipPts[i], 2, new Scalar(0, 0, 255));
        Imgproc.circle(im, foldPts[i], 2, new Scalar(255, 0, 0));
        Imgproc.circle(im, endPts[i], 2, new Scalar(0, 255, 0));
        Imgproc.line(im, tipPts[i], foldPts[i], new Scalar(255, 0, 255));
        Imgproc.line(im, tipPts[i], endPts[i], new Scalar(255, 255, 255));
        Imgproc.line(im, endPts[i], foldPts[i], new Scalar(255, 0, 255));
    }