This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Top-Hat Filtering to Remove Uneven Background Illumination on NVIDIA Jetson TX2 Developer Kit

This example shows how to deploy Image Processing Toolbox™ algorithms to NVIDIA® Jetson TX2 board using the GPU Coder™ Support Package for NVIDIA GPUs. The imtophat function that performs morphological top-hat filtering on a grayscale image is used as an example to demonstrate this concept. Top-hat filtering computes the morphological opening of the image (using imopen) and then subtracts the result from the original image. The generated CUDA code uses shared memory to speed up the operations on the GPU.

Prerequisites

Target Board Requirements

  • NVIDIA Jetson Tegra TX2 embedded platform.

  • Ethernet crossover cable to connect the target board and host PC (if the target board cannot be connected to a local network).

  • NVIDIA CUDA toolkit installed on the board.

  • OpenCV 3.0 (or higher) library on the target for reading and displaying images/video.

  • Environment variables on the target for the compilers and libraries. For information on the supported versions of the compilers and libraries and their setup, see Install and Setup Prerequisites for NVIDIA boards.

Development Host Requirements

  • GPU Coder for code generation. For an overview and tutorials, visit the GPU Coder product page.

  • NVIDIA CUDA toolkit on the host.

  • Environment variables on the host for the compilers and libraries. For information on the supported versions of the compilers and libraries, see Third-party Products. For setting up the environment variables, see Environment Variables.

Create a Folder and Copy Relevant Files

The following line of code creates a folder in your current working folder (pwd), and copies all the relevant files into this folder. If you do not want to perform this operation or if you cannot generate files in this folder, change your current working folder.

gpucoderdemo_setup('gpucoderdemo_topHatFilteringOnJetsonTX2');

Verify NVIDIA Support Package Installation on Host

Use the checkHardwareSupportPackageInstall function to verify that the host system is compatible to run this example.

checkHardwareSupportPackageInstall();

Connect to the NVIDIA Hardware

The GPU Coder Support Package for NVIDIA GPUs uses an SSH connection over TCP/IP to execute commands while building and running the generated CUDA code on the Jetson platform. You must therefore connect the target platform to the same network as the host computer or use an Ethernet crossover cable to connect the board directly to the host computer. Refer to the NVIDIA documentation on how to set up and configure your board.

To communicate with the NVIDIA hardware, you must create a live hardware connection object by using the jetson function. You must know the host name or IP address, username, and password of the target board to create a live hardware connection object.

hwobj= jetson('host-name','username','password');

When there are multiple live connection objects for different targets, the code generator performs remote build on the target for which a recent live object was created. To choose a hardware board for performing remote build, use the setupCodegenContext() method of the respective live hardware object. If only one live connection object was created, it is not necessary to call this method.

hwobj.setupCodegenContext;

Verify the GPU Environment

Use the coder.checkGpuInstall function and verify that the compilers and libraries needed for running this example are set up correctly.

envCfg = coder.gpuEnvConfig('jetson');
envCfg.BasicCodegen = 1;
envCfg.Quiet = 1;
envCfg.HardwareObject = hwobj;
coder.checkGpuInstall(envCfg);

About the 'imtophat' Function

The imtophatDemo_gpu calls imtophat internally. The imtophat function performs morphological opening on the image using the imopen function. The result of the image is subtracted from the original image. The imopen operation is basically imerode operation followed by imdilate.

This example is shown on an input grayscale image.

original = imread('rice.png');
imshow(original),title('Input to Top-Hat Filtering');

Create a disc-shaped structuring element with a radius of 12. Neighbourhood, Nhood of this structuring element is passed as an input argument for the imtophat function.

se = strel('disk',12);
Nhood = se.Neighborhood;
type imtophatDemo_gpu
function [out]  = imtophatDemo_gpu(img,Nhood) %#codegen

%   Copyright 2019 The MathWorks, Inc.   

coder.gpu.kernelfun;

% This example uses OpenCV for reading an image 
% and displaying output image. Update buildinfo to link with 
% OpenCV library available on target.
opencv_link_flags = '`pkg-config --cflags --libs opencv`';
coder.updateBuildInfo('addLinkFlags',opencv_link_flags);
coder.updateBuildInfo('addCompileFlags','-std=c++11');

out = imtophat(img,Nhood);

end

Generate & Deploy CUDA Code on the Target

This program uses imtophatDemo_gpu.m, as the entry-point function for code generation. To generate a CUDA executable, create a GPU code configuration object.

cfg = coder.gpuConfig('exe');

Use the coder.hardware function to create a configuration object for the Jetson platform and assign it to the Hardware property of the GPU code configuration object cfg.

cfg.Hardware = coder.hardware('NVIDIA Jetson');

The custom main file is a wrapper that calls the imtophat function in the generated code. Post processing steps are added in the main file using OpenCV interfaces. Build Flags for OpenCV Libraries are included in imtophatDemo_gpu.m function.

cfg.CustomSource = fullfile('main.cpp');

To generate CUDA code, use the codegen function and pass the GPU code configuration object along with input arguments. In this step, CUDA code is generated on the host, generated files are copied over and built on the target in the workspace directory. The workspace directory is available as a property, workspaceDir in the hardware object, hwobj.

codegen -args {original,coder.Constant(Nhood)} -config cfg imtophatDemo_gpu -report

Run the Application on the Target

This application takes a grayscale image as input. Copy the rice.png file from host to the target device by using the putFile command. hwobj.putFile('rice.png',hwobj.workspaceDir);

Use the runApplication method of the hardware object to launch the application on the target hardware.

hwobj.runApplication('imtophatDemo_gpu','rice.png');

Top-Hat Filtered Image on Jetson TX2

Kill the Application

Use the killApplication method of the hardware object to kill the running application on the target.

hwobj.killApplication('imtophatDemo_gpu');

Run Command: Cleanup

Run cleanup function to remove the generated files and return to the original folder.

cleanup