You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
convexityDefects result is located on the hull but has a large depth #4954
|| 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
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.
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.
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).
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 :)
@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.
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
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:
[[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:
Activity
Alizyt commentedon May 5, 2016
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.
lcmsrl commentedon Dec 12, 2016
We experienced the same issue in Opencv 3.1.0 :-(
Opencv 2.4.8 works fine.
DiCanio commentedon Dec 21, 2016
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:
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).
Alizyt commentedon Dec 22, 2016
@DiCanio Looking forward to your update.
bakercp commentedon Feb 9, 2017
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 :)
eyaler commentedon Jan 25, 2018
@DiCanio any update?
DiCanio commentedon Jan 26, 2018
@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.
eyaler commentedon Jan 27, 2018
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.
hannarud commentedon Jan 3, 2019
Not sure, but I think the problem is that
cv2.convexHullmay 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 theconvexityDefectsfunction). Please, see my reproducible code (Python) in #4539hannarud commentedon Jan 21, 2019
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:
Then, when computing convex hull, it looks alright when you just look at the points:
hull_points1:
hull_points2:
but if we will return indexes (note, you can pass indexes only to convexityDefects):
hull_ind1:
hull_ind2:
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:Merge pull request #28163 from dkurt:d.kurtaev/convexHull_repeats
Merge pull request opencv#28163 from dkurt:d.kurtaev/convexHull_repeats