主要内容

为使用半精密数据类型的Sobel边缘检测生成代码

此示例显示了如何通过使用半精度浮点数来生成来自MATLAB®功能的独立C ++库,该功能使用半精度浮点数来执行图像的Sobel边缘检测。Sobel Edge算法接受表示为矩阵的图像,并返回强调对应于其边缘的高空间频率区域的图像。此示例还显示了如何通过使用MEX函数来测试Genenrated代码。

Sobel边缘检测算法

在Sobel边缘检测算法中,对灰度图像执行2-D空间梯度操作。该操作强调了对应于图像中的边缘的高空间频率区域。

类型sobeledgedetectionalg.
功能edrimimg = sobelededetectiong(img,thresh)%#codegen%sobelededetection示例matlab函数用于边缘检测。%Copyright 2018 MathWorks,Inc。kern =一半([1 2 1; 0 0 0; -1 -2 -1]);%找到水平和垂直梯度。h = conv2(img(:,:,2),kern,'sider');v = conv2(img(:,:,2),kern','sider');%找到梯度的大小。e = sqrt(h。* h + v。* v);%阈值边缘边缘= uint8((e> reshet)* 240);结尾

Sobel Edge算法计算水平梯度H和垂直梯度V.使用两个正交滤波器内核的输入图像maskx.掩盖。在过滤操作之后,该算法计算梯度幅度并应用箭头,以找到对应于边缘的图像的区域。

读取图像并将数据包到RGBA包装列主要订单

使用Imread.读取图像的功能。Imread.表示具有整数的图像的RGB通道,每个像素一个。整数范围为0到255.只需将输入铸造到半型可能导致卷曲期间的溢出。为避免此问题,将图像缩放到0到1之间的值。

Im = imread('peppers.png');数字();图像(IM);触发=半(IM)/ 255;阈值=半(100)/ 255;

生成mex.

为此生成C ++ MEX功能sobeledgedetectionalg.使用使用的功能Codegen.命令。

cfg = coder.config('mex');cfg.targetlang ='c ++';cfg.generateReport = true;Codegen.-Config.CFG.-  args.{闪存,阈值}sobeledgedetectionalg.
代码生成成功:要查看报告,请打开('codegen / mex / sobelededetectionalg / html / eport.mldatx')。

运行生成的mex和显示检测到的边缘

在生成C ++代码之前,必须首先测试MATLAB环境中的MEX函数,以确保它在功能上等同于原始MATLAB代码,并且不会发生运行时错误。默认,Codegen.生成一个名为mex函数sobeledgedetectionalg_mex.在当前文件夹中。这允许您测试MATLAB代码和MEX函数并比较结果。

OUT_DISP = SOBELEDGEDETECTIONAG_MEX(刷新,阈值);数字();ImageC(OUT_DISP);

生成静态C ++库

使用Codegen.命令生成C ++静态库。默认情况下,生成的库位于文件夹中codegen / lib / sobeledgedetectional /

cfg = coder.config('lib');cfg.targetlang ='c ++';cfg.generateReport = true;Codegen.-Config.CFG.-  args.{闪存,阈值}sobeledgedetectionalg.;
代码生成成功:要查看报告,请打开('codegen / lib / sobelededetectionalg / html / export.mldatx')。

检查生成的函数

类型Codegen / lib / sobeledgedetectionalg / sobeledgedetectionalg.cpp
// // file:sobelededetectionalg.cpp // // matlab编码器版本:5.2 // c / c ++源代码在:23-feb-2021 13:32:47 // //包括文件#include“sobeledgedetectionalg.h“#include”conv2movingwindowsamecm.h“#include”rtwhalf.h“#include”sobeledgedetectionalg_data.h“#include”sobeledgedetectionalg_initialize.h“#include  //函数定义// // sobelededetection示例MATLAB函数用于边缘检测MATLAB函数MATLAB函数MATLAB函数用于边缘检测。//版权所有2018 MathWorks,Inc. //////////////////////////ime16_t ince [589824] // real16_t thresh // unsigned char edgeimg [196608] //返回类型:void // void sobelededetectionalg(const real16_t img [589824],Real16_t resh,unsigned char editimg [196608]){静态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)};静态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(-1.0f),Real16_t(-2.0f),Real16_t(-1.0f)};静态Real16_T H [196608];静态Real16_t v [196608];if(!isinitialized_sobeledgedetectiong){sobeledgedetectionalg_initialize();} //查找水平和垂直渐变。编码器:: conv2movingwindowsamecm(*(real16_t(*)[196608])和IMG [196608],HV,H);编码器:: conv2movingwindowsamecm(*(real16_t(*)[196608])和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( std::sqrt(static_cast(b_h * b_h + h1 * h1))); h[k] = b_h; edgeImg[k] = static_cast((b_h > thresh) * 240U); } } // // File trailer for sobelEdgeDetectionAlg.cpp // // [EOF] //

也可以看看

||

相关话题