Opencv divide a contour in two sections

2.7k Views Asked by At

I have a contour in Opencv with a convexity defect (the one in red) and I want to cut that contour in two parts, horizontally traversing that point, is there anyway to do it, so I just get the contour marked in yellow? Image describing the problem

enter image description here

1

There are 1 best solutions below

0
On

That's an interesting question. There are some solutions based on how the concavity points are distributed in your image.

1) If such points does not occur at the bottom of the contour (like your simple example). Then here is a pseudo-code.

  1. Find convex hull C of the image I.
  2. Subtract I from C, that will give you the concavity areas (like the black triangle between two white triangles in your example).
  3. The point with the minimum y value in that area gives you the horizontal line to cut.

2) If such points can occur anywhere, you need a more intelligent algorithm which has cut lines that are not constrained by only being horizontal (because the min-y point of that difference will be the min-y of the image). You can find the "inner-most" corner points, and connect them to each other. You can recursively cut the remainder in y-,x+,y+,x- directions. It really depends on the specs of your input.