Convert Intensity to Binary Images

Binary images contain Boolean pixel values that are either 0 or 1. Pixels with the value 0 are displayed as black; pixels with the value 1 are displayed as white. Intensity images contain pixel values that range between the minimum and maximum values supported by their data type. Binary images can contain only 0s and 1s, but they are not binary images unless their data type is Boolean.Thresholding Intensity Images Using Relational Operators

Thresholding Intensity Images Using Relational Operators

You can use the Relational Operator block to perform a thresholding operation that converts your intensity image to a binary image. This example shows you how to accomplish this task.

You can open the example model by typing

ex_vision_thresholding_intensityex_vision_thresholding_intensity

on the MATLAB® command line.

  1. Create a new Simulink® model, and add to it the blocks shown in the following table.

    Block

    Library

    Quantity

    Image From File

    Computer Vision System Toolbox™ > Sources

    1

    Video Viewer

    Computer Vision System Toolbox > Sinks

    2

    Relational Operator

    Simulink > Logic and Bit Operations

    1

    Constant

    Simulink > Sources

    1

  2. Use the Image from File block to import your image. In this example the image file is a 256-by-256 matrix of 8-bit unsigned integer values that range from 0 to 255. Set the File name parameter to rice.png

  3. Use the Video Viewer1 block to view the original intensity image. Accept the default parameters.

  4. Use the Constant block to define a threshold value for the Relational Operator block. Since the pixel values range from 0 to 255, set the Constant value parameter to 128. This value is image dependent.

  5. Use the Relational Operator block to perform a thresholding operation that converts your intensity image to a binary image. Set the Relational Operator parameter to >. If the input to the Relational Operator block is greater than 128, its output is a Boolean 1; otherwise, its output is a Boolean 0.

  6. Use the Video Viewer block to view the binary image. Accept the default parameters.

  7. Connect the blocks as shown in the following figure.

  8. Set the configuration parameters. Open the Configuration dialog box by selecting Simulation > Model Configuration Parameters menu. Set the parameters as follows:

    • Solver pane, Stop time = 0

    • Solver pane, Type = Fixed-step

    • Solver pane, Solver = Discrete (no continuous states)

  9. Run your model.

    The original intensity image appears in the Video Viewer1 window.

    The binary image appears in the Video Viewer window.

      Note:   A single threshold value was unable to effectively threshold this image due to its uneven lighting. For information on how to address this problem, see Correct Nonuniform Illumination.

You have used the Relational Operator block to convert an intensity image to a binary image. For more information about this block, see the Relational Operator block reference page in the Simulink documentation. For additional information, see Converting Between Image Types in the Image Processing Toolbox documentation.

Thresholding Intensity Images Using the Autothreshold Block

In the previous topic, you used the Relational Operator block to convert an intensity image into a binary image. In this topic, you use the Autothreshold block to accomplish the same task. Use the Autothreshold block when lighting conditions vary and the threshold needs to change for each video frame.

    Note:   Running this example requires a DSP System Toolbox™ license.

ex_vision_autothresholdex_vision_autothreshold
  1. If the model you created in Thresholding Intensity Images Using Relational Operators is not open on your desktop, you can open the model by typing

    ex_vision_thresholding_intensityex_vision_thresholding_intensity

    at the MATLAB command prompt.

  2. Use the Image from File block to import your image. In this example the image file is a 256-by-256 matrix of 8-bit unsigned integer values that range from 0 to 255. Set the File name parameter to rice.png

  3. Delete the Constant and the Relational Operator blocks in this model.

  4. Add an Autothreshold block from the Conversions library of the Computer Vision System Toolbox into your model.

  5. Use the Autothreshold block to perform a thresholding operation that converts your intensity image to a binary image. Select the Output threshold check box. This block outputs the calculated threshold value at the Th port.

  6. Add a Display block from the Sinks library of the DSP System Toolbox library. Connect the Display block to the Th output port of the Authothreshold block.

    Your model should look similar to the following figure:

  7. Double-click the Image From File block. On the Data Types pane, set the Output data type parameter to double.

  8. If you have not already done so, set the configuration parameters. Open the Configuration dialog box by selecting Model Configuration Parameters from the Simulation menu. Set the parameters as follows:

    • Solver pane, Stop time = 0

    • Solver pane, Type = Fixed-step

    • Solver pane, Solver = Discrete (no continuous states)

  9. Run the model.

    The original intensity image appears in the Video Viewer1 window.

    The binary image appears in the Video Viewer window.

    In the model window, the Display block shows the threshold value, calculated by the Autothreshold block, that separated the rice grains from the background.

You have used the Autothreshold block to convert an intensity image to a binary image. For more information about this block, see the Autothreshold block reference page in the Computer Vision System Toolbox Reference. To open an example model that uses this block, type vipstaples at the MATLAB command prompt.

Was this topic helpful?