This example shows how to convert an RGB image to a grayscale image by using the OpenCV Importer. The converter converts an RGB image to a grayscale image by eliminating the hue and saturation information while retaining the luminance.
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 to accept the RGB image for conversion.
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.
Computer Vision Toolbox™ OpenCV Interface for Simulink
Computer Vision Toolbox
Embedded Coder®
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.
This example uses the Simulink model ToGrayScale.slx
.
In this model, the subsystem_slwrap_toGrayScale
subsystem resides
in the RGBtoGRAY_OpenCVCallerLib
library. You create the
subsystem_slwrap_toGrayScale
subsystem by using the OpenCV
Importer. The subsystem accepts an RGB image from the Image From
File block and converts it to a grayscale output image. The output is then
displayed on a Video Viewer block.
To start the OpenCV Importer app, click Apps
on the MATLAB® Toolstrip. In the Welcome page, specify the Project
name as RGBtoGRAY
. Make sure that the project
name does not contain any spaces. Click Next.
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\ImageRGBtoGray
.
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 toGrayScale.cpp
.
Include files: Specify the path of the
.hpp
header file located inside your project
folder as toGrayScale.hpp
.
Analyze your library to find the functions and types for import. Once the
analysis is completed, click Next. Select the
toGrayScale
function and click
Next.
From What to import, click the I/O Type for
img as InputOutput
, and then
click Next.
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 RGBtoGRAY_OpenCVCallerLib
is created
from your OpenCV code. You can use any of the blocks in the library for model
simulation. In this example, the subsystem
subsystem_slwrap_toGrayScale
is used.
To use the generated subsystem subsystem_slwrap_toGrayScale
with
the Simulink model toGrayScale.slx
:
In your MATLAB
Current Folder, right-click the model
toGrayScale.slx
and click
Open
from the context menu. Drag the
generated subsystem to the model and connect the blocks.
Double-click the subsystem and specify these parameter values.
Parameters | Values | Description |
---|---|---|
Rows | 384 | Number of rows in the output image |
Columns | 512 | Number of columns in the output image |
Channels | 1 | Number of channels in the output image |
Underlying Type | uint8 | Underlying data type of OpenCV
Mat |
is Image | on | Whether input is an image or a matrix |
Click Apply and then click OK.
On the Simulink Toolstrip, in the Simulation tab,
click to simulate the model. After the simulation is
complete, the Video Viewer block displays the grayscale image of the
input image
peppers.png
.