Convert RGB Image to Grayscale Image by Using OpenCV Importer
This example shows how to convert an RGB image to a grayscale image by using theOpenCV Importerapp. The converter converts an RGB image to a grayscale image by eliminating the hue and saturation information while retaining the luminance.
First import an OpenCV function into Simulink by using theInstall 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 RGB image for conversion.
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 inPortable C Code Generation for Functions That Use OpenCV Library. Configure the identified compiler by using themex -setup c++
command. For more information, seeChoose a C++ Compiler.
Model Description
This example uses the Simulink modelToGrayScale.slx
.
In this model, thesubsystem_slwrap_toGrayScale
subsystem resides in theRGBtoGRAY_Lib
library. You create thesubsystem_slwrap_toGrayScale
subsystem by using theOpenCV 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.
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\ImageRGBtoGray
. All your output files are saved to this folder.
Step 1: Import OpenCV Function to Create a Simulink Library
1. To start theOpenCV Importerapp, clickAppson the MATLAB Toolstrip. In the Welcome page, specify theProject nameasRGBtoGRAY
. Make sure that the project name does not contain any spaces. ClickNext.
2. In Specify OpenCV Library, specify these file locations, and then clickNext.
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 astoGrayScale.cpp
.Include files: Specify the path of the
.hpp
header file located inside your project folder astoGrayScale.hpp
.
3. Analyze your library to find the functions and types for import. Once the analysis is complete, clickNext. Select thetoGrayScale
function and clickNext.
4. From What to import, select theI/O Typeforimg
asInputOutput
, and then clickNext.
5. In Create Simulink Library, configure the default values of OpenCV types. By default,Create a single C-caller block for the OpenCV function选择创建一个C调用块一起吗the subsystem in the generated Simulink library.
6. SelectConfigure library to use Simulink.ImageType signalsto configure the generated library subsystem to useSimulink.ImageType
signals.
7. SetDefault Color Format of Simlink.ImageType signaltoRGB
, which is the default color format of the image.
8. SetDefault Array layout of Simulink.ImageType signaltoColumn-major
, which is the default array layout of the image.
9. To create a Simulink library, clickNext.
A Simulink libraryRGBtoGRAY_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 subsystemsubsystem_slwrap_toGrayScale
is used.
Step 2: Use Generated Subsystem in Simulink Model
To use the generated subsystemsubsystem_slwrap_toGrayScale
with the Simulink modelToGrayScale.slx
:
1. In your MATLAB current folder, right-click the modelToGrayScale.slx
and clickOpenfrom the context menu. Drag the generated subsystem from the library to the model. Insert the subsystem between From Multimedia File block and Video Viewer block.
2. Double-click the subsystem and configure these parameter values:
Rows:
480
Columns:
640
Channels:
1
Underlying Type:
uint8
3. ClickApply, and then clickOK.
Step 3: Simulate the RGB to Gray Convertor
On the Simulink Toolstrip, in theSimulationtab, click onRunto simulate the model. After the simulation is complete, the Video Viewer block displays the grayscale image of the input imagepeppers.png
.
See Also
ToOpenCV|FromOpenCV|Simulink.ImageType