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
OpenCV python version: 4.8.1
Operating System / Platform: Windows 10
Python version: 3.11.5
Detailed description
The "convexHull" function produces an incorrect result for the outer contour, making any further data processing and information extraction from the contour impossible. In my opinion, this contour is correct and does not have self-intersections, but it has two points of self-contact, which causes the incorrect result.
Yes, that's it, and for 8 years, no one bothered to fix it. I think it's time to remind about this issue again. I really hope we won't forget about it for another 8 years. Considering that it's an extremely trivial algorithm, which is very easy to implement. Finding the convex hull is just the basics. Sorry for the emotions.
I wrote a quick fix for my project, It recalculate Convex Hull using other library if monotonous problem detected. Maybe it will be useful to someone.
import cv2 as cv
import numpy as np
from scipy.spatial import ConvexHull
def DetIsMonotonousClockwiseConvexHull(convex_hull_indices_):
index = 0
prev_val = -1
all_ok = True
while index < convex_hull_indices_.shape[0] and all_ok:
curr_val = convex_hull_indices_[index][0]
all_ok = (prev_val < curr_val)
prev_val = curr_val
index = index + 1
return all_ok
def FixClockwiseConvexHullIfNeeded(src_convex_hull_indices_, contour_):
if DetIsMonotonousClockwiseConvexHull(src_convex_hull_indices_):
return src_convex_hull_indices_
recalculated_convex_hull = ConvexHull(np.squeeze(contour_))
return np.array(sorted([[index] for index in recalculated_convex_hull.vertices]))
sklansky-algorithm for convex hull is wrong then opencv result for convex hull is wrong
It's extremely interesting that the incorrectness of the algorithm has already been identified and proven. Perhaps some hybrid scheme will allow solving this problem, a mixture of several algorithms. This issue, as I understand it, arises extremely rarely, and I accidentally discovered it when adding random noise to my original dataset. Out of 100,000 original images, this error manifested itself in only one.
As I see it, it is quite clear when the convex hull becomes incorrect - this can be easily detected if Monotonous is violated. Perhaps it is worth adding some post-processor that would trigger in case of Monotonous violation.
I've done a bit more digging and found related issues #4539 and #5908. Perhaps the root cause of all problems is that convexHull function does not accept contour, but a set of points. While convexityDefects requres contour (simple polygon) as input. Due to this convexHull can shuffle points and produce incorrect output from the contour (simple polygon) convex hull point of view.
So I beleive instead of patching this mess further we need either separate function to calculate convex hull of contours (simple polygons) or a flag/mode/autodetection in the current convexHull function.
Additionally, looks like contours returned by findContours are not simple polygons because they might have points of self-contact which are basically self-intersections. So additional output processing is required here as well.
@ZhivaevDmitry I apologize if this sounds naive and perhaps I do not fully understand your problem. However since point 46 and point 48 are the same why does this final convex hull is considered wrong,
@YanBC Somewhere between versions 4.1 and 4.9 of OpenCV, the code responsible for cv.convexHull and cv.convexityDefects was modified. According to the new internal rules, cv.convexHull is supposed to (but doesn't always) return a monotonous array of convex hull indices, i.e., h[i] < h[i+1]. Furthermore, now if you pass a non-monotonous array of convex hull indices to cv.convexityDefects, an error will be thrown:
.../convhull.cpp:360: error: (-5 argument) The convex hull indices are not monotonous, which can be in the case when the input contour contains self-intersections in function 'convexityDefects'
Activity
mshabunin commentedon Jan 22, 2024
@ZhivaevDmitry , isn't it a duplicate of #4954?
ZhivaevDmitry commentedon Jan 22, 2024
Yes, that's it, and for 8 years, no one bothered to fix it. I think it's time to remind about this issue again. I really hope we won't forget about it for another 8 years. Considering that it's an extremely trivial algorithm, which is very easy to implement. Finding the convex hull is just the basics. Sorry for the emotions.
ZhivaevDmitry commentedon Jan 22, 2024
I wrote a quick fix for my project, It recalculate Convex Hull using other library if monotonous problem detected. Maybe it will be useful to someone.
greyscale2.png.zip
Output:
LaurentBerger commentedon Jan 23, 2024
https://docs.opencv.org/4.x/d3/dc0/group__imgproc__shape.html#ga014b28e56cb8854c0de4a211cb2be656
https://docs.opencv.org/4.x/d0/de3/citelist.html#CITEREF_Sklansky82
https://math.stackexchange.com/questions/2632754/why-is-sklansky-algorithm-convex-hull-wrong
sklansky-algorithm for convex hull is wrong then opencv result for convex hull is wrong
ZhivaevDmitry commentedon Jan 23, 2024
It's extremely interesting that the incorrectness of the algorithm has already been identified and proven. Perhaps some hybrid scheme will allow solving this problem, a mixture of several algorithms. This issue, as I understand it, arises extremely rarely, and I accidentally discovered it when adding random noise to my original dataset. Out of 100,000 original images, this error manifested itself in only one.
As I see it, it is quite clear when the convex hull becomes incorrect - this can be easily detected if Monotonous is violated. Perhaps it is worth adding some post-processor that would trigger in case of Monotonous violation.
mshabunin commentedon Jan 24, 2024
I've done a bit more digging and found related issues #4539 and #5908. Perhaps the root cause of all problems is that
convexHullfunction does not accept contour, but a set of points. WhileconvexityDefectsrequres contour (simple polygon) as input. Due to thisconvexHullcan shuffle points and produce incorrect output from the contour (simple polygon) convex hull point of view.So I beleive instead of patching this mess further we need either separate function to calculate convex hull of contours (simple polygons) or a flag/mode/autodetection in the current
convexHullfunction.Additionally, looks like contours returned by
findContoursare not simple polygons because they might have points of self-contact which are basically self-intersections. So additional output processing is required here as well.Links:
YanBC commentedon Aug 2, 2024
@ZhivaevDmitry I apologize if this sounds naive and perhaps I do not fully understand your problem. However since point
46and point48are the same why does this final convex hull is considered wrong,while the following convex hull is correct?
ZhivaevDmitry commentedon Aug 2, 2024
@YanBC Somewhere between versions 4.1 and 4.9 of OpenCV, the code responsible for cv.convexHull and cv.convexityDefects was modified. According to the new internal rules, cv.convexHull is supposed to (but doesn't always) return a monotonous array of convex hull indices, i.e., h[i] < h[i+1]. Furthermore, now if you pass a non-monotonous array of convex hull indices to cv.convexityDefects, an error will be thrown:
opencv-alalek commentedon Aug 3, 2024
relates #16594
ZhivaevDmitry commentedon Aug 3, 2024
@opencv-alalek Yes, here is another minimalistic example demonstrating the problem:
OpenCV python version: 4.9.0
Operating System / Platform: Windows 10
Python version: 3.11.5
input:
results:
asmorkalov commentedon Dec 5, 2025
@dkurt Do you have progress with the issue?
dkurt commentedon Dec 5, 2025
@asmorkalov not started yet
asmorkalov commentedon Dec 9, 2025
Still reproducible with current 4.x (pre 4.13.0).
Merge pull request #28163 from dkurt:d.kurtaev/convexHull_repeats
Merge pull request opencv#28163 from dkurt:d.kurtaev/convexHull_repeats