Products & Services Solutions Academia Support User Community Company

Learn more about Video and Image Processing Blockset   

Intensity to Binary Conversion

Overview of Intensity and 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. Intensity 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

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:

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

    Block

    Library

    Quantity

    Image From File

    Video and Image Processing Blockset > Sources

    1

    Video Viewer

    Video and Image Processing Blockset > Sinks

    2

    Relational Operator

    Simulink > Logic and Bit Operations

    1

    Constant

    Simulink > Sources

    1

  2. Position the blocks as shown in the following figure.

  3. 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

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

  5. 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.

  6. 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.

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

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

  9. Set the configuration parameters. Open the Configuration dialog box by selecting 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)

  10. 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 Correcting for 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 another example that uses this technique, see Counting Objects in an Image. 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.

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

    doc_thresholding

    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. From the Video and Image Processing Blockset library, and then from the Conversions library, click-and-drag an Autothreshold block into your model.

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

  6. Use the Autothreshold block to perform a thresholding operation that converts your intensity image to a binary image. Select the Output threshold check box.

    The block outputs the calculated threshold value at the Th port.

  7. From the Signal Processing Blockset library, and then from the Signal Processing Sinks library, click-and-drag a Display block into the model. Connect it to the Th port.

    Your model should look similar to the following figure:

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

  9. If you have not already done so, set the configuration parameters. Open the Configuration dialog box by selecting 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)

  10. 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 Video and Image Processing Blockset Reference. To open a demo model that uses this block, type vipstaples at the MATLAB command prompt.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS