Functions > Image Processing > Image Manipulation > Example: Binarization and Quantization
  
Example: Binarization and Quantization
Use the binarize function to produce an image with 2 levels of grey. Use quantize to produce an image with n levels of grey.
For each element in the image, the functions return 0 if the element is ≤ to a specified threshold and they return 1 if the element is greater than the threshold.
Binarization and quantization can be used for enhancing features of an image by choosing the appropriate threshold or number of levels. They can also be a first step in an image analysis.
For information on using this example, refer to About Image Processing Examples.
binarize
1. Read in an image.
Click to copy this expression
(bridge.bmp)
2. Set the threshold.
Click to copy this expression
3. Produce a binarized image.
Click to copy this expression
Click to copy this expression
(bridge_bin.bmp)
You can change the value of thresh to view the effect on the image.
Setting Binarization Automatically
Use automatic binarization to highlight items of interest in machine vision applications.
If a threshold value is not specified, then the binarize function automatically calculates one.
1. Apply binarize to a grayscale digitized image of an industrial part.
Click to copy this expression
Click to copy this expression
Click to copy this expression
(part.gif)
(part_bin.bmp)
Using the Optional Arguments
To emphasize or de-emphasize particular ranges of levels, you can provide the optional arguments lowThresh, highThresh, inValue, and outValue. All levels between lowThresh and highThresh are binarized to inValue, and the remaining levels are binarized to outValue. The values for the input parameters are arbitrary and do not have to be from 0 to 255. inValue and outValue may be the same, in which case the entire image is set to one level only.
Set the image levels from 50 to 150 to 25, and set the rest of the levels to 200:
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
(bridge_bin2.bmp)
quantize
1. Apply quantize to the grayscale image used above.
Click to copy this expression
Click to copy this expression
Click to copy this expression
(bridge_q3.bmp)
2. Specify the levels in a vector and apply the quantize function to the same image. Compare this 3-level quantization with the previous one:
Click to copy this expression
Click to copy this expression
Click to copy this expression
(bridge_q3v.bmp)
The values in v must be strictly ascending, but they may be any real number. This specification is useful if you want to use quantization levels which are not uniformly spaced within the range of the given image.