Update Image RGB value for many pixels with logical index

Hello all, I'm doing my first steps with MatLab, and faced a problem for several hours now that I'm unable to solve.
If have converted an RGB image to a double matrix, which works. Now I want to modify all pixels of the RGB matrix that have a value >=0.7 in the green channel. I determine the indexes of those pixels using a logical Matrix:
logical_mark_green = my_image_double(:,:,2) >= 0.7; %store logical Matrix containing a '1' at all indexes where value of index 2 (=green) >= 0.7
Afterwards, and that's where I am stuck, I want to overwrite the determined pixels to the colour black:
my_image_double(logical_mark_green, :) = 0; %overwrite image on indexes stored in logical Matrix with 3D-Array representing black colour
But I only get a corrupted image with a small black line. Can anybody help me out? I appreciate any help. And kindly provide beginner-friendly answers :)

 Accepted Answer

One way is to repeat the mask for three layers before applying
img = double(imread('peacock.jpg'))/255;
mask = img(:,:,2) > 0.5;
mask = repmat(mask, 1,1,3);
img(mask) = 0;
imshow(img);

2 Comments

This works, thanks a ton! Can you explain why repmat solves it? The logical Matrix is n*n, after using repmat it is (n*1)*(n*1)*3, right?
Yes, after repmat, the dimensions of mask become n*n*3, which also matches the dimensions of the img variable. The dimensions of the logical mask must match the variable it is trying to change.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!