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.

Deploy and Run Sobel Edge Detection with I/O on NVIDIA Jetson

This example shows you how to deploy Sobel edge detection that uses webcam and display on the NVIDIA® Hardware using the GPU Coder™ Support Package for NVIDIA® GPUs. In the example Sobel Edge Detection using Webcam on NVIDIA Hardware, we have seen accessing the webcam on NVIDIA hardware using the GPU Coder Support Package for NVIDIA GPUs. In the current example, we will generate code for accessing I/O (webcam and display) to deploy on the NVIDIA hardware. We will consider the same Sobel edge algorithm for showing this capability.


Target Board Requirements

  • NVIDIA Jetson TX1/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).

  • V4L2 and SDL (v1.2) libraries on the target.

  • 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 installing and setting up prerequisites for NVIDIA boards.

  • USB webcam connected to the USB host port of the target.

  • A monitor connected to the display port of the target.

Development Host Requirements

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. For example,

hwobj = jetson('jetson-tx2-name','ubuntu','ubuntu');

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;

Prepare Sobel Edge Detection Application for Stand-Alone Deployment

Include the webcam and display interfaces inside the Sobel edge detection application

hwobj = jetson;
w = webcam(hwobj);
d = imageDisplay(hwobj);

Sobel Edge MATLAB Application for Code Generation

type sobel_edge_detection_deploy/sobelEdgeDetection.m
function sobelEdgeDetection() %#codegen
% Copyright 2018 The MathWorks, Inc.

hwobj = jetson; % To redirect to the code generatable functions.
w = webcam(hwobj,1);
d = imageDisplay(hwobj);

% Main loop
kern = [1 2 1; 0 0 0; -1 -2 -1];

for k = 1:1800
    % Capture the image from the webcam on hardware.
    img = snapshot(w);
    % 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 > 100) * 240);
    % Display image.


Copy Relevant files to the Current Directory

The following line of code creates a folder in your current working directory (host), and copies all the relevant files into this folder.if you cannot generate files in this folder, change your current working directory before running this command.


Generate CUDA Code for the Target Using GPU Coder

To generate a CUDA executable that can be deployed on to a NVIDIA target, create a GPU code configuration object for generating an executable.

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 code configuration object cfg.

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

Use the BuildDir property to specify the directory for performing remote build process on the target. If the specified build directory does not exist on the target then the software creates a directory with the given name. If no value is assigned to cfg.Hardware.BuildDir, the remote build process happens in the last specified build directory. If there is no stored build directory value, the build process takes place in the home directory.

cfg.Hardware.BuildDir = '~/remoteBuildDir';

Set the GenerateExampleMain property to generate an example C++ main function and compile it. This example does not require modifications to the generated main files.

cfg.GenerateExampleMain = 'GenerateCodeAndCompile'

To generate CUDA code, use the codegen function and pass the GPU code configuration along with sobelEdgeDetection entry-point function. After the code generation takes place on the host, the generated files are copied over and built on the target.

codegen('-config ',cfg,'sobelEdgeDetection','-report');

Run the Sobel Edge Detection on the Target

Run the generated executable on the target.

pid = hwobj.runApplication('sobelEdgeDetection');

A window opens on the target hardware display showing the Sobel edge detection output of the live webcam feed.


Remove the files and return to the original folder.



This example introduced an application where Sobel edge detection application is running on the NVIDIA hardware on the live webcam feed and displaying the output on the native display.