# isContourConvex always returns "False"?

I have tried to use cv2.isContourConvex on several different shapes, but I get "False" all the time. I think I might have misunderstood how cv2.isContourConvex is working. Can anyone help?

Here is how I have tried to use it on the image below (which should be convex, right?):

img = cv2.imread('convex.png')

img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
image,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contours[0]

print cv2.isContourConvex(cnt)


edit retag close merge delete

Sort by ยป oldest newest most voted

which should be convex, right? NO

Convex : If you choose two points inside shape then All points of line segment defined by end points belong to shape.

this segment proove that shape is not convex

Discrete geometry : should convex right ? I'm agree but reality it is not convex

this one is convex :

Point p1(10,10),p4(10,100), p3(100,100), p2(100,10) ;
vector<Point> ctr;

ctr.push_back(p1);
ctr.push_back(p4);
ctr.push_back(p3);
ctr.push_back(p2);

if (isContourConvex(ctr))
cout << "Contour is convex\n";
else
cout << "Contour is not convex\n";

more

1

Oh I see, thank you! :) Will all images give the result "False" then because of pixelation? And is it any other method I could use to check if shapes in images is convex or not, without getting the problem with pixels?

( 2018-07-06 07:37:06 -0500 )edit

I gave you an example of convex shape. Now you should use convexHull. You can compare surface of convex hull and contours. tutorial here

In convexhull results you gan get distance between contour and convex hull.

( 2018-07-06 07:44:11 -0500 )edit

Official site

GitHub

Wiki

Documentation