Main Content

Shadow Detection by Using OpenCV Code in Simulink

This example shows how to detect shadows in a recorded video by using the OpenCV Importer.

First import an OpenCV function into Simulink® by using the Install and Use Computer Vision Toolbox Interface for OpenCV in Simulink. 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 to accept the recorded video and a threshold value for shadow detection. The shadow detection works per the threshold value. The threshold value can be changed 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 shadow detector is implemented by using the Simulink model ShadowDetection.slx.

In this model, the subsystem_slwrap_run_shadow_detection subsystem resides in the Shadow_Detection_Lib library. You create the subsystem_slwrap_run_shadow_detection subsystem by using the OpenCV Importer. The subsystem accepts a video from the From Multimedia File block and a threshold value to detect the shadow in the video. The output is displayed using the Video Viewer block. In the subsystem_slwrap_run_shadow_detection subsystem, inImage is the input image, thresh is the threshold of the algorithm and outImage is the output image. The threshold slider is used to change the threshold 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:


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\ShadowDetection. 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 Shadow_Detection. 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 shadow_detection.cpp.

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

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

4. From What to import, select the I/O Type for inImage and thresh as Input, outImage as Output and then click Next.

5. In Create Simulink Library, verify the default values and click Next.

A Simulink library Shadow_Detection_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_run_shadow_detection is used.

Step 2: Use Generated Subsystem in Simulink Model

To use the generated subsystem subsystem_slwrap_run_shadow_detection with the Simulink model ShadowDetection.slx:

1. In your MATLAB Current Folder, right-click the model ShadowDetection.slx and click Open from the context menu. Drag the generated subsystem subsystem_slwrap_detectAndDraw to the model. Connect the subsystem to the MATLAB Function block.

2. Double-click the subsystem and specify these parameter values.

3. Click Apply, and then click OK.

Step 3: Simulate the Shadow Detector

On the Simulink toolstrip, in the Simulation tab, click on simulate the model. After the simulation is complete, the Video Viewer block displays a video with shadows marked with a red outline depending on the threshold value. For the displayed example video, threshold value is set to 0.0651.

See Also


Related Topics