Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Converting an image with specific values to black and white

Asked by med-sweng

med-sweng (view profile)

on 27 Apr 2013

I have an image that has only three values(i.e; 56, 98, 170). Those values represent different labels. How can I convert such image to only black and white without graylevels?

This may exapnad to having 4-vaues, 5-values, ...etc.

But, I think that if a solution was gained for three values, this could be applied to more than three value.


1 Comment

Walter Roberson

Walter Roberson (view profile)

on 27 Apr 2013

Which of the values should be converted to black, and which to white? Or do you want a three-level output?


med-sweng (view profile)


No products are associated with this question.

1 Answer

Answer by Walter Roberson

Walter Roberson (view profile)

on 27 Apr 2013
bwimage = (YourImage == 170);

This assumes that 56 and 98 are both to convert to black. If the constants such as 170 are really floating point values such as .539 then you will need to modify slightly; see


Walter Roberson

Walter Roberson (view profile)

Contact us