主要内容

雾整改

这个例子显示了GPU的使用图像处理函数代码生成。示例将模糊图像作为输入,并生成一个除雾的形象。这个例子是一个典型的雾矫正算法的实现。本例使用conv2 im2gray, imhist功能。

第三方的先决条件

要求

这个示例中生成CUDA®墨西哥人,以下第三方的要求。

  • CUDA启用NVIDIA GPU®和兼容的驱动程序。half-precision代码生成,GPU计算能力必须有一个最低为6.0。

可选

等non-MEX构建静态、动态库或可执行文件,这个例子有以下额外的需求。

验证GPU环境

验证所需的编译器和库运行这个示例设置正确,使用coder.checkGpuInstall函数。

envCfg = coder.gpuEnvConfig (“主机”);envCfg。BasicCodegen = 1;envCfg。安静= 1;coder.checkGpuInstall (envCfg);

fog_rectification入口点函数

fog_rectification.m入口点函数接受一个雾蒙蒙的图像作为输入,并返回一个除雾的形象。

类型fog_rectification
函数[出]= fog_rectification(输入)% # codegen % 2017 - 2019版权MathWorks, inc . coder.gpu.kernelfun;% restoreOut用于存储的输出恢复restoreOut = 0(大小(输入),“双”);%改变输入图像的精度水平双输入=(输入)的两倍。/ 255;% %黑暗信道估计从输入darkChannel = min(输入,[],3);% diff_im作为输入和输出变量用于各向异性扩散diff_im = 0.9 * darkChannel;num_iter = 3;% 2 d卷积面具各向异性扩散hN = (0.0625 0.1250 0.0625;0.1250 0.2500 0.1250;0.0625 0.1250 0.0625);hN =双(hN); %% Refine dark channel using Anisotropic diffusion. for t = 1:num_iter diff_im = conv2(diff_im,hN,'same'); end %% Reduction with min diff_im = min(darkChannel,diff_im); diff_im = 0.6*diff_im ; %% Parallel element-wise math to compute % Restoration with inverse Koschmieder's law factor = 1.0./(1.0-(diff_im)); restoreOut(:,:,1) = (input(:,:,1)-diff_im).*factor; restoreOut(:,:,2) = (input(:,:,2)-diff_im).*factor; restoreOut(:,:,3) = (input(:,:,3)-diff_im).*factor; restoreOut = uint8(255.*restoreOut); restoreOut = uint8(restoreOut); %% % Stretching performs the histogram stretching of the image. % im is the input color image and p is cdf limit. % out is the contrast stretched image and cdf is the cumulative prob. % density function and T is the stretching function. p = 5; % RGB to grayscale conversion im_gray = im2gray(restoreOut); [row,col] = size(im_gray); % histogram calculation [count,~] = imhist(im_gray); prob = count'/(row*col); % cumulative Sum calculation cdf = cumsum(prob(:)); % finding less than particular probability i1 = length(find(cdf <= (p/100))); i2 = 255-length(find(cdf >= 1-(p/100))); o1 = floor(255*.10); o2 = floor(255*.90); t1 = (o1/i1)*[0:i1]; t2 = (((o2-o1)/(i2-i1))*[i1+1:i2])-(((o2-o1)/(i2-i1))*i1)+o1; t3 = (((255-o2)/(255-i2))*[i2+1:255])-(((255-o2)/(255-i2))*i2)+o2; T = (floor([t1 t2 t3])); restoreOut(restoreOut == 0) = 1; u1 = (restoreOut(:,:,1)); u2 = (restoreOut(:,:,2)); u3 = (restoreOut(:,:,3)); % Replacing the value from look up table out1 = T(u1); out2 = T(u2); out3 = T(u3); out = zeros([size(out1),3], 'uint8'); out(:,:,1) = uint8(out1); out(:,:,2) = uint8(out2); out(:,:,3) = uint8(out3); return

生成CUDA代码和墨西哥人功能

设置输入代码生成和GPU创建一个配置代码生成。

inputImage = imread (“foggyInput.png”);cfg = coder.gpuConfig (墨西哥人的);

运行代码生成

生成fog_rectification_mex墨西哥人文件使用codegen命令。

codegenarg游戏{inputImage}配置cfgfog_rectification
代码生成成功:查看报告

运行墨西哥人函数与模糊图像

运行生成的fog_rectification_mex多雾的输入图像,然后画出雾蒙蒙的除雾图像。

[outputImage] = fog_rectification_mex (inputImage);%绘制图片p1 =情节(1、2、1);p2 =情节(1、2、2);imshow (inputImage“父”,p1);imshow (outputImage“父”,p2);标题(p1,“模模糊糊”输入图像);标题(p2,“就输出图像澄清”);

因为CPU和GPU之间的结构差异,数值验证并不总是匹配。这个场景是真的当使用单一数据类型或在MATLAB代码在执行整数类型转换。在这个例子中,整数类型转换fog_rectification.m用MATLAB仿真程序入口点函数产生数值差异了。

Half-Precision

计算在这个例子也可以在half-precision浮点数,完成使用fog_rectification_half_precision.m入口点函数。与half-precision数据类型生成和执行代码,CUDA 6.0或更高的计算能力是必需的。设置ComputeCapability代码配置对象的属性“6.0”。half-precision,内存分配(malloc)模式生成CUDA代码必须设置为“离散”。

inputImageHalf = (imread一半(“foggyInput.png”));cfg = coder.gpuConfig (墨西哥人的);cfg.GpuConfig。ComputeCapability =“6.0”;cfg.GpuConfig。MallocMode =“离散”;codegenarg游戏{inputImageHalf}配置cfgfog_rectification_half_precision
代码生成成功:查看报告

运行Half-Precision墨西哥人函数与模糊图像

运行生成的fog_rectification_half_precision_mex多雾的输入图像,然后画出雾蒙蒙的除雾图像。

[outputImageHalf] = fog_rectification_half_precision_mex (inputImageHalf);%绘制图片p1 =情节(1、2、1);p2 =情节(1、2、2);imshow (inputImage“父”,p1);imshow (outputImageHalf“父”,p2);标题(p1,“模模糊糊”输入图像);标题(p2,“除雾输出图像(一半));

另请参阅

功能

对象

相关的话题