Main Content

Draw Rectangles on Image

This example shows you how to draw rectangles on an image by overwriting the pixel values of the input image.

Example Model

Open the Simulink® model.

modelname = 'ex_blkDrawShapesRectangles.slx';
open_system(modelname)

This model reads an input image using a Image From File block with the File name parameter set to peppers.png.

The model then changes the data type of the input image to double, using an Image Data Type Conversion block with the Output data type parameter set to double.

The model draws filled, translucent rectangles at the locations specified by the Rectangular Shape Coordinates Constant (Simulink) block by using a Draw Shapes block with these parameters:

  • ShapeRectangles

  • Fill shapeson

  • Fill colorWhite

  • Opacity factor (between 0 and 1)0.4

The Draw Shapes block overwrites the value of each pixel in the specified rectangular areas of the original image.

Simulate Model

Run the model. The model displays the shape-embedded output image by using a Video Viewer block..

sim(modelname);