| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Video and Image Processing Blockset |
| Contents | Index |
| Learn more about Video and Image Processing Blockset |
| On this page… |
|---|
Sharpening and Blurring an Image Removing Salt and Pepper Noise from Images Removing Periodic Noise from Video |
To sharpen a color image, you need to make the luma intensity transitions more acute, while preserving the color information of the image. To do this, you convert an R'G'B' image into the Y'CbCr color space and apply a highpass filter to the luma portion of the image only. Then, you transform the image back to the R'G'B' color space to view the results. To blur an image, you apply a lowpass filter to the luma portion of the image. This example shows how to use the 2-D FIR Filter block to sharpen and blur an image. The prime notation indicates that the signals are gamma corrected.
Define an R'G'B' image in the MATLAB workspace. To read in an R'G'B' image from a PNG file and cast it to the double-precision data type, at the MATLAB command prompt, type
I= im2double(imread('peppers.png'));
I is a 384-by-512-by-3 array of double-precision floating-point values. Each plane of this array represents the red, green, or blue color values of the image.
To view the image this array represents, at the MATLAB command prompt, type
imshow(I)

Now that you have defined your image, you can create your model.
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
Image From Workspace | Video and Image Processing Blockset > Sources | 1 |
Color Space Conversion | Video and Image Processing Blockset > Conversions | 2 |
2-D FIR Filter | Video and Image Processing Blockset > Filtering | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 1 |
Position the blocks as shown in the following figure.

Use the Image From Workspace block to import the R'G'B' image from the MATLAB workspace. Set the parameters as follows:
Main pane, Value = I
Main pane, Image signal = Separate color signals
The block outputs the R', G', and B' planes of the I array at the output ports.
The first Color Space Conversion block converts color information from the R'G'B' color space to the Y'CbCr color space. Set the Image signal parameter to Separate color signals

Use the 2-D FIR Filter block to filter the luma portion of the image. Set the block parameters as follows:
Coefficients = fspecial('unsharp')
Output size = Same as input port I
Padding options = Symmetric
Filtering based on = Correlation

The fspecial('unsharp') command creates two-dimensional highpass filter coefficients suitable for correlation. This highpass filter sharpens the image by removing the low frequency noise in it.
The second Color Space Conversion block converts the color information from the Y'CbCr color space to the R'G'B' color space. Set the block parameters as follows:
Conversion = Y'CbCr to R'G'B'
Image signal = Separate color signals

Use the Video Viewer block to automatically display the new, sharper image in the Video Viewer window when you run the model. Set the Image signal parameter to Separate color signals, by selecting File > Image Signal.
Connect the blocks as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting 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)
A sharper version of the original image appears in the Video Viewer window.

To blur the image, double-click the 2-D FIR Filter block. Set Coefficients parameter to fspecial('gaussian',[15 15],7) and then click OK.
The fspecial('gaussian',[15 15],7) command creates two-dimensional Gaussian lowpass filter coefficients. This lowpass filter blurs the image by removing the high frequency noise in it.
A blurred version of the original image appears in the Video Viewer window. The following image is shown at its true size.

In this example, you used the Color Space Conversion and 2-D FIR Filter blocks to sharpen and blur an image. For more information on these blocks, see the Color Space Conversion and 2-D FIR Filter block reference pages in the Video and Image Processing Blockset Reference. For more information on the fspecial function, see the Image Processing Toolbox documentation.
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:
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);
I is a 256-by-256 matrix of 8-bit unsigned integer values.
To view the image this matrix represents, at the MATLAB command prompt, type
imshow(I)

The intensity image contains noise that you want your model to eliminate.
Create a Simulink model, and add the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
Image From Workspace | Video and Image Processing Blockset > Sources | 1 |
Median Filter | Video and Image Processing Blockset > Filtering | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 2 |
Position the blocks as shown in the following figure.

Use the Image From Workspace block to import the noisy image into your model. Set the Value parameter to I.
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.
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.
Connect the blocks as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting 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)
The original noisy image appears in the Video Viewer window. To view the image at its true size, right-click the window and select Set Display To True Size.

The cleaner image appears in the Video Viewer1 window. The following image is shown at its true size.

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 Video and Image Processing Blockset Reference.
Periodic noise can be introduced into a video stream during acquisition or transmission due to electrical or electromechanical interference. In this example, you remove periodic noise from an intensity video using the 2-D FIR Filter block. You can use this technique to remove noise from other images or video streams, but you might need to modify the filter coefficients to account for the noise frequency content present in your signal:
Create a Simulink model, and add the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
Read Binary File | Video and Image Processing Blockset > Sources | 1 |
Image Data Type Conversion | Video and Image Processing Blockset > Conversions | 1 |
2-D FIR Filter | Video and Image Processing Blockset > Filtering | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 3 |
Add | Simulink > Math Operations | 1 |
Open the Periodic noise reduction demo by typing vipstripes at the MATLAB command prompt.
Click-and-drag the Periodic Noise block into your model.
The block outputs a sinusoid with a normalized frequency that ranges between 0.61π and 0.69π radians per sample and a phase that varies between zero and three radians. You are using this sinusoid to represent periodic noise.
Place the blocks so that your model resembles the following figure. The unconnected ports disappear when you set block parameters.

You are now ready to set your block parameters by double-clicking the blocks, modifying the block parameter values, and clicking OK.
Use the Read Binary File block to import a binary file into the model. Set the block parameters as follows:
File name = cat_video.bin
Four character code = GREY
Number of times to play file = inf
Sample time = 1/30

Use the Image Data Type Conversion block to convert the data type of the video to single-precision floating point. Accept the default parameter.
Use the Video Viewer block to view the original video. Accept the default parameters.
Use the Add block to add the noise video to the original video. Accept the default parameters.
Use the Video Viewer1 block to view the noisy video. Accept the default parameters.
Define the filter coefficients in the MATLAB workspace. Type the following code at the MATLAB command prompt:
vipdh_stripes
The variable h, as well as several others, are loaded into the MATLAB workspace. The variable h represents the coefficients of the band reject filter capable of removing normalized frequencies between 0.61π and 0.69π radians per sample. The coefficients were created using the Filter Design and Analysis Tool (FDATool) and the ftrans2 function.
Use the 2-D FIR Filter block to model a band-reject filter capable of removing the periodic noise from the video. Set the block parameters as follows:
Coefficients = h
Output size = Same as input port I
Padding options = Circular
Choose a type of padding that minimizes the effect of the pixels outside the image on the processing of the image. In this example, circular padding produces the best results because it is most effective at replicating the sinusoidal noise outside the image.

Use the Video Viewer2 block to view the approximation of the original video. Accept the default parameters.
Connect the block as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting Configuration Parameters from the Simulation menu. Set the parameters as follows:
Solver pane, Stop time = inf
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)

The noisy video appears in the Video Viewer1 window. The following video is shown at its true size.

The approximation of the original video appears in the Video Viewer2 window, and the artifacts of the processing appear near the edges of the video. The following video is shown at its true size.

You have used the Read Binary File block to import a binary video into your model, the 2-D FIR Filter to remove periodic noise from this video, and the Video Viewer block to display the results. For more information about these blocks, see the Read Binary File, 2-D FIR Filter, and Video Viewer block reference pages in the Video and Image Processing Blockset Reference. For more information about the Filter Design and Analysis Tool (FDATool), see the Signal Processing Toolbox documentation. For information about the ftrans2 function, see the Image Processing Toolbox documentation.
This example shows you how to modify the contrast in two intensity images using the Contrast Adjustment and Histogram Equalization blocks.
Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
Image From File | Video and Image Processing Blockset > Sources | 2 |
Contrast Adjustment | Video and Image Processing Blockset > Analysis & Enhancement | 1 |
Histogram Equalization | Video and Image Processing Blockset > Analysis & Enhancement | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 4 |
Place the blocks so that your model resembles the following figure.

Use the Image From File block to import the first image into the Simulink model. Set the File name parameter to pout.tif.
Use the Image From File1 block to import the second image into the Simulink model. Set the File name parameter to tire.tif.
Use the Contrast Adjustment block to modify the contrast in pout.tif. Set the Adjust pixel values from parameter to Range determined by saturating outlier pixels, as shown in the following figure.

This block adjusts the contrast of the image by linearly scaling the pixel values between user-specified upper and lower limits.
Use the Histogram Equalization block to modify the contrast in tire.tif. Accept the default parameters.

This block enhances the contrast of images by transforming the values in an intensity image so that the histogram of the output image approximately matches a specified histogram.
Use the Video Viewer blocks to view the original and modified images. Accept the default parameters.
Connect the blocks as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting 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)
Run the model.
The results appear in the Video Viewer windows.

In this example, you used the Contrast Adjustment block to linearly scale the pixel values in pout.tif between new upper and lower limits. You used the Histogram Equalization block to transform the values in tire.tif so that the histogram of the output image approximately matches a uniform histogram. For more information, see the Contrast Adjustment and Histogram Equalization block reference pages in the Video and Image Processing Blockset Reference.
This example shows you how to modify the contrast in color images using the Histogram Equalization block.
Use the following code to read in an indexed RGB image, shadow.tif, and convert it to an RGB image.
[X map] = imread('shadow.tif');
shadow = ind2rgb(X,map);Create a new Simulink model, and add to it the blocks shown in the following table.
Block | Library | Quantity |
|---|---|---|
Image From Workspace | Video and Image Processing Blockset > Sources | 1 |
Color Space Conversion | Video and Image Processing Blockset > Conversions | 2 |
Histogram Equalization | Video and Image Processing Blockset > Analysis & Enhancement | 1 |
Video Viewer | Video and Image Processing Blockset > Sinks | 2 |
Constant | Simulink > Sources | 1 |
Divide | Simulink > Math Operations | 1 |
Product | Simulink > Math Operations | 1 |
Place the blocks so that your model resembles the following figure.

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
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.
Use the Constant block to define a normalization factor. Set the Constant value parameter to 100.
Use the Divide block to normalize the L* values to be between 0 and 1. Accept the default parameters.
Use the Histogram Equalization block to modify the contrast in the image. Accept the default parameters.

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.
Use the Product block to scale the values back to be between the 0 to 100 range. Accept the default parameters.
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
Use the Video Viewer blocks to view the original and modified images. For each block, set the Image signal parameter to Separate color signals.
Connect the blocks as shown in the following figure.

Set the configuration parameters. Open the Configuration dialog box by selecting 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)
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 block reference page in the Video and Image Processing Blockset Reference.
![]() | Feature Extraction | Template Matching | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |