Abhishek V. Pai
Abhishek V. Pai

Reputation: 351

using mouse click to detect contour in opencv

I have various contours/segments in an image. Right now I use the keyboard to input which segment I want to extract from the image (given below)

enter image description here

Consider the pink area as segment 1 and white area as segment 2. I use keyboard to input which segment(1 or 2) I want extracted.On drawing contour I get

enter image description here

I have saved the colours of each segments in an array and I use the array to extract the particular segment.

Now I want know if its possible to use mouse click to identify the segment/contour, So that I can draw multiple contours and identify the segment using mouse and not use keyboard to input the segment number.

Upvotes: 1

Views: 1430

Answers (1)

Zack Knopp
Zack Knopp

Reputation: 2935

I know this question was asked a while ago, but this is what I did to solve my problem. I was doing it in python, but it should be similar.

I used the OpenCV floodfill algorithm to fill where I clicked, using the drawn contours on a binary image as the floodfill mask, where the contours are white and background is black. This way the floodfill cannot pass the contour edges.

Once you fill the contour with a certain color, it's easy to extract it using some sort of thresholding.

Hope this helps.

Upvotes: 2

Related Questions