Crop an Image

You can use the Selector block to crop your image or video stream. In this example, you learn how to use the Selector block to trim an image down to a particular region of interest:

  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

    2

    Selector

    Simulink > Signal Routing

    1

  2. Use the Image From File block to import the intensity image. Set the File name parameter to coins.png. The image is a 246-by-300 matrix of 8-bit unsigned integer values.

  3. Use the Video Viewer block to display the original image. Accept the default parameters. This block automatically displays the original image in the Video Viewer window when you run the model.

  4. Use the Selector block to crop the image. Set the block parameters as follows:

    • Number of input dimensions = 2

    • 1

      • Index Option = Starting index (dialog)

      • Index = 140

      • Output Size = 70

    • 2

      • Index Option = Starting index (dialog)

      • Index = 200

      • Output Size = 70

    The Selector block starts at row 140 and column 200 of the image and outputs the next 70 rows and columns of the image.

  5. Use the Video Viewer1 block to display the cropped image. This block automatically displays the modified image in the Video Viewer window when you run the model.

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

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

  8. Run the model.

    The original image appears in the Video Viewer window.

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

In this example, you used the Selector block to crop an image. For more information about the Selector block, see the Simulink documentation. For information about the imcrop function, see the Image Processing Toolbox™ documentation.

Was this topic helpful?