If I understand you correctly, you have a label map of objects - each with an associated ID with 0 as the background. You also have a vector of important IDs and a colour image that is associated with this label map.
You wish to set all locations that have an important ID to 1 colour. I would first create a logical mask where true
means that the pixel is important and false
otherwise. What I mean by important is that the pixel is either 1, 6 or 9 if we go with your example. You can use bsxfun
combined with any
to create this mask, then we can use this to index into your image and set the right colour to these locations.
Therefore, do this:
%// Create your logical mask
mask3D = bsxfun(@eq, array1, permute(vector(:), [3 2 1]));
mask = any(mask3D, 3);
%// Set the image pixels to blue at these locations
red = img(:,:,1);
green = img(:,:,2);
blue = img(:,:,3);
red(mask) = 0;
green(mask) = 0;
blue(mask) = 255;
img = cat(3, red, green, blue);
Here's a quick example run. Suppose we have this image with squares:

We can see that there are three squares. Let's change object 1 and object 3 to blue. Before we do that, we need to get a label map:
%
im = imread('http://i.stack.imgur.com/DnYQS.png');
%
array1 = bwlabel(im);
%
img = 255*uint8(im);
img = cat(3, img, img, img);
array1
is our label map as you have also mentioned in your question and img
is the colour version of the input image. Now, vector = [1 3]
so we can change those objects. The labelling is such that the top left square is label 1, the middle is label 2 and the bottom right is label 3.
Once I do this and I run the above code, this is the image I get:
