Skip to content

convexityDefects result is located on the hull but has a large depth #4954

Closed

Description

@opencv-pushbot

Transferred from http://code.opencv.org/issues/4242

|| Christoph Pacher on 2015-03-17 16:48
|| Priority: Normal
|| Affected: branch 'master' (3.0-dev)
|| Category: imgproc, video
|| Tracker: Bug
|| Difficulty: 
|| PR: 
|| Platform: x86 / Windows

convexityDefects result is located on the hull but has a large depth

Hi, 

Over here:
http://answers.opencv.org/question/57454/convexitydefects-computes-wrong-result/

I posted my experience with wrong results from convexityDefects along some sample code.
I get wrong results with the hull ordered cw or ccw, so I am not sure if this is connect to Bugfix #3545. 

Is this a bug? 

Thanks
Christoph

History

Vadim Pisarevsky on 2015-04-27 12:41
-   Category set to imgproc, video

Activity

  1. Alizyt commented on May 5, 2016

    @Alizyt

    I met the same issue today, the 'first defect' returned always seems to be wrong, e.g. val=0x0769e4f8 {2059, 2111, 651, 133888}. Obviously, index 651 isn't between 2059 and 2111, and the depth value turned out to be extremely large.

  2. lcmsrl commented on Dec 12, 2016

    @lcmsrl

    We experienced the same issue in Opencv 3.1.0 :-(

    Opencv 2.4.8 works fine.

  3. DiCanio commented on Dec 21, 2016

    @DiCanio

    I can confirm this issue for OpenCV 3.1.0 as well.

    Here is some additional information on this issue:
    As it turned out there are cases where the convexityDefects method yields different result sizes depending on the orientation that has been used to calculate the convex hull of a point set.
    This behaviour seems to be wrong, since the points making up the convex hull are still the same. The only difference is that the ordering is reversed. Thus, the result size of the convexityDefects function should always be the same no matter what the convex hull orientation is.

    Furthermore, as the documentation states, the convexHull function implements the modified Sklansky algorithm which was proven wrong by Toussaint. So maybe, in order to resolve this issue, it would be beneficial to check if the incorrect depth information (as well as the problem mentioned above) still occurs when using a correct convex hull algorithm.

    Correct convex hull algorithms:

    • Shin, Woo
    • Melkman
    • Lee's Algorithm
    • Graham-Yao algorithm

    Since I'm working on a project that is in need of this functionality to be correct I will try to feed the convexityDefects function with the result of a correct convex hull algorithm and check whether this has any impact on its results (will keep you updated).

  4. Alizyt commented on Dec 22, 2016

    @Alizyt

    @DiCanio Looking forward to your update.

  5. bakercp commented on Feb 9, 2017

    @bakercp

    I too encountered this bug and undertook a c api workaround that may not work in all circumstances, but seems to work for my needs.

    https://gist.github.com/bakercp/b443755a55b2cc1cbedaaeb75a93ddb6

    Since the c api doesn't offer indices, but does seem to yield correct points, the simple algorithm attempts to efficiently do a reverse lookup on the defect start, end and point indices in terms of the original contour indices. YMMV :)

  6. eyaler commented on Jan 25, 2018

    @eyaler

    @DiCanio any update?

  7. DiCanio commented on Jan 26, 2018

    @DiCanio

    @eyaler I wasn't able to reproduce the issue back then but the fact that the hull algorithm is incorrect still holds. I would have a look at this again if you got an image or a minimal reproducer.

  8. eyaler commented on Jan 27, 2018

    @eyaler

    using python-opencv 3.4.0.12 i could not reproduce the original issue reported here:
    http://answers.opencv.org/question/57454/convexitydefects-computes-wrong-result/

    i get the same results for defects nevermind the orientation of the original contour or the hull. the only difference is the reordered indices

    i do not think the Sklansky issue is relevant here, as in the original issue the hull looks ok. perhaps we should open a new bug for that.

  9. hannarud commented on Jan 3, 2019

    @hannarud

    Not sure, but I think the problem is that cv2.convexHull may return points from original contour in non-monotonous manner (the convex hull and the contour from which it is obtained have different starting points and some points are extra-overlooked by the convexityDefects function). Please, see my reproducible code (Python) in #4539

  10. hannarud commented on Jan 21, 2019

    @hannarud

    Well, having two examples of contours that produce this issue, I think, I can elaborate a bit. First, here are the contours that help to reproduce the issue:

    contour1 = np.array([[[26, 9]], [[25, 10]], [[24, 10]], [[23, 10]], [[22, 10]], [[21, 10]], [[20, 11]], [[19, 11]], [[18, 11]], [[17, 12]], [[17, 13]], [[18, 14]], [[18, 15]], [[18, 16]], [[18, 17]], [[19, 18]], [[19, 19]], [[20, 20]], [[21, 21]], [[21, 22]], [[22, 23]], [[22, 24]], [[23, 25]], [[23, 26]], [[24, 27]], [[25, 28]], [[26, 29]], [[27, 30]], [[27, 31]], [[28, 32]], [[29, 32]], [[30, 33]], [[31, 34]], [[30, 35]], [[29, 35]], [[30, 35]], [[31, 34]], [[32, 34]], [[33, 34]], [[34, 33]], [[35, 32]], [[35, 31]], [[35, 30]], [[36, 29]], [[37, 28]], [[37, 27]], [[38, 26]], [[39, 25]], [[40, 24]], [[40, 23]], [[41, 22]], [[42, 21]], [[42, 20]], [[42, 19]], [[43, 18]], [[43, 17]], [[44, 16]], [[45, 15]], [[45, 14]], [[46, 13]], [[46, 12]], [[45, 11]], [[44, 11]], [[43, 11]], [[42, 10]], [[41, 10]], [[40, 9]], [[39, 9]], [[38, 9]], [[37, 9]], [[36, 9]], [[35, 9]], [[34, 9]], [[33, 9]], [[32, 9]], [[31, 9]], [[30, 9]], [[29, 9]], [[28, 9]], [[27, 9]]], dtype=np.int32)
    
    contour2 = np.array([[[23, 6]], [[22, 7]], [[21, 7]], [[20, 7]], [[19, 7]], [[18, 7]], [[17, 8]], [[16, 8]], [[15, 9]], [[14,10]], [[14, 11]], [[14, 12]], [[14, 13]], [[15, 13]], [[16, 14]], [[17, 15]], [[17, 16]], [[17, 17]], [[17, 18]], [[17, 19]], [[17, 20]], [[17, 21]], [[17, 22]], [[18, 23]], [[19, 23]], [[20, 24]], [[20, 25]], [[20, 26]], [[21, 27]], [[21, 28]], [[22, 29]], [[22, 30]], [[23, 31]], [[24, 32]], [[25, 33]], [[26, 33]], [[27, 34]], [[28, 35]], [[27, 36]], [[26, 36]], [[27, 36]], [[28, 35]], [[29, 35]], [[30, 34]], [[31, 34]], [[32, 33]], [[32, 32]], [[33, 31]], [[33, 30]], [[34, 29]], [[35, 28]], [[35, 27]], [[35, 26]], [[35, 25]], [[36, 24]], [[36, 23]], [[37, 22]], [[37, 21]], [[37, 20]], [[37, 19]], [[37, 18]], [[38, 17]], [[38, 16]], [[38, 15]], [[39, 14]], [[39, 13]], [[40, 12]], [[40, 11]], [[39, 10]], [[39, 9]], [[39, 8]], [[38, 7]], [[37, 7]], [[36, 7]], [[35, 7]], [[34, 7]], [[33, 7]], [[32, 6]], [[31, 6]], [[30, 6]], [[29, 6]], [[28, 6]], [[27, 6]], [[26, 6]], [[25, 6]], [[24, 6]]], dtype=np.int32)
    

    Then, when computing convex hull, it looks alright when you just look at the points:

    hull_points1 = cv2.convexHull(contour1, returnPoints=True)
    print(hull_points1)
    hull_points2 = cv2.convexHull(contour2, returnPoints=True)
    print(hull_points2)
    

    hull_points1:

    [[[46 13]]
     [[42 21]]
     [[35 32]]
     [[33 34]]
     [[30 35]]
     [[29 35]]
     [[23 26]]
     [[19 19]]
     [[18 17]]
     [[17 13]]
     [[17 12]]
     [[18 11]]
     [[21 10]]
     [[26  9]]
     [[40  9]]
     [[45 11]]
     [[46 12]]]
    

    hull_points2:

    [[[40 12]]
     [[37 22]]
     [[35 28]]
     [[32 33]]
     [[31 34]]
     [[27 36]]
     [[26 36]]
     [[22 30]]
     [[17 22]]
     [[14 13]]
     [[14 10]]
     [[16  8]]
     [[18  7]]
     [[23  6]]
     [[32  6]]
     [[38  7]]
     [[39  8]]
     [[40 11]]]
    

    but if we will return indexes (note, you can pass indexes only to convexityDefects):

    hull_ind1 = cv2.convexHull(contour1, returnPoints=False)
    print(hull_ind1)
    hull_ind2 = cv2.convexHull(contour2, returnPoints=False)
    print(hull_ind2)
    

    hull_ind1:

    [[59]
     [51]
     [40]
     [38]
     [33] *** note this! this should be 35!
     [34]
     [23]
     [16]
     [14]
     [10]
     [ 9]
     [ 8]
     [ 5]
     [ 0]
     [66]
     [61]
     [60]]
    

    hull_ind2:

    [[66]
     [56]
     [50]
     [45]
     [44]
     [38] *** note this! this should be 40!
     [39]
     [31]
     [22]
     [12]
     [ 9]
     [ 7]
     [ 5]
     [ 0]
     [77]
     [71]
     [70]
     [67]]
    

    If we look at the original image array, we'll notice that the wrong indexes were produced by the points that are met twice in the original image array ([[30, 35]] for the first image array and [[27, 36]] for the second one). So I guess the code for producing convex hull indexes just selects the wrong occurrence. I marked the pixel in red:

    image1

    image2

  11. added a commit that references this issue on Dec 19, 2025
  12. added a commit that references this issue on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions