How to store different colour of this colour image into an array?

5 views (last 30 days)
How to store the diffrent colours of image into an array? Here the background colour is white.
  4 Comments
Walter Roberson
Walter Roberson on 30 Jul 2015
Just the color? Or extract portions of the image that are the same color?
If you want just the color then the "map" variable that I create in my Answer will contain the list of RGB colors. White will be the last of the rows.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Jul 2015
Edited: Walter Roberson on 30 Jul 2015
[rows,cols, panes] = size(YourImage);
Now convert the image into a colormap and label matrix. I assume here that the color regions you want to find will be exactly identical.
as_2d = reshape(YourImage, [], 3);
[map, ~, coloridx] = unique(as_2d, 'rows');
labels = reshape(coloridx, rows, cols);
Now to extract:
numcolors = size(map,1);
extracted_images = cell(numcolors,1);
for K = 1 : numcolors
timg = zeros(rows, cols, panes);
pixelmatch = find(labels == K);
for L = 1 : panes
timg(pixelmatch + (L-1) * rows * cols) = map(K,L);
end
extracted_images{K} = timg;
end
The above code extracts the various colored portions into new images of exactly the same size as the original, one new image for each unique color. If there are multiple disconnected regions with the same color they will be extracted together.
You could copy from the image instead of from the map, but the map will have exactly the same value due to the way it was built.
If you wanted to group "close" colors as being all one region then you would be wanting to copy from the original matrix instead of from the map -- but you would also be needing to define when colors are "close" or not. If that is your task then you should be looking for Image Analyst's Color Segmentation Demo
  8 Comments
Image Analyst
Image Analyst on 30 Jul 2015
Edited: Image Analyst on 30 Jul 2015
Why are E and H in the same image? They are not the same color.
Let me ask this: Is what you want to have each letter, because it is a unique color, in its own image (without any of the other colored letters)? If so, do you want the original image size, or do you want to get a smaller image by cropping to the bounding box of the letter?
Have you seen my File Exchange?
Have you tried converting to HSV color space and using unique on the hue channel to count the colors?
hsvImage = rgb2hsv(rgbImage);
h = hsvImage(:,:,1);
uniqueHues = unique(h(:));
Then you can extract each unique color and call regionprops() to get the bounding box, then call imcrop() to extract each color out into it's own small image. By the way, I hope you're using PNG images, not JPG.
Walter Roberson
Walter Roberson on 30 Jul 2015
The following code assumes that the background color is the color that sorts last (which would be white if the image includes white)
%part 1 as_2d = reshape(YourImage, [], 3); [map, ~, coloridx] = unique(as_2d, 'rows'); labels = reshape(coloridx, rows, cols) - 1; %make it compatible with rgb2ind
%part 2 numcolors = size(map,1); extracted_images = cell(numcolors-1,1); [maxcolidx, maxpos] = max(labels(:)); [maxrow, maxcol] = ind2sub(size(labels),maxpos); bgcol = YourImage(maxrow,maxcol,:); empty_image = repmat(bgcol,rows,cols,1); for K = 1 : numcolors - 1; timg = empty_image; pixelmatch = find(labels == K - 1); for L = 1 : panes pixelidx = pixelmatch + (L-1) * rows * cols; timg(pixelidx) = YourImage(pixelidx); end extracted_images{K} = timg; end
Now extracted_images{5} (for example) contains the 5th extracted subimage that is all exactly the same color, placed on a background that is exactly the same as the original background.
When you run this, you will notice that 32 images get extracted. That is because there are 32 different colors in your original image and you instructed that images were to be extracted by color. This is what happens when you ask to separate by color and your letters have "drop shadows" !
So I am going to give you a second chance: you can replace all of what I marked as "part 1" above with
[labels, map] = rgb2ind(YourImage, 5);
This reduces down to 5 colors (including the background). Then you can run "part 2" to extract the segments of the original image that are closest to those 5 colors.
When you do this you will notice that one of the images holds most of two letters and that another image has little in it. The reason for this is that the drop shadows are in some cases quite different colors from the letters they are right beside -- so different that the 'H' and 'L' are "closer together" in color than the drop shadow on the 'E' is to anything else.
This is the point at which you need to refer back to what I wrote earlier: "but you would also be needing to define when colors are "close" or not. If that is your task then you should be looking for Image Analyst's Color Segmentation Demo"
Mind you, this is also the point at which you should be asking yourself whether you really want to separate by color or if instead you want to separate according to what is adjacent. Because those are very different tasks.

Sign in to comment.

More Answers (1)

ashwathy dev
ashwathy dev on 5 Aug 2015
Thank you sir. It worked..

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!