0
votes

So I have a labeled array (array1) with connected regions of interest (background is all zeros, connected regions are all 1's for the first region, all 2's for the second, 3's for the 3rd etc.) I also have a vector (vector1) of the region labels I find important (ex. 1,6,9). I want to find the locations of these values in the labeled array and then change values in one channel of a separate 3 channel array at the same locations (want to color certain parts of an image green based regions of interest found in another image).

I can use the below code to change all channels, but don't know how to specify (img(y,x,1=0), img(y,x,2=0), img(y,x,3=255)).

for i=1:1:length(vector1)
    img(array1==vector1(i))=255;
end
2

2 Answers

0
votes

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:

%// Originally a binary image
im = imread('http://i.stack.imgur.com/DnYQS.png');

%// Get label map 
array1 = bwlabel(im);

%// Create colour version out of this binary image
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:

enter image description here

0
votes

You can achieve it in two lines -

%// Create a 3D mask to select the labels [1,6,9] only, as listed in "vector1"
mask = bsxfun(@and,ismember(array1,vector1),ones(1,1,3));

%// Map "img" with "mask" and set them to tuple:[0,0,255] (blue color)
img(mask) = reshape(repmat([0 0 255],nnz(mask)/3,1),[],1)

Sample run -

array1 =  %// Input
     2     0     1     1     0     6
     0     0     1     0     0     6
     9     0     0     0     0     6
     9     9     0     4     0     0
vector1 =  %// Input
     1     6     9
img(:,:,1) =   %// Input: Before code run
   228    19   175    30   192   188
   204    23    34   164   149   248
   188   204   185    84   189   222
    14   241    29   167    60    22
img(:,:,2) =
    94   202   197    59   200   136
    95    94    53   164    26    24
   175    53   100   124    75   104
   153    23   141    39    61    27
img(:,:,3) =
    29   246   111    48   197   172
   201   111   168    68   101   110
    75   178    28   204    70   116
   154   194   239   125    10   156
img(:,:,1) =   %// Output: After code run
   228    19     0     0   192     0
   204    23     0   164   149     0
     0   204   185    84   189     0
     0     0    29   167    60    22
img(:,:,2) =
    94   202     0     0   200     0
    95    94     0   164    26     0
     0    53   100   124    75     0
     0     0   141    39    61    27
img(:,:,3) =
    29   246   255   255   197   255
   201   111   255    68   101   255
   255   178    28   204    70   255
   255   255   239   125    10   156

Notice the positions of the values [1,6,9] in array1 and the corresponding value changes in img1 before and after code runs.