Ask Your Question
1

isContourConvex always returns "False"?

asked 2018-07-06 06:54:07 -0500

Maren gravatar image

updated 2018-07-06 07:06:25 -0500

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)

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-07-06 07:13:56 -0500

LBerger gravatar image

updated 2018-07-06 07:25:03 -0500

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.

your shape : image description

this segment proove that shape is not convex

image description

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";
edit flag offensive delete link more

Comments

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?

Maren gravatar imageMaren ( 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.

LBerger gravatar imageLBerger ( 2018-07-06 07:44:11 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-07-06 06:49:15 -0500

Seen: 219 times

Last updated: Jul 06 '18