Generate Code for Object Detection
This example shows how to generate C code using MATLAB® Coder™ from MATLAB applications that use Image Processing Toolbox™ functions. The example describes how to setup your MATLAB environment and prepare your MATLAB code for code generation.
This example also demonstrates how to solve issues that you might encounter in your MATLAB code that prevent code generation. To illustrate the process, the code used by this example includes some readiness issues and build issues that you must overcome before you can generate code.
Set Up Compiler
Specify which C/C++ compiler you want to use with MATLAB Coder to generate code by using the
mex function with the
Define Entry-Point Function
The entry-point function is a MATLAB function used as the source code for code generation. First, prototype the image processing workflow without support for code generation. This example defines a function called
detectCells.m that performs cell detection using segmentation and morphological techniques. This function is attached to the example as a supporting file.
Test the example code with a sample image,
I = imread('cell.tif'); Iseg = detectCells(I);
Confirm the accuracy of the segmentation by overlaying the segmented image on the original image.
Because you modify this code for code generation, it is good to work with a copy of the code. This example includes a copy of the helper function
detectCellsCodeGen.m. The version of the function used for code generation includes the MATLAB Coder compilation directive
%#codegen at the end of the function signature. This directive instructs the MATLAB code analyzer to diagnose issues that would prohibit successful code generation.
Open the MATLAB Coder app by using the
coder function. (Alternatively, in MATLAB, select the Apps tab, navigate to Code Generation and click the MATLAB Coder app.)
Specify the name of your entry-point function,
detectCellsCodeGen, and press Enter.
Determine Code Readiness for Code Generation
Click Next. MATLAB Coder identifies any issues that might prevent code generation. The example code contains five unsupported function calls.
Review the readiness issues. Click Review Issues. In the report, MATLAB Coder displays your code in an editing window with the readiness issues listed below, flagging uses of the
imshow function which does not support code generation.
Correct Readiness Issues
Address the readiness issues. Remove the calls to
imshow and related display code from your example. The display statements are not necessary for the segmentation operation. You can edit the example code directly in MATLAB Coder. When you have removed the code, click Save to save your edits and rerun the readiness check. After rerunning the readiness check, MATLAB Coder displays the No issues found message.
Define Size and Data Type of Function Inputs
Every input to your code must be specified to be of fixed size, variable size or a constant. There are several ways to specify the size of your input argument but the easiest way is by giving MATLAB Coder an example of calling your function. Enter a script that calls your function in the text entry field. For this example, enter the following code in the MATLAB prompt and press Autodefine Input Types.
I = imread('cell.tif'); Iseg = detectCellsCodeGen(I);
For more information about defining inputs, see the MATLAB Coder documentation. After MATLAB Coder returns with the input type definition, click Next.
Check for and Resolve Run-Time Issues
Even though you performed MATLAB Coder readiness checks, additional issues might arise during the build process that can prevent code generation. While the readiness checks look at function dependencies to determine readiness, the build process examines coding patterns. You can use the same code you entered to define input types (which is preloaded into the dialog box). Click Check for Issues.
This example contains a build issue: it passes an array of
strel objects to
imdilate and arrays of objects are not supported for code generation.
Address the build issues identified. For this example, modify the call to
imdilate to avoid passing an array of
strel objects. Replace the single call to
imdilate with two separate calls to
imdilate where you pass one strel object with each call.
Rerun the test build to make sure your changes fixed the issue. Click Check for Issues. MATLAB Coder displays a message declaring that no issues were detected.
You are now ready to generate code. Click Next.
Choose the type of code you want to generate and select the target platform. MATLAB Coder can generate C or C++ source code, a MEX file, a static library, a shared library, or a standalone executable. For Production Hardware, you can select from many choices including ARM and Intel processors.
This example uses the default options. The build type is Source Code and the language is C. For device options, specify a generic device from a device vendor and a MATLAB Host Computer for the device type. When you choose MATLAB Host Computer, MATLAB Coder generates code that depends on a precompiled shared library. Image Processing Toolbox functions use a shared library to preserve performance optimizations.
MATLAB Coder displays the generated code.
Click Next to complete the process. MATLAB Coder displays information about what it generated. By default, MATLAB Coder creates a
codegen subfolder in your work folder that contains the generated output.
codegen (MATLAB Coder) | MATLAB
Coder (MATLAB Coder)