2

My question is simple. I have an rgb image and a logical matrix. I want to set the pixel which is true in the corresponding element of logical matrix to (150,160,170).

For example:

    1 1 1 1 1     1 1 1 1 1    1 1 1 1 1                0 0 0 0 0
    1 1 1 1 1     1 1 1 1 1    1 1 1 1 1                0 0 0 0 0
r=  1 1 1 1 1  g= 1 1 1 1 1  b=1 1 1 1 1   logical_mat =1 0 0 0 0
    1 1 1 1 1     1 1 1 1 1    1 1 1 1 1                1 1 0 0 0
    1 1 1 1 1     1 1 1 1 1    1 1 1 1 1                1 1 1 0 0

I want it results in

    1   1   1   1 1     1   1   1   1 1    1   1   1   1 1 
    1   1   1   1 1     1   1   1   1 1    1   1   1   1 1 
r=  150 1   1   1 1  g= 160 1   1   1 1  b=170 1   1   1 1 
    150 150 1   1 1     160 160 1   1 1    170 170 1   1 1 
    150 150 150 1 1     160 160 160 1 1    170 170 170 1 1 

I have tried logical index, if set pixel into same color is easy

lm = repmat(logical_mat,[1 1 3]);
rgb(lm) = 150;

But I dont know how to set the value channel by channel.

Thanks in advance.

beerbajay
  • 19,652
  • 6
  • 58
  • 75
oibook13
  • 21
  • 3

1 Answers1

0

You're already creating the right logical matrix:

lm = repmat(logical_mat,[1 1 3]);

You need to create a 3-channel color matrix the same size.

cm = repmat(cat(3,150,160,170), size(lm,1), size(lm,2))

Then, index into the color matrix with lm:

rgb(lm) = cm(lm);
tmpearce
  • 12,523
  • 4
  • 42
  • 60