This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Rotate an Image

You can use the Rotate block to rotate your image or video stream by a specified angle. In this example, you learn how to use the Rotate block to continuously rotate an image.


Running this example requires a DSP System Toolbox™ license.

  1. Define an RGB image in the MATLAB® workspace. At the MATLAB command prompt, type

    I = checker_board;

    I is a 100-by-100-by-3 array of double-precision values. Each plane of the array represents the red, green, or blue color values of the image.

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


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




    Image From Workspace

    Computer Vision System Toolbox™ > Sources



    Computer Vision System Toolbox > Geometric Transformations


    Video Viewer

    Computer Vision System Toolbox > Sinks



    Simulink > Math Operations



    DSP System Toolbox > Sinks



    DSP System Toolbox > Signal Management > Switches and Counters


  4. Use the Image From Workspace block to import the RGB image from the MATLAB workspace. On the Main pane, set the Value parameter to I. Each plane of the array represents the red, green, or blue color values of the image.

  5. Use the Video Viewer block to display the original image. Accept the default parameters.

    The Video Viewer block automatically displays the original image in the Video Viewer window when you run the model. Because the image is represented by double-precision floating-point values, a value of 0 corresponds to black and a value of 1 corresponds to white.

  6. Use the Rotate block to rotate the image. Set the block parameters as follows:

    • Rotation angle source = Input port

    • Sine value computation method = Trigonometric function

    The Angle port appears on the block. You use this port to input a steadily increasing angle. Setting the Output size parameter to Expanded to fit rotated input image ensures that the block does not crop the output.

  7. Use the Video Viewer1 block to display the rotating image. Accept the default parameters.

  8. Use the Counter block to create a steadily increasing angle. Set the block parameters as follows:

    • Count event = Free running

    • Counter size = 16 bits

    • Output = Count

    • Clear the Reset input check box.

    • Sample time = 1/30

    The Counter block counts upward until it reaches the maximum value that can be represented by 16 bits. Then, it starts again at zero. You can view its output value on the Display block while the simulation is running. The Counter block's Count data type parameter enables you to specify it's output data type.

  9. Use the Gain block to convert the output of the Counter block from degrees to radians. Set the Gain parameter to pi/180.

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

  11. 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 = inf

    • Solver pane, Type = Fixed-step

    • Solver pane, Solver = Discrete (no continuous states)

  12. Run the model.

    The original image appears in the Video Viewer window.

    The rotating image appears in the Video Viewer1 window.

In this example, you used the Rotate block to continuously rotate your image. For more information about this block, see the Rotate block reference page in the Computer Vision System Toolbox Reference. For more information about other geometric transformation blocks, see the Resize and Shear block reference pages.


If you are on a Windows operating system, you can replace the Video Viewer block with the To Video Display block, which supports code generation.

Was this topic helpful?