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