Main Content

Video Cartoonizer by Using OpenCV Code in Simulink

This example shows how to use video cartoonizer to enhance colors of the recorded video by using the OpenCV Importer app. The cartoonizer varies the colors in the video using parameters such as maskRadius, threshold, and ramp.

First import an OpenCV function into Simulink® by following the Install and Use Computer Vision Toolbox Interface for OpenCV in Simulink. The app 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 to accept the recorded video. The cartoonizer works per the maskRadius, threshold, and ramp value. You can change the maskRadius, threshold, and ramp values using the slider component available in the model.

You learn how to:

  • Import an OpenCV function into a Simulink library.

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

Set Up Your C++ Compiler

To build the OpenCV libraries, identify a compatible C++ compiler for your operating system, as described in Portable C Code Generation for Functions That Use OpenCV Library. Configure the identified compiler by using the mex -setup c++ command. For more information, see Choose a C++ Compiler.

Model Description

In this example, a video cartoonizer is implemented by using the Simulink model Cartoonizer.slx.

In this model, the subsystem_slwrap_cartoonize subsystem resides in the Cartoonizer_Lib library. You create the subsystem_slwrap_cartoonize subsystem by using the OpenCV Importer app. The subsystem accepts a video from the From Multimedia File block, maskRadius, threshold, and a ramp value to cartoonize the video. The output is displayed using the Video Viewer block. In the subsystem_slwrap_cartoonize subsystem, inImage is the input image, maskRadius is the size of the image filter for intensity comparison, threshold is the threshold intensity difference between pixels which results in darkening the video, ramp is intensity gradient in the output image and outImage is the output image. The sliders of maskRadius, threshold, and ramp is used to change the value during the simulation.

Copy Example Folder to a Writable Location

To access the path to the example folder, at the MATLAB® command line, enter:

     OpenCVSimulinkExamples;

Each subfolder contains all the supporting files required to run the example.

Before proceeding with these steps, ensure that you copy the example folder to a writable folder location and change your current working folder to ...example\Cartoonizer. All your output files are saved to this folder.

Step 1: Import OpenCV Function to Create a Simulink Library

1. To start the OpenCV Importer app, click Apps on the MATLAB Toolstrip. In the Welcome page, specify the Project name as Cartoonizer. Make sure that the project name does not contain any spaces. Click Next.

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

  • Project root folder: Specify the path of your example folder. This path is the path to the writable project folder where you have saved your example files. All your output files are saved to this folder.

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

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

3. Analyze your library to find the functions and types for import. Once the analysis is complete, click Next. Select the cartoonize function and click Next.

4. From What to import, select the I/O Type for inImage, maskRadius, threshold, and ramp as Input, outImg as Output and then click Next.

5. In Create Simulink Library, configure 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 in the generated Simulink library.

6. Select Configure library to use Simulink.ImageType signals to configure the generated library subsystem to use Simulink.ImageType signals.

7. Set Default Color Format of Simlink.ImageType signal to RGB, which is the default color format of the image.

8. Set Default Array layout of Simulink.ImageType signal to Column-major, which is the default array layout of the image.

9. To create a Simulink library, click Next.

A Simulink library cartoonize_Lib is created from your OpenCV code into the project root folder. The library contains a subsystem and a C Caller block. You can use any of these blocks for model simulation. In this example, the subsystem subsystem_slwrap_cartoonize is used.

Step 2: Use Generated Subsystem in Simulink Model

To use the generated subsystem subsystem_slwrap_cartoonize with the Simulink model smileDetect.slx:

1. In your MATLAB current folder, right-click the model Cartoonizer.slx and click Open from the context menu. Drag the generated subsystem from the library to the model. Insert the subsystem between the input blocks and the Video Viewer block.

2. Double-click the subsystem and configure these parameter values:

  • Rows: 240

  • Columns: 360

  • Channels: 3

  • Underlying Type: uint8

3. Click Apply, and then click OK.

Step 3: Simulate the Cartoonizer

On the Simulink Toolstrip, in the Simulation tab, click on Run to simulate the model. After the simulation is complete, the Video Viewer block displays the video with color enhancement based on the value of maskRadius, threshold, and ramp.

See Also

| |

Related Topics