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
on the MATLAB® command line.
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('disk',15).
The 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 first Sum block to subtract the estimated background from the original image. Set the block parameters as follows:
Icon shape = rectangular
List of signs = -+
Use the Constant block to define an offset value. Set the Constant value parameter to 80.
Use the Data Type Conversion block to convert the offset value to an 8-bit unsigned integer. Set the Output data type mode parameter to uint8.
Icon shape = rectangular
List of signs = ++
Solver pane, Stop time = 0
Solver pane, Type = Fixed-step
Solver pane, Solver = discrete (no continuous states)
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.