Broomer
Broomer

Reputation: 107

Calculate a color threshold from within a contour dynamically

I have an input image which consists of 3 colors.These colors are circular in shape and nested.

The image is similar to this : https://www.google.ie/search?q=red+yellow+blue+nested+circles&client=ms-unknown&tbm=isch&tbs=rimg:CbQTsOKsM7yhIkCaDOdJHzqnN2Xk-DhItFHm0Zqt6wMB32Tm1CzyzQ7wrXERbVqngEyMBzO57J8UuHLak9WPqWfjV7kgvdJ47BJlKhIJmgznSR86pzcR8SW2ldYWlqIqEgll5Pg4SLRR5hG-6WlMFrVBvioSCdGaresDAd9kEVFfCyyB-AgqKhIJ5tQs8s0O8K0RT790ELynuK8qEglxEW1ap4BMjBHBPar4Jd2NtioSCQczueyfFLhyEY7iP_13IGcsOKhIJ2pPVj6ln41cRTMOWeqZE5oYqEgm5IL3SeOwSZREray5kAy-dzw%3D%3D&tbo=u&ved=0ahUKEwjWzfSC6IbXAhXFbBoKHW3GBrUQuIIBCCM#imgrc=5tQs8s0O8K32hM:

I will be working with several images like this.The imaage is always of the same thing.however dude to different camera , lighting , even printer differences , the actual color can vary. In that it will always be red yellow green in the order showen. By using HSV and thresholds I can easily determine upper and lower values for each color. However If I change to a different set of images theses values are no longer functional.

My idea to overcome this is to look for contours first in the image. For each contour I would like to get an upper and lower threshold. Using a combination of canny , gaussian and contours I am able to draw contour around each color from testing this seems general enough for purpose.

Where I'm stuck is getting the threshold value from within the contours. Is this possible? Or is there simpler logic I am over looking to achieve this ?

At present I'm using python , but language is secondary.

Upvotes: 1

Views: 361

Answers (1)

user1196549
user1196549

Reputation:

Forget about the contours, they will make things harder than necessary.

A better approach could be by classifying the pixels using k-means. Initialize with at least three clusters, centered around green, yellow, red. Maybe one centered on white, for the background.

After convergence you should have the exact colors, together with segmentation.

https://en.wikipedia.org/wiki/K-means_clustering#Standard_algorithm

Upvotes: 1

Related Questions