MATLAB: How to create a color mask on RGB jpeg image,which changes pixels with a specific range of RGB to a solid colour of choice

jpegrgb

I am trying to create a mask for an RGB jpeg image. The purpose of this mask needs to be to isolate pixels within a certain range of values for R, G and B channels (i.e., for 50<R<60, 40<G<45, and 31<G<32 – or something like this. I do not know how to isolate the range of values). I then need to replace the pixels that meet this criteria with a specific, solid color. The end goal is to create a habitat map by isolating pixels of certain reflectance values. So far, I have only been able to replace selected pixels with the color Black.
How can I:
a) select pixels within a RANGE of R G and B values,
and b) replace those pixels with any chosen colour?
Thank you so much for any help!
This is my attempted code:
myfile = 'myfile.jpg';
data=imread(myfile);
blue = squeeze(data(:,:,3)); % rgB
green = squeeze(data(:,:,2)); % rGb
red = squeeze(data(:,:,1)); % Rgb
% To look at the image and select which pixels to show:
imshow(data)
%% Mask pixels of certian RGB values
wrack_mask = (data(:, :, 3) > 100) & (data(:, :, 2) > 100) & (data(:, :, 1) > 100);
% need to select a range, not just > 100
% trying to mask the image bsxfun() function, but this only yields a black mask
maskedWrack = bsxfun(@times, data, cast(wrack_mask, 'like', data));
imshow(MaskedWrack)
…and this is the result:myfile.jpg

Best Answer

I seem to have solved it by using imoverlay, and adding more terms to the data selection:
lf;
wrack_mask = (data(:, :, 3) > 49) & (data(:, :, 3) < 83) & (data(:, :, 2) > 35) & (data(:, :, 2) < 67) & (data(:, :, 1) > 18) & (data(:, :, 1) > 48);
dkred = [0.7 0.2 0.1];
MaskedWrack = imoverlay(data,wrack_mask,dkred);
imshow(MaskedWrack)