0

Let's say I have an image of a ball like this one:

ball

I want to separate the colors of the ball to the color groups. In this case I should have 2 main color groups - "brown" and "white". The "brown" group will have all the brown pixels and the "white" group will have all the white pixels. I'm using matlab for this task. The way that I thought to do is:

  1. to look at the RGB channels. I used scatter to look if I could clearly see some groups, but I didn't.
  2. to look at the bayer vales. But couldn't see any groups either.
  3. to run an edge detector. Then, in each enclosed area I'll find the mean of the pixels. The areas that will have similar mean values (within a certain threshold) will belong to the same group. It seemed to sort of to work but in many case it didn't
  4. Any other ideas?
Spektre
  • 49,595
  • 11
  • 110
  • 380
theateist
  • 13,879
  • 17
  • 69
  • 109
  • there are many color-based image segmentation methods, you can use kmeans for [example](https://www.mathworks.com/help/images/examples/color-based-segmentation-using-k-means-clustering.html). search google for more. – user2999345 Apr 02 '17 at 06:45
  • To how many groups do you want to separate your picture? always 2? – Binyamin Even Apr 02 '17 at 07:53
  • take a look at related QA: [RGB value base color name](http://stackoverflow.com/a/37476754/2521214) for easy hack but essentially you want to do color based segmentation. it is similar to flood fill but you stop when either color is too far from start point color or its neighbors based on 2 thresholds (absolute and relative). As you want ROI then you can fill/label directly to it leaving original image as is. – Spektre Apr 02 '17 at 08:07

2 Answers2

0

This task is called segmentation, in your case each color is a segment and segments are not always continuous.

Searching segmentation examples for Matlab should yield a lot of code examples and theorems.

Note one thing, there is no ground truth solution, you can't say how many segments there are for each image since it is subjective question. In a general case you can run clustering algorithm on the color values which will break the image to color segments, there are algorithms which will find the number of groups automatically - this can be a good start for the number of color groups in your image.

quick search yielded these works, they can get you started with ideas:

Image segmentation with matlab

Using EM for image segmentation

antonpuz
  • 3,256
  • 4
  • 25
  • 48
0

While image segmentation would be the correct way to treat color separation, if your image is simple, you can try to do it brute-force.

Here, converting to HSV would be easier to handle with the image.

For the white parts of the image:

I=imread('ball.jpg');
H=rgb2hsv(I);
% separate dimensions
h=H(:,:,1);
s=H(:,:,2);
v=H(:,:,3);

% color conditions
v(v<0.8 | s>0.7 | h>0.7 )=NaN;
h(isnan(v))=NaN;
s(isnan(v))=NaN;

% convert image back
W=cat(3,h,s,v);
White_image=hsv2rgb(W);
figure; imagesc(White_image);

enter image description here

And for the brown parts:

% separate dimensions
h=H(:,:,1);
s=H(:,:,2);
v=H(:,:,3);

% color conditions
v(s<0.6 | v>0.8 )=NaN;
h(isnan(v))=NaN;
s(isnan(v))=NaN;

% convert image back
B=cat(3,h,s,v);
Brown_image=hsv2rgb(B);
figure; imagesc(Brown_image); axis off

enter image description here

Adiel
  • 3,071
  • 15
  • 21