为使用半精度数据类型的索贝尔边缘检测生成代码

索贝尔边缘检测算法

```type sobelEdgeDetectionAlg ```
```function edgeImg = sobelEdgeDetectionAlg(img,thresh) %#codegen % Entry-point function for half-precision Sobel edge detection example. % Copyright 2018-2022 The MathWorks, Inc. kern = half([1 2 1; 0 0 0; -1 -2 -1]); % Finding horizontal and vertical gradients. h = conv2(img(:,:,2),kern,'same'); v = conv2(img(:,:,2),kern','same'); % Finding magnitude of the gradients. e = sqrt(h.*h + v.*v); % Threshold the edges edgeImg = uint8((e > thresh) * 240); end ```

读取图像并将数据打包为 RGBA 打包的列优先顺序

```im = imread('peppers.png'); figure(); image(im); imPacked = half(im)/255; thresh = half(100)/255; ```

生成 MEX

```cfg = coder.config('mex'); cfg.TargetLang = 'C++'; cfg.GenerateReport = true; codegen -config cfg -args {imPacked,thresh} sobelEdgeDetectionAlg ```
```Code generation successful: To view the report, open('codegen/mex/sobelEdgeDetectionAlg/html/report.mldatx') ```

运行生成的 MEX 并显示检测到的边缘

```out_disp = sobelEdgeDetectionAlg_mex(imPacked,thresh); figure(); imagesc(out_disp); ```

生成静态 C++ 库

```cfg = coder.config('lib'); cfg.TargetLang = 'C++'; cfg.GenerateReport = true; codegen -config cfg -args {imPacked,thresh} sobelEdgeDetectionAlg; ```
```Code generation successful: To view the report, open('codegen/lib/sobelEdgeDetectionAlg/html/report.mldatx') ```

检查生成的函数

```type codegen/lib/sobelEdgeDetectionAlg/sobelEdgeDetectionAlg.cpp ```
```// // File: sobelEdgeDetectionAlg.cpp // // MATLAB Coder version : 24.1 // C/C++ source code generated on : 20-Apr-2024 05:56:37 // // Include Files #include "sobelEdgeDetectionAlg.h" #include "conv2MovingWindowSameCM.h" #include "rtwhalf.h" #include "sobelEdgeDetectionAlg_data.h" #include "sobelEdgeDetectionAlg_initialize.h" #include <cmath> // Function Definitions // // Entry-point function for half-precision Sobel edge detection example. // Copyright 2018-2022 The MathWorks, Inc. // // Arguments : const real16_T img[589824] // real16_T thresh // unsigned char edgeImg[196608] // Return Type : void // void sobelEdgeDetectionAlg(const real16_T img[589824], real16_T thresh, unsigned char edgeImg[196608]) { static const real16_T hv[9]{real16_T(1.0F), real16_T(0.0F), real16_T(-1.0F), real16_T(2.0F), real16_T(0.0F), real16_T(-2.0F), real16_T(1.0F), real16_T(0.0F), real16_T(-1.0F)}; static const real16_T hv1[9]{ real16_T(1.0F), real16_T(2.0F), real16_T(1.0F), real16_T(0.0F), real16_T(0.0F), real16_T(0.0F), real16_T(-1.0F), real16_T(-2.0F), real16_T(-1.0F)}; static real16_T h[196608]; static real16_T v[196608]; if (!isInitialized_sobelEdgeDetectionAlg) { sobelEdgeDetectionAlg_initialize(); } // Finding horizontal and vertical gradients. coder::conv2MovingWindowSameCM(&img[196608], hv, h); coder::conv2MovingWindowSameCM(&img[196608], hv1, v); // Finding magnitude of the gradients. // Threshold the edges for (int k{0}; k < 196608; k++) { real16_T b_h; real16_T h1; b_h = h[k]; h1 = v[k]; b_h = static_cast<real16_T>( std::sqrt(static_cast<float>(b_h * b_h + h1 * h1))); h[k] = b_h; edgeImg[k] = static_cast<unsigned char>((b_h > thresh) * 240U); } } // // File trailer for sobelEdgeDetectionAlg.cpp // // [EOF] // ```