Amit Agarwal
Amit Agarwal

Reputation: 363

How to split an OpenCV Contour based on a known straight line?

Given:

  1. a contour found using findContours() in openCV python
  2. a line with line equation (y=mx + c).

If the line intersects the contour, split/divide the original contour into two contours such that one contour falls on one side of the line and second contour on the other side.

How can I do this?

Example: NB: Line isn't necessarily horizontal.

Contour with line passing through

Upvotes: 2

Views: 782

Answers (1)

user1196549
user1196549

Reputation:

The expression y - (mx + c) is positive on a side of the line and negative on the other. Compute the sign for every vertex in sequence, keep the positive ones and the intersections. An intersection occurs on sides that exhibit a change of sign; you find the point by interpolation between the endpoints.

This gives you the positive polygon. Repeat with the negative signs to get the negative polygon.

Upvotes: 2

Related Questions