Ask Your Question

Convexity defect not working as expected

asked 2016-01-03 09:33:12 -0500

ls998 gravatar image

updated 2016-01-08 02:11:45 -0500

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 =;
    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++) {, tipPts[i], 2, new Scalar(0, 0, 255));, foldPts[i], 2, new Scalar(255, 0, 0));, 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));
edit retag flag offensive close merge delete


There is an issue about your problem (it should be same problem). You can modify opencv source code and it shoul work....

LBerger gravatar imageLBerger ( 2016-01-03 13:43:09 -0500 )edit

Thank you. About when will a version with this fixed be released?

ls998 gravatar imagels998 ( 2016-01-03 14:03:41 -0500 )edit

I don't know. PR5908 is going on. If you have build opencv using cmake you can modify source code. If you don't may be it's time to learn...

LBerger gravatar imageLBerger ( 2016-01-03 14:29:25 -0500 )edit

yes. Right now im on Windows, and I have some system limitations, so I'll just have to revert back to 2.4.9 for now

ls998 gravatar imagels998 ( 2016-01-03 15:14:09 -0500 )edit

hi ~ can you share your source code to me ? im facing the problem of finding and draw the fingertips points~ my email is [email protected] thank you ~

tey gravatar imagetey ( 2016-11-06 11:08:27 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-01-05 16:50:51 -0500

ls998 gravatar image

This issue was being caused by a bug in OpenCV 3.1.0 -

By reverting my OpenCV version back to 2.4.9, the program works properly again.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-01-03 09:01:24 -0500

Seen: 986 times

Last updated: Jan 05 '16