Find the Histogram of an Image

The Histogram block computes the frequency distribution of the elements in each input image by sorting the elements into a specified number of discrete bins. You can use the Histogram block to calculate the histogram of the R, G, and/or B values in an image. This example shows you how to accomplish this task:

    Note:   Running this example requires a DSP System Toolbox™ license.

You can open the example model by typing

ex_vision_find_histogramex_vision_find_histogram

on the MATLAB® command line.

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

    Block

    Library

    Quantity

    Image From File

    Computer Vision System Toolbox™ > Sources

    1

    Video Viewer

    Computer Vision System Toolbox > Sinks

    1

    Matrix Concatenate

    Simulink > Math Operations

    1

    Vector Scope

    DSP System Toolbox > Sinks

    1

    Histogram

    DSP System Toolbox > Statistics

    3

  2. Use the Image From File block to import an RGB image. Set the block parameters as follows:

    • Sample time = inf

    • Image signal = Separate color signals

    • Output port labels: = R|G|B

    • On the Data Types tab, Output data type: = double

  3. Use the Video Viewer block to automatically display the original image in the viewer window when you run the model. Set the Image signal parameter to Separate color signals from the File menu.

  4. Use the Histogram blocks to calculate the histogram of the R, G, and B values in the image. Set the Main tab block parameters for the three Histogram blocks as follows:

    • Lower limit of histogram: 0

    • Upper limit of histogram: 1

    • Number of bins: = 256

    • Find the histogram over: = Entire Input

    The R, G, and B input values to the Histogram block are double-precision floating point and range between 0 and 1. The block creates 256 bins between the maximum and minimum input values and counts the number of R, G, and B values in each bin.

  5. Use the Matrix Concatenate block to concatenate the R, G, and B column vectors into a single matrix so they can be displayed using the Vector Scope block. Set the Number of inputs parameter to 3.

  6. Use the Vector Scope block to display the histograms of the R, G, and B values of the input image. Set the block parameters as follows:

    • Scope Properties pane, Input domain = User-defined

    • Display Properties pane, clear the Frame number check box

    • Display Properties pane, select the Channel legend check box

    • Display Properties pane, select the Compact display check box

    • Axis Properties pane, clear the Inherit sample increment from input check box.

    • Axis Properties pane, Minimum Y-limit = 0

    • Axis Properties pane, Maximum Y-limit = 1

    • Axis Properties pane, Y-axis label = Count

    • Line Properties pane, Line markers = .|s|d

    • Line Properties pane, Line colors = [1 0 0]|[0 1 0]|[0 0 1]

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

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

  9. Run the model using either the simulation button, or by selecting Simulation > Start.

    The original image appears in the Video Viewer window.

  10. Right-click in the Vector Scope window and select Autoscale.

    The scaled histogram of the image appears in the Vector Scope window.

You have now used the 2-D Histogram block to calculate the histogram of the R, G, and B values in an RGB image. To open a model that illustrates how to use this block to calculate the histogram of the R, G, and B values in an RGB video stream, type viphistogram at the MATLAB command prompt.

Was this topic helpful?