Draw Different Shapes by Using OpenCV Code in Simulink

This example shows how to draw different shapes on images.

In this example, you first import an OpenCV function into Simulink® by using the OpenCV Code Import Wizard. The wizard creates a Simulink library that contains a subsystem and a C Caller block for the specified OpenCV function. The subsystem is then used in a preconfigured Simulink model. This subsystem accepts coordinates of a specified shape and results in a defined shape that is displayed on a Video Viewer.

In this example, you learn how to:

  • Import an OpenCV function into a Simulink library.

  • Use blocks from a generated library in a Simulink model.

Required Products

  • Computer Vision Toolbox™ OpenCV Interface for Simulink

  • Computer Vision Toolbox

  • Embedded Coder®

Set Up Your C++ Compiler

To build the OpenCV libraries, identify a compatible C++ compiler for your operating system, as described in Compiler used to build OpenCV libraries. Configure the identified compiler by using the mex -setup c++ command. For more information, see Choose a C++ Compiler.

Model Description

These Simulink models are available in the DrawShapes folder:

  • DrawAtom.slx

  • DrawEllipse.slx

  • DrawFilledCircle.slx

  • DrawLine.slx

  • DrawPolygon.slx

  • DrawRook.slx

This example uses the DrawFilledCircle.slx model. In this model, the subsystem_slwrap_drawFilledCircle subsystem resides in the DrawCircle_OpenCVCallerLib library. You create the subsystem_slwrap_drawFilledCircle subsystem by using the OpenCV Importer. The subsystem accepts the x and y coordinates for the center of the circle and radius as input to the subsystem. It creates a circle on an input image from the Image From File block. The output is then displayed on a Video Viewer block.

Step 1: Import OpenCV Function to Create a Simulink Library

  1. To start the OpenCV Importer app, click Apps on the MATLAB® Toolstrip. The OpenCV import wizard opens to a Welcome page. Specify the Project name as DrawCircle. Make sure that the project name does not contain any spaces. Click Next.

  2. In Specify OpenCV Library, specify these file locations and click Next.

    • Project root folder: Specify the path to the example folder as matlab\toolbox\simulink\supportpackages\simulinkopencv\example\DrawShapes. This folder contains all the supporting files required to run this example. All your output files are saved to this folder.

    • Source files: Specify the path of the .cpp file located inside your project folder as opencvcode.cpp.

    • Include files: Specify the path of the .hpp header file located inside your project folder as opencvcode.hpp.

  3. Analyze your library to find the functions and types for import. Once the analysis is completed, click Next. From the listed functions, select the drawFilledCircle function and click Next.

  4. From What to import, select the I/O Type for img as InputOutput and other arguments as Input. Click Next.

  5. In Create Simulink Library, verify the default values of OpenCV types. By default, Create a single C-caller block for the OpenCV function is selected to create a C Caller block along with the subsystem. To create a Simulink library, click Next.

    A Simulink library DrawCircle_OpenCVCallerLib is created from your OpenCV code. You can use any of these blocks for model simulation. In this example, the subsystem subsystem_slwrap_drawFilledCircle is used.

Step 2: Use Generated Subsystem in Simulink Model

To use the generated subsystem subsystem_slwrap_drawFilledCircle with the Simulink model DrawFilledCircle.slx:

  1. In your MATLAB Current Folder, right-click the model DrawFilledCircle.slx and click Open from the context menu. Drag the generated subsystem to the model and connect the blocks.

  2. Double-click the subsystem and verify the parameter values.

On the Simulink Toolstrip, in the Simulation tab, click to simulate the model. After the simulation is complete, the Video Viewer block displays the filled circle on the input image peppers.png.

Draw Atom on Image by Using C Caller Block

This example shows how to use a C Caller block in a Simulink model to draw an atom on an image.

  1. Import drawEllipse and drawFilledCircle OpenCV functions into Simulink by using the OpenCV Code Import Wizard. During import, select the I/O Type for drawEllipse and drawFilledCircle as shown in this graphic.

  2. Once you import the functions, the DrawCircle_OpenCVCallerLib library is created. This Simulink library contains subsystems and the C Caller blocks required to draw atom on an image.

    Open the model DrawAtomCcaller.slx. Drag the slwrap_drawEllipse C Caller block from the Simulink library DrawCircle_OpenCVCallerLib. Create three copies of the C Caller block and then place these blocks at the four blank positions inside the drawEllipses subsystem.

    In the model, drag the slwrap_drawEllipse C Caller block from the Simulink library DrawCircle_OpenCVCallerLib and place the block at the blank position.

  3. On the Simulink Toolstrip, in the Simulation tab, click to simulate the model. After the simulation is complete, the Video Viewer block displays the atom on a white input image.

See Also


Related Topics