MATLAB: Converting an RGB image to black and white based on a user defined color scale

black and whitecolor imageimage analysis

I have a 408x576x3 unit 8 image that I would have the user define a color range. If the RGB value is less than the defined range, the pixel is converted to the color black and all other pixels with RGB values greater than the defined range are converted to white.
An example in the image attached would to have (according to the color spectrum in the scale bar) anything form 0 to 0.02 become black and all else turned white.
Is there such a way to create and IF statement where the user can define a RGB value where anything equal to or less than becomes black, and all else is white?

Best Answer

You can use logical indexing with inequalities. Check the below example.
[X,Y,Z] = peaks(100) ;
Z(Z>=-2 & Z<=2) = 1 ;
Z(Z<=-2 | Z>=2) = 0 ;
pcolor(X,Y,Z)