Correct Nonuniform Illumination

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.

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


    Video Viewer

    Computer Vision System Toolbox > Sinks



    Simulink > Sources



    Simulink > Math Operations


    Data Type Conversion

    Simulink > Signal Attributes


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

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

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

  5. Use the Video Viewer1 block to view the background estimated by the Opening block. Accept the default parameters.

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

  7. Use the Video Viewer2 block to view the result of subtracting the background from the original image. Accept the default parameters.

  8. Use the Constant block to define an offset value. Set the Constant value parameter to 80.

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

  10. 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 = rectangular

    • List of signs = ++

  11. Use the Video Viewer3 block to view the corrected image. Accept the default parameters.

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

  13. 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)

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

Was this topic helpful?