MATLAB: Update Image RGB value for many pixels with logical index

Image Processing Toolboxlogical matrixmaskingMATLABmodifypixelrgb

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 🙂

Best 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);
Related Question