MATLAB: How to convert the green pixels to white

color segmentationImage Processing Toolbox

I have this image labelled dark green(In the image attached). I want to turn all of the pixels that are not black to white. How do i do that?

Best Answer

%black pixels have 0 for all 3 components, R, G, B. So a non-black pixel has at least one non-0 component
mask = any(YourRGBImage,3); %true if any component is non-zero
%now that we know which ones are non-black, make those pixels white
%ah, short-cut: since logical true is 1 and white is 1, and logical false is 0 and black is 0
%we can just use the mask itself as the black / white image
maskedImage = double(repmat(mask, 1, 1, 3));