Adjust the Contrast of Color Images

This example shows you how to modify the contrast in color images using the Histogram Equalization block.

  1. Use the following code to read in an indexed RGB image, shadow.tif, and convert it to an RGB image. The model provided above already includes this code in file > Model Properties > Model Properties > InitFcn, and executes it prior to simulation.

    [X map] = imread('shadow.tif');
    shadow = ind2rgb(X,map);
  2. Create a new Simulink® model, and add to it the blocks shown in the following table.

    Block

    Library

    Quantity

    Image From Workspace

    Computer Vision System Toolbox™ > Sources

    1

    Color Space Conversion

    Computer Vision System Toolbox > Conversions

    2

    Histogram Equalization

    Computer Vision System Toolbox > Analysis & Enhancement

    1

    Video Viewer

    Computer Vision System Toolbox > Sinks

    2

    Constant

    Simulink > Sources

    1

    Divide

    Simulink > Math Operations

    1

    Product

    Simulink > Math Operations

    1

  3. Place the blocks listed in the table above into your new model.

  4. Use the Image From Workspace block to import the RGB image from the MATLAB® workspace into the Simulink model. Set the block parameters as follows:

    • Value = shadow

    • Image signal = Separate color signals

  5. Use the Color Space Conversion block to separate the luma information from the color information. Set the block parameters as follows:

    • Conversion = sR'G'B' to L*a*b*

    • Image signal = Separate color signals

    Because the range of the L* values is between 0 and 100, you must normalize them to be between zero and one before you pass them to the Histogram Equalization block, which expects floating point input in this range.

  6. Use the Constant block to define a normalization factor. Set the Constant value parameter to 100.

  7. Use the Divide block to normalize the L* values to be between 0 and 1. Accept the default parameters.

  8. Use the Histogram Equalization block to modify the contrast in the image. This block enhances the contrast of images by transforming the luma values in the color image so that the histogram of the output image approximately matches a specified histogram. Accept the default parameters.

  9. Use the Product block to scale the values back to be between the 0 to 100 range. Accept the default parameters.

  10. Use the Color Space Conversion1 block to convert the values back to the sR'G'B' color space. Set the block parameters as follows:

    • Conversion = L*a*b* to sR'G'B'

    • Image signal = Separate color signals

  11. Use the Video Viewer blocks to view the original and modified images. For each block, set the Image signal parameter to Separate color signals from the file menu.

  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.

    As shown in the following figure, the model displays the original image in the Video Viewer1 window.

    As the next figure shows, the model displays the enhanced contrast image in the Video Viewer window.

In this example, you used the Histogram Equalization block to transform the values in a color image so that the histogram of the output image approximately matches a uniform histogram. For more information, see the Histogram Equalization reference page.

Was this topic helpful?