Global threshold techniques, which are often the first step in object measurement, cannot be applied to unevenly illuminated images. To correct this problem, you can change the lighting conditions and take another picture, or you can use morphological operators to even out the lighting in the image. Once you have corrected for nonuniform illumination, you can pick a global threshold that delineates every object from the background. In this topic, you use the Opening block to correct for uneven lighting in an intensity image:
You can open the example model by typing
Create a new Simulink® model, and add to it the blocks shown in the following table.
Image From File
Computer Vision System Toolbox™ > Sources
Computer Vision System Toolbox > Morphological Operations
Computer Vision System Toolbox > Sinks
Simulink > Sources
Simulink > Math Operations
Data Type Conversion
Simulink > Signal Attributes
Use the Image
From File block to import the intensity image. Set the File
name parameter to
rice.png. This image
is a 256-by-256 matrix of 8-bit unsigned integer values.
Use the Video Viewer block to view the original image. Accept the default parameters.
Use the Opening block
to estimate the background of the image. Set the Neighborhood
or structuring element parameter to
strel function creates
a circular STREL object with a radius of 15 pixels. When working with
the Opening block, pick a STREL object that fits within the objects
you want to keep. It often takes experimentation to find the neighborhood
or STREL object that best suits your application.
Use the Video Viewer1 block to view the background estimated by the Opening block. Accept the default parameters.
Use the first Sum block to subtract the estimated background from the original image. Set the block parameters as follows:
Icon shape =
List of signs =
Use the Video Viewer2 block to view the result of subtracting the background from the original image. Accept the default parameters.
Use the Constant block
to define an offset value. Set the Constant value parameter
Use the Data
Type Conversion block to convert the offset value to an 8-bit
unsigned integer. Set the Output data type mode parameter
Use the second Sum block to lighten the image so that it has the same brightness as the original image. Set the block parameters as follows:
Icon shape =
List of signs =
Use the Video Viewer3 block to view the corrected image. Accept the default parameters.
Connect the blocks as shown in the following figure.
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
Solver pane, Type =
Solver pane, Solver =
(no continuous states)
Run the model.
The original image appears in the Video Viewer window.
The estimated background appears in the Video Viewer1 window.
The image without the estimated background appears in the Video Viewer2 window.
The preceding image is too dark. The Constant block provides an offset value that you used to brighten the image.
The corrected image, which has even lighting, appears in the Video Viewer3 window. The following image is shown at its true size.
In this section, you have used the Opening block
to remove irregular illumination from an image. For more information
about this block, see the Opening reference
page. For related information, see the Top-hat block
reference page. For more information about STREL objects, see the
strel function in the Image Processing Toolbox™ documentation.