Generate Portable Code for Image Filtering Algorithm
R2026bThis example shows how to instruct the code generator to avoid using precompiled libraries for MATLAB® functions. By default, the code generator uses optimized, platform-specific, precompiled libraries for MATLAB functions if such libraries are available. Precompiled libraries can improve the performance of the generated code on the target platform. However, if you want to create portable applications that can run on many platforms, you can instruct the code generator to avoid using precompiled libraries if alternative implementations of their algorithms are available.
Because this example uses an image processing function, you must have the Image Processing Toolbox™ to run this example.
Create a MATLAB entry-point function libraryUsageTest that uses the
imfilter (Image Processing Toolbox) function to filter an input
image.
function filteredImage = libraryUsageTest(unfilteredImage) h = [-1 0 1]; filteredImage = imfilter(unfilteredImage,h); end
Generate a static C library for the libraryUsageTest function by
using the command. Create a code configuration object for a
static library and set the codegenInstructionSetExtensions property to
"None". Use the -args argument to specify that
the input argument is an 8-by-8 array of doubles and use the -report
option to generate a code generation report.
cfg = coder.config("lib"); cfg.InstructionSetExtensions = "None"; codegen -config cfg -report libraryUsageTest -args {zeros(8)}
Open the code generation report and inspect the generated C function
imfilter. The last line of this function calls the precompiled
library ippfilter_real64.
void imfilter(const double varargin_1[64], double b[64])
{
double aTmp[80];
int i;
for (i = 0; i < 8; i++) {
aTmp[i] = 0.0;
aTmp[i + 72] = 0.0;
memcpy(&aTmp[i * 8 + 8], &varargin_1[i * 8], 8U * sizeof(double));
}
double kernel[3];
double kernelSizeT[2];
double outSizeT[2];
double padSizeT[2];
outSizeT[0] = 8.0;
padSizeT[0] = 8.0;
outSizeT[1] = 8.0;
padSizeT[1] = 10.0;
kernel[0] = -1.0;
kernel[1] = 0.0;
kernel[2] = 1.0;
kernelSizeT[0] = 1.0;
kernelSizeT[1] = 3.0;
ippfilter_real64(&aTmp[0], &b[0], &outSizeT[0], 2.0, &padSizeT[0], &kernel[0],
&kernelSizeT[0], false);
}To generate portable C code that implements the image filtering algorithm, instruct the code generator to avoid precompiled libraries. Use one of these approaches:
In a MATLAB Coder™ code configuration object, set the
UsePrecompiledLibrariesproperty to"Avoid".In the MATLAB Coder Code Generation Settings dialog box, set Use precompiled libraries to
Avoid precompiled libraries when possible.In the Simulink® Configuration Parameters dialog box, set Use precompiled libraries for MATLAB functions (Simulink) to
Avoid precompiled libraries when possible.
For this example, set the UsePrecompiledLibraries property of the
code configuration object to "Avoid". Generate code for the
libraryUsageTest function.
cfg.UsePrecompiledLibraries = "Avoid"; codegen -config cfg -report libraryUsageTest -args {zeros(8)}
Open the code generation report and inspect the generated C function
imfilter. This function does not contain calls to precompiled
libraries.
void imfilter(const double varargin_1[64], double b[64])
{
double a_padded[80];
int b_i;
int i;
int jb;
for (i = 0; i < 8; i++) {
a_padded[i] = 0.0;
a_padded[i + 72] = 0.0;
memcpy(&a_padded[i * 8 + 8], &varargin_1[i * 8], 8U * sizeof(double));
}
memset(&b[0], 0, 64U * sizeof(double));
for (i = 0; i < 8; i++) {
int cColOffset;
cColOffset = i << 3;
for (jb = 0; jb < 3; jb++) {
int ia;
ia = (i + jb) << 3;
for (b_i = 0; b_i < 8; b_i++) {
int b_tmp;
b_tmp = cColOffset + b_i;
b[b_tmp] += (-(2.0 - (double)jb) + 1.0) * a_padded[ia + b_i];
}
}
}
}See Also
coder.MexCodeConfig | coder.CodeConfig | coder.EmbeddedCodeConfig
Topics
- Deploy Generated Code
- Relocate or Share Generated Code (Simulink Coder)
- Model Configuration Parameters: Simulation Target (Simulink)