Segment color regions
I am trying to make a script that counts the number of regions with similar colour on an image. The idea is to count the number of regions in which a colour is used. For instance, if the image contains two colours, it should output 2 but if the image is made of one colour and a line of a different colour passing through the middle it should output 3. An important point is that the images could contain gradients of a similar colour and that should not be counted as a region.
This should output 3 regions
This should output 5 regions
I have tried a couple of implementations. Initially, by brute forcing it with k means and finding the right k which takes for ever with images with even a discrete amount of hue (from HSV). I also tried with watershed algorithm but found that is not very reliable. Thresholding was not very useful either.
Is there a more efficient way to compute it?
Did you try using contours? Detect contours --> detect number of regions surrounded by contours in your image ---> Detect the color of each region
Use kmeans for colour clustering and then measure the similarity.
@Kitnos An interesting idea, but that would mean I know the colours before hand but I am searching for a general algorithm.
@holger In the question I mention I already try kmeans but it takes too long to find the right k. Also I am unclear as to what you mean with measuring the similarity.
@Jorge Diaz Try to find the dominant color in each region, for example find H-S histogram and find peak H value.
https://stackoverflow.com/questions/2...
@Kitnos Thank you very much for your comment but the objective is to generalise the algorithm. If I had the colours being used or the number or location regions, it would be trivial.
Hmm - i still think kmeans is the way to go - i use it often for finding contrast colors. The thing about k-mean is finding the right value of k. Just run with k = 50 (some value i have in mind) and drop empty clusters.
And by measuring similarity of colors i mean i would just compare the lumination(NOT Brightness). Formula for lumination
Anything with lumination factor(bigger number / lower number) < 4.5 is hard to distinguish. At least the contrast is pretty low and my human eye sees them as "similar".
OK i read Kitnos first comment - its not bad.Why not combining everything together.
Number of contours = Number of areas For each area -> centroid of kmean is the avg color
Done?!
My answer is on the way. Apparently I can't answer before 2 days..:shrug:
It looks like your colors are precise and consistent within each region. IOW, no edge-blurring or 'shading.'
In that case, you could find a color that is not present in the image. Then pick a point and do a flood-fill from there. That should give you an easily tallied region. Find the next point, etc. If you need to compress the data rather than listing all pixel coordinates, you could easily use contour detection in conjunction.