Documentation

使用覆盆子PI相机模块V2对NVIDIA Jetson Nano的Sobel边缘检测

This example shows you how to capture and process images from a Raspberry Pi Camera Module V2 connected to the NVIDIA® Jetson Nano using theGPU Coder™支金宝app持NVIDIA GPU的支持包。The GPU Coder Support Package for NVIDIA GPUs allows you to capture images from the Camera Module V2 and bring them right into the MATLAB® environment for processing. In this example you will learn how to develop a Sobel edge detection algorithm by using this capability.

Prerequisites

Target Board Requirements

  • NVIDIA Jetson Nano embedded platform.

  • 覆盆子PI相机模块V2连接到目标的CSI主机端口。

  • 以太网交叉电缆连接目标板和主机PC(如果目标板不能连接到本地网络)。

  • V4L2 library on the target.

  • GStreamer 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安装和设置NVIDIA板的先决条件

Development Host Requirements

Create a Hardware Object

连接到NVIDIA Jetson Nano

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 DRIVE or Jetson platforms. 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 Jetson Nano, you must create a live hardware connection object by using thedrive要么jetsonfunction. 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, use the following command to create live object for Jetson hardware,

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

Run thegetCameraListfunction of thehwobj要找到可用的摄像机的对象。如果此函数输出空表,则尝试重新连接相机并再次执行函数。

验证GPU环境

Use theCoder.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);

创建相机对象

使用来自的名称创建相机对象getCameraListfunction.

camObj =相机(hwobj,“vi-opport,imx219 6-0010”,[640 480]);

camObjis a handle to a camera object. To display the images captured from Camera Module V2 in MATLAB, use the following commands.

fori = 1:100 img = snapshot(camObj); imagesc(img); drawnow;end

This camera object captures RGB and 3-channel gray scale images.

创建显示对象

Use theimageageSplay.函数创建显示对象。这是一个使用的系统对象imshow.function to display the images in MATLAB.

dispObj = imageDisplay(hwobj); img = snapshot(camObj); image(dispObj,img);

Sobel Edge Detection Algorithm

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

Calculate the Gradients

我们将找到具有相应Sobel内核的输入图像的水平梯度(h)和垂直梯度(v)。这两个Sobel内核彼此正交。我们将确保我们的算法在继续前进数据之前在测试图像上工作。

kern = [1 2 1;0 0 0;-1 -2 -1];img = imread('peppers.png');ImageC(IMG);H = CONV2(IMG(:,:,2),kern,'相同的');v = conv2(img(:,:,2),kern','相同的');

Calculate the Gradient Magnitude

Next we find the gradient magnitude from the horizontal and vertical gradients (h and v).

e = sqrt(h。* h + v。* v);

阈值边缘图像

We threshold the image to find the regions of image that we consider to be edges.

edgeimg = uint8((e> 100)* 240);ImageC(edageimg);

在实时数据上运行Sobel边缘检测算法

我们可以创建一个MATLAB功能,sobelEdgeDetectionAlg.m, out of the MATLAB code we developed in the previous sections of this example. View the MATLAB function in the editor.

编辑('sobeledgedetectionalg.m');

功能sobelEdgeDetectionAlg()takes image and threshold input for edge detection and returns the results of edge detection algorithm. We will call this function on the images captured in a loop. The threshold variable阈值can be varied to get a proper edge image. This way we can use the camera access capability of the support package to tune the algorithm suitable for the specified camera.

fori = 1:200 img = snapshot(camObj); thresh = 100; edgeImage = sobelEdgeDetectionAlg(img, thresh); image(dispObj,edgeImage);end

要将上面的示例部署为目标上的独立应用程序,请按照示例Deploy and Run Sobel Edge Detection with I/O on NVIDIA Jetson Nano

close全部

概括

该示例介绍了一个应用程序,其中使用Sobel边缘检测算法在Matlab中处理来自连接到NVIDIA Jetson Nano的摄像机的图像。