Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join/union contours in OpenCV?

Is suсh operation as "joining" exists/reasonable for contours? Since contour delimits some area on the plane multiple contours can be unioned as sets.

Isn't they?

If yes then how to union contours?

like image 731
Suzan Cioc Avatar asked Nov 21 '25 10:11

Suzan Cioc


1 Answers

Yes, you can union (and intersect, and set difference, and symmetric difference) contours. Since they are vectors of cv::Point and thus are represented as polygons, you can use whatever algorithms/libraties are suitable for that. Just search SO for polygon union etc. But something like that is not (yet?) included in OpenCV.

An easy (but not very performant) way in OpenCV is to create two black images, one for each contour, draw each contour white and filled, and use a bitwise or on the images to get the union image. You can then extreact the contour with cv::findContours again. The other operations (intersect, set diff and sym diff) can be accomplished with bitwise and, substraction and addition+threshold accordingly.

like image 65
Tobias Hermann Avatar answered Nov 23 '25 01:11

Tobias Hermann