Documentation Center

  • Trial Software
  • Product Updates

Sharpen an Image

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 an image. The prime notation indicates that the signals are gamma corrected.

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

    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 array represents, type this command at the MATLAB command prompt:

    imshow(I)
    

    Now that you have defined your image, you can create your model.

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

    2-D FIR Filter

    Computer Vision System Toolbox > Filtering

    1

    Video Viewer

    Computer Vision System Toolbox > Sinks

    1

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

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

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

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

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

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

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

  11. Run the model.

    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.

In this example, you used the Color Space Conversion and 2-D FIR Filter blocks to sharpen an image. For more information, see the Color Space Conversion and 2-D FIR Filter, and fspecial reference pages.

Was this topic helpful?