Remove Salt and Pepper Noise from Images

Median filtering is a common image enhancement technique for removing salt and pepper noise. Because this filtering is less sensitive than linear techniques to extreme changes in pixel values, it can remove salt and pepper noise without significantly reducing the sharpness of an image. In this topic, you use the Median Filter block to remove salt and pepper noise from an intensity image:

  1. Define an intensity image in the MATLAB® workspace and add noise to it by typing the following at the MATLAB command prompt:

    I= double(imread('circles.png'));
    I= imnoise(I,'salt & pepper',0.02);

    Iis a 256-by-256 matrix of 8-bit unsigned integer values.

    The model provided with this example already includes this code in file>Model Properties>Model Properties>InitFcn, and executes it prior to simulation.

  2. To view the image this matrix represents, at the MATLAB command prompt, type


    The intensity image contains noise that you want your model to eliminate.

  3. Create a Simulink® model, and add the blocks shown in the following table.




    Image From Workspace

    Computer Vision System Toolbox™ > Sources


    Median Filter

    Computer Vision System Toolbox > Filtering


    Video Viewer

    Computer Vision System Toolbox > Sinks


  4. Use the Image From Workspace block to import the noisy image into your model. Set the Value parameter to I.

  5. Use the Median Filter block to eliminate the black and white speckles in the image. Use the default parameters.

    The Median Filter block replaces the central value of the 3-by-3 neighborhood with the median value of the neighborhood. This process removes the noise in the image.

  6. Use the Video Viewer blocks to display the original noisy image, and the modified image. Images are represented by 8-bit unsigned integers. Therefore, a value of 0 corresponds to black and a value of 255 corresponds to white. Accept the default parameters.

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

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

  9. Run the model.

    The original and filtered images are displayed.

You have used the Median Filter block to remove noise from your image. For more information about this block, see the Median Filter block reference page in the Computer Vision System Toolbox Reference.

Was this topic helpful?