Ask Your Question
0

Segment color regions

asked 2019-09-24 08:49:57 -0600

Jorge Diaz gravatar image

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 Two coloured image

This should output 5 regions Multicoloured image

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?

edit retag flag offensive close merge delete

Comments

2

Did you try using contours? Detect contours --> detect number of regions surrounded by contours in your image ---> Detect the color of each region

Kitnos gravatar imageKitnos ( 2019-09-24 09:47:47 -0600 )edit

Use kmeans for colour clustering and then measure the similarity.

holger gravatar imageholger ( 2019-09-24 10:15:54 -0600 )edit

@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 gravatar imageJorge Diaz ( 2019-09-25 04:29:42 -0600 )edit

@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 gravatar imageKitnos ( 2019-09-25 05:05:31 -0600 )edit

@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.

Jorge Diaz gravatar imageJorge Diaz ( 2019-09-25 07:36:10 -0600 )edit

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".

holger gravatar imageholger ( 2019-09-25 19:02:57 -0600 )edit

OK i read Kitnos first comment - its not bad.Why not combining everything together.

  1. Run contour to find areas
  2. Crop the contour and run kmeans(with k=1) on each of the contour to get the dominant color.

Number of contours = Number of areas For each area -> centroid of kmean is the avg color

Done?!

holger gravatar imageholger ( 2019-09-25 19:08:08 -0600 )edit
1

My answer is on the way. Apparently I can't answer before 2 days..:shrug:

Jorge Diaz gravatar imageJorge Diaz ( 2019-09-26 08:05:37 -0600 )edit

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.

Martian gravatar imageMartian ( 2019-09-28 01:59:58 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2019-09-27 03:12:04 -0600

Jorge Diaz gravatar image

In the end I solved this issue using contrast gradients to delimit the areas to apply watershed. It doesn't work perfectly but it gives a rough estimate for the number of colour regions in the image. I use this estimate to give a range in which to calculate using k-means to give an accurate result. Even just doing watershed give a good estimate with the right parameters.

Thank you for all the help @holger and @Kitnos. The contour idea was not an option since to apply it I would need to make masks for each of the colours which would mean that I actually know the colours before-hand.

edit flag offensive delete link more

Comments

1

Thats great :-)

holger gravatar imageholger ( 2019-09-27 05:22:26 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-09-24 08:49:57 -0600

Seen: 1,648 times

Last updated: Sep 27 '19