Question

I have been given an image where the words have been correctly segmented and represented via different colors.

Here is the image:

enter image description here

I was told that without even having the source file I can select specific words/color from that image and save it for further processing. This can be done by creating a new image, v, such that

v=r+g*256+b*256*256+1

where r, g and b are the red, blue and green channels respectively.

I tried to use this but all it created was a white blank image

img = imread('color_test.bmp');
r = img(:,:,1);
g = img(:,:,2);
b = img(:,:,3);

v=r+g*256+b*256*256+1;

I dont understand how the above v image would allow me to select individual colors?

Was it helpful?

Solution

The transformation you apply to create your vector v is just a way to create unique numerical values for all possible RGB color values. To understand how this works, please imagine a simplified RGB system where the values for each color are limited between 0 and 9. Furthermore, we simplify your transformation by replacing 255 by 10:

 v = r + g*10 + b*100

Now it is easy to see that every value that
(1) v always between 0 and 999 (2) all red color values are coded at the ones
(3) all green color values are coded at the tens
(4) and all blue color values are coded in the hundreds.

Hence, different colors will always result in distinct values in v.

Let's say that your image contains n different colors. Then your matrix v will contain only n unique values (i.e., one for each color) which can be identified using the command unique

 colorValues = unique(v); 

If you now want to identify all regions in your image that correspond to a specific color, for instance, the first value in your vector colorValues, you can simply use

v == colorValues(1)

which would give you ones in all cells that contain the specified color.

If you want to split this your image into several images based on color, you could use

newImg = zeros(size(img)); 
newImg(repmat(v == colorValues(1), [1 1 3]) = 255; 

Now newImg should only contain everything that matches the color in colorValues(1).

To look for different colors, just use different indices, e.g.

 newImg(repmat(v == colorValues(2), [1 1 3]) = 255

OTHER TIPS

All I could gather from your question is that you need to somehow store "info" about the individually colored texts. So, the following code might solve it and for this I had to actually note down the colors for each of the 6 differently colored texts. The resultant 4D matrix stores all the "info". Please note that we could easily make it 3D by squeezing out the 3rd dimension which stores redundant data as we are looking for a perfect match of colors.

Code

%// Read image
img = imread('color_test.bmp');

%%// Create a database of text colors
color1 = [236 3 104]; %%// text1
color2 = [57 228 2]; %%// text2
color3 = [147 190 131]; %%// text3
color4 = [5 107 106]; %%// text4
color5 = [254 223 188]; %%// text5
color6 = [19 98 13]; %%// text6
color = [color1;color2;color3;color4;color5;color6]; %%// all text colors in a Nx3 matrix

%%// Save all the individual colored images in a 4D matrix for later
img_all = false([size(img) size(color,1)]);
for k = 1:size(color,1)
    img_all(:,:,:,k) = bsxfun(@eq,img,permute(color(k,:),[1 3 2]));
end

%%// Show the images as black and white
figure,
for k = 1:size(color,1)
    subplot(size(color,1),1,k), imshow(uint8(255.*img_all(:,:,:,k)));
end

%%// Show the images in their original colors
figure,
for k = 1:size(color,1)
    subplot(size(color,1),1,k), imshow(uint8(bsxfun(@times,img_all(:,:,:,k),permute(color(k,:),[1 3 2]))));
end

Output

enter image description here

enter image description here

Let us know if this worked out for you!

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