Home > Software design >  How to convert pixel values of an image array to white or black only?
How to convert pixel values of an image array to white or black only?

Time:12-15

I would like to convert the image below to only black and white (no gray areas).

So, if I have the variable "image_array" (shape 12288x1 double, values ranging from 0 to 1), how can I convert all the values in this array so that they are either 0 or 1 only? The threshold could be if a value is <0.5, then its new value=0. Otherwise, its new value=1.

I was thinking of just doing this: create a for loop to iterate through each value in "image_array" and, with an if statement, to compare the current value against the threshold & assign its new value? Or, is there a simpler way, more efficent way to do this?

enter image description here

CodePudding user response:

If you have the Image Processing and Computer Vision Toolbox, you can simply use imbinarize. It does global thresholding according to Otsu's method or adaptive thresholding.

CodePudding user response:

Let A be the image mentioned in the question:

th1=.5;
A(A>th1)=1;
A(A<=th1)=0;
  • Related