Question

I want to extract each colored region in MATLAB after applying SRM segmentation method on a particular image.

I tried the following, but it seems that it extracts regions with different color (not the same color degree only), and with the largest area.

I = imread('./img/bfly.jpg');
imshow(I)

bw = im2bw(I);
imshow(bw)

L = bwlabel(bw);
imshow(L == 0)

props = regionprops(L);
[~,ind] = max([props.Area]);
imshow(L == ind);

Is there a way to extract each color separately?

This is an example image. I want to extract the brown color alone, the green color alone, and so on ...

enter image description here

Was it helpful?

Solution

Since your image appears to have no smooth variations of color, it should be straightforward to separate the colors into different images with unique to convert the image to label matrix (you could do this with rgb2ind also) followed by accumarray:

[Iu,ia,iu] = unique(reshape(I,[],3),'rows');
counts = accumarray(iu,1);
[counts,sortinds] = sort(counts,'descend');

Now say you want the N largest components:

N = 10;
largestLabels = sortinds(1:N);

Then the image for color ii:

mapi = reshape(iu == largestLabels(ii),size(I,1),size(I,2));
numeli = counts(ii)

The corresponding RGB values and the number of pixels of each color:

>> colorRegionSummary = [uint32(Iu(largestLabels,:)) counts(1:N)]
colorRegionSummary =
     89    120     23   8206  % green
     73     59     42   4370  % dark brown (wing)
     64    128    184   2723  % blue (right shade)
    105    136     25   2143  % green (bottom right shade)
     64    127    178   1667  % blue (top left shade)
    170    151    191   1380  % purple
     58    132    201   1372  % blue (left shade)
    177    130     45   1242  % orange (bottom wing shade)
    184    123     50   1193  % orange (top wing shade)
    118    114     56    586  % tan (top right)

Note that these are not connected components, just components with the same color. For a given mapi, you can then apply bwlabel to get the connected components for that color.

OTHER TIPS

You could start with encoding the three color arrays (RGB) in a way so that you can merge them into one, two-dimensional array, e.g.

2Dimage = I(:,:,1) + 1e3*I(:,:,2) + 1e6*I(:,:,3)

that way, you get a unique number for each color: R + 1e3*G + 1e6*B. Note that each channel is encoded with a number in the interval [0, 255].

Now you can go and extract the different color regions from the image using

C = unique(2Dimage)

to obtain the unique colors you need to look for and then

for idx = 1:length(C)
    find(C(idx)==2Dimage)
end

to locate the different parts of the image. The color can be readily obtained from the original image I at the corresponding locations/indices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top