IEEE Robotics & Automation Magazine - June 2020 - 156

features, but it is possible to use either the CPU or the GPU for
that purpose with the classes cv::DescriptorMatcher
and cv::cuda::DescriptorMatcher, respectively.
In the third example, we compute the dense optical flow
with the Farneback algorithm [10]. The source code for the
CPU version is as follows:
1
2
3
4
5

cv::Mat src, dst;
cv::Mat prev, cv::Mat next;
cv::Mat flow(prev.size(), CV_32FC2);
cv::cvtColor(src, next, cv::COLOR_BGR2GRAY);
cv::calcOpticalFlowFarneback(prev, next, flow, 0.5, 3, 15, 3,
5, 1.2, 0);

Since optical flow is computed with the difference between
the current and previous frames, we need to define additional variables in line 2
to store the frames. We
also define a matrix of
CUDA and other computing
float numbers f l o w
for the result [in line 3,
frameworks have become
C V _ 3 2 F C 2 means a
programming standards for 2-channel (complex) floating-point array]. This flow
matrix contains the graparallel computing.
dient of the movement
between two frames; for
each pixel location in the
original frame, the channels contain dx and dy , so that
prev_x + dx = next_x, and prev_y + dy = next_y.
The computation steps are quite simple: the color image is
converted into a gray image (line 4), and the optical flow

Figure 6. The output image of the dense optical flow algorithm; the
hue represents the flow angle, and the intensity is proportional to
the flow magnitude.

algorithm is executed (line 5). For the sake of simplicity, we
have omitted additional instructions for displaying the result
and storing the frames.
The GPU version is not very different:
1
2
3
4
5

cv::Mat src, dst, flow;
cv::cuda::GpuMat gpu_src, gpu_flow;
cv::cuda::GpuMat prev, next;
gpu_src.upload(src);
cv::Ptr fof =
cv::cuda::FarnebackOpticalFlow::create();
6 cv::cuda::cvtColor( gpu_src, next, cv::COLOR_BGR2GRAY );
7 fof->calc( prev, next, gpu_flow );
8 gpu_flow.download( flow );

Besides defining all of the intermediate matrices in GPU
memory (lines 2-3), the main difference is in the interface to
the optical flow algorithm. In this version, the algorithm
object is first defined in line 5, and then applied to the frames
in line 7. Finally, the result is downloaded to CPU memory
for visualization.
The output of the optical flow algorithm is displayed in
Figure 6. The hue of each pixel block represents the orientation of the optical flow vector at that point, and the intensity
is proportional to the magnitude of the flow. The results are
shown in Table 4. As in the previous example, the execution
times for the GPUs are lower than for the CPUs, since computing dense optical flow is a demanding operation.
Finally, we test the DNN module for OpenCV. Since version 3.1, there is a DNN module in the library that implements forward pass (inferencing) with networks pretrained
using some popular deep-learning frameworks such as
Caffe [11] or TensorFlow [12]. A backend for CUDA was
added in OpenCV 4.2.0. In this example, we use the YOLO
v3 network [13], a state-of-the-art, real-time object-detection system.
While the details of the OpenCV DNN module are
beyond the scope of this article, its design is based on a
unique interface that runs on different backends and computation devices (CPU, OpenCL, and CUDA). Consequently,
the source code is exactly the same, no matter if the CPU or
GPU is used, except for the parameters that select the appropriate backend and computation target. The values for using
the CPU are as follows:
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV);
net.setPreferableTarget (cv.dnn.DNN_TARGET_CPU);

The GPU can be selected with
Table 4. The computation times (in milliseconds)
for the dense optical flow algorithm (lower, in
bold, is better).
Desktop PC

Laptop PC

Embedded PC

CPU 196.382 ± 0.889 228.838 ± 6.736 983.943 ± 12.776
GPU 33.970 ± 0.231

156

*

78.561 ± 0.498

IEEE ROBOTICS & AUTOMATION MAGAZINE

*

net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA);
net.setPreferableTarget (cv.dnn.DNN_TARGET_CUDA);

686.960 ± 9.729

JUNE 2020

A typical output image from the DNN module is shown in
Figure 7, where several cars are correctly identified in the
input image. The frame rate for the CPU and GPU versions



IEEE Robotics & Automation Magazine - June 2020

Table of Contents for the Digital Edition of IEEE Robotics & Automation Magazine - June 2020

Contents
IEEE Robotics & Automation Magazine - June 2020 - Cover1
IEEE Robotics & Automation Magazine - June 2020 - Cover2
IEEE Robotics & Automation Magazine - June 2020 - Contents
IEEE Robotics & Automation Magazine - June 2020 - 2
IEEE Robotics & Automation Magazine - June 2020 - 3
IEEE Robotics & Automation Magazine - June 2020 - 4
IEEE Robotics & Automation Magazine - June 2020 - 5
IEEE Robotics & Automation Magazine - June 2020 - 6
IEEE Robotics & Automation Magazine - June 2020 - 7
IEEE Robotics & Automation Magazine - June 2020 - 8
IEEE Robotics & Automation Magazine - June 2020 - 9
IEEE Robotics & Automation Magazine - June 2020 - 10
IEEE Robotics & Automation Magazine - June 2020 - 11
IEEE Robotics & Automation Magazine - June 2020 - 12
IEEE Robotics & Automation Magazine - June 2020 - 13
IEEE Robotics & Automation Magazine - June 2020 - 14
IEEE Robotics & Automation Magazine - June 2020 - 15
IEEE Robotics & Automation Magazine - June 2020 - 16
IEEE Robotics & Automation Magazine - June 2020 - 17
IEEE Robotics & Automation Magazine - June 2020 - 18
IEEE Robotics & Automation Magazine - June 2020 - 19
IEEE Robotics & Automation Magazine - June 2020 - 20
IEEE Robotics & Automation Magazine - June 2020 - 21
IEEE Robotics & Automation Magazine - June 2020 - 22
IEEE Robotics & Automation Magazine - June 2020 - 23
IEEE Robotics & Automation Magazine - June 2020 - 24
IEEE Robotics & Automation Magazine - June 2020 - 25
IEEE Robotics & Automation Magazine - June 2020 - 26
IEEE Robotics & Automation Magazine - June 2020 - 27
IEEE Robotics & Automation Magazine - June 2020 - 28
IEEE Robotics & Automation Magazine - June 2020 - 29
IEEE Robotics & Automation Magazine - June 2020 - 30
IEEE Robotics & Automation Magazine - June 2020 - 31
IEEE Robotics & Automation Magazine - June 2020 - 32
IEEE Robotics & Automation Magazine - June 2020 - 33
IEEE Robotics & Automation Magazine - June 2020 - 34
IEEE Robotics & Automation Magazine - June 2020 - 35
IEEE Robotics & Automation Magazine - June 2020 - 36
IEEE Robotics & Automation Magazine - June 2020 - 37
IEEE Robotics & Automation Magazine - June 2020 - 38
IEEE Robotics & Automation Magazine - June 2020 - 39
IEEE Robotics & Automation Magazine - June 2020 - 40
IEEE Robotics & Automation Magazine - June 2020 - 41
IEEE Robotics & Automation Magazine - June 2020 - 42
IEEE Robotics & Automation Magazine - June 2020 - 43
IEEE Robotics & Automation Magazine - June 2020 - 44
IEEE Robotics & Automation Magazine - June 2020 - 45
IEEE Robotics & Automation Magazine - June 2020 - 46
IEEE Robotics & Automation Magazine - June 2020 - 47
IEEE Robotics & Automation Magazine - June 2020 - 48
IEEE Robotics & Automation Magazine - June 2020 - 49
IEEE Robotics & Automation Magazine - June 2020 - 50
IEEE Robotics & Automation Magazine - June 2020 - 51
IEEE Robotics & Automation Magazine - June 2020 - 52
IEEE Robotics & Automation Magazine - June 2020 - 53
IEEE Robotics & Automation Magazine - June 2020 - 54
IEEE Robotics & Automation Magazine - June 2020 - 55
IEEE Robotics & Automation Magazine - June 2020 - 56
IEEE Robotics & Automation Magazine - June 2020 - 57
IEEE Robotics & Automation Magazine - June 2020 - 58
IEEE Robotics & Automation Magazine - June 2020 - 59
IEEE Robotics & Automation Magazine - June 2020 - 60
IEEE Robotics & Automation Magazine - June 2020 - 61
IEEE Robotics & Automation Magazine - June 2020 - 62
IEEE Robotics & Automation Magazine - June 2020 - 63
IEEE Robotics & Automation Magazine - June 2020 - 64
IEEE Robotics & Automation Magazine - June 2020 - 65
IEEE Robotics & Automation Magazine - June 2020 - 66
IEEE Robotics & Automation Magazine - June 2020 - 67
IEEE Robotics & Automation Magazine - June 2020 - 68
IEEE Robotics & Automation Magazine - June 2020 - 69
IEEE Robotics & Automation Magazine - June 2020 - 70
IEEE Robotics & Automation Magazine - June 2020 - 71
IEEE Robotics & Automation Magazine - June 2020 - 72
IEEE Robotics & Automation Magazine - June 2020 - 73
IEEE Robotics & Automation Magazine - June 2020 - 74
IEEE Robotics & Automation Magazine - June 2020 - 75
IEEE Robotics & Automation Magazine - June 2020 - 76
IEEE Robotics & Automation Magazine - June 2020 - 77
IEEE Robotics & Automation Magazine - June 2020 - 78
IEEE Robotics & Automation Magazine - June 2020 - 79
IEEE Robotics & Automation Magazine - June 2020 - 80
IEEE Robotics & Automation Magazine - June 2020 - 81
IEEE Robotics & Automation Magazine - June 2020 - 82
IEEE Robotics & Automation Magazine - June 2020 - 83
IEEE Robotics & Automation Magazine - June 2020 - 84
IEEE Robotics & Automation Magazine - June 2020 - 85
IEEE Robotics & Automation Magazine - June 2020 - 86
IEEE Robotics & Automation Magazine - June 2020 - 87
IEEE Robotics & Automation Magazine - June 2020 - 88
IEEE Robotics & Automation Magazine - June 2020 - 89
IEEE Robotics & Automation Magazine - June 2020 - 90
IEEE Robotics & Automation Magazine - June 2020 - 91
IEEE Robotics & Automation Magazine - June 2020 - 92
IEEE Robotics & Automation Magazine - June 2020 - 93
IEEE Robotics & Automation Magazine - June 2020 - 94
IEEE Robotics & Automation Magazine - June 2020 - 95
IEEE Robotics & Automation Magazine - June 2020 - 96
IEEE Robotics & Automation Magazine - June 2020 - 97
IEEE Robotics & Automation Magazine - June 2020 - 98
IEEE Robotics & Automation Magazine - June 2020 - 99
IEEE Robotics & Automation Magazine - June 2020 - 100
IEEE Robotics & Automation Magazine - June 2020 - 101
IEEE Robotics & Automation Magazine - June 2020 - 102
IEEE Robotics & Automation Magazine - June 2020 - 103
IEEE Robotics & Automation Magazine - June 2020 - 104
IEEE Robotics & Automation Magazine - June 2020 - 105
IEEE Robotics & Automation Magazine - June 2020 - 106
IEEE Robotics & Automation Magazine - June 2020 - 107
IEEE Robotics & Automation Magazine - June 2020 - 108
IEEE Robotics & Automation Magazine - June 2020 - 109
IEEE Robotics & Automation Magazine - June 2020 - 110
IEEE Robotics & Automation Magazine - June 2020 - 111
IEEE Robotics & Automation Magazine - June 2020 - 112
IEEE Robotics & Automation Magazine - June 2020 - 113
IEEE Robotics & Automation Magazine - June 2020 - 114
IEEE Robotics & Automation Magazine - June 2020 - 115
IEEE Robotics & Automation Magazine - June 2020 - 116
IEEE Robotics & Automation Magazine - June 2020 - 117
IEEE Robotics & Automation Magazine - June 2020 - 118
IEEE Robotics & Automation Magazine - June 2020 - 119
IEEE Robotics & Automation Magazine - June 2020 - 120
IEEE Robotics & Automation Magazine - June 2020 - 121
IEEE Robotics & Automation Magazine - June 2020 - 122
IEEE Robotics & Automation Magazine - June 2020 - 123
IEEE Robotics & Automation Magazine - June 2020 - 124
IEEE Robotics & Automation Magazine - June 2020 - 125
IEEE Robotics & Automation Magazine - June 2020 - 126
IEEE Robotics & Automation Magazine - June 2020 - 127
IEEE Robotics & Automation Magazine - June 2020 - 128
IEEE Robotics & Automation Magazine - June 2020 - 129
IEEE Robotics & Automation Magazine - June 2020 - 130
IEEE Robotics & Automation Magazine - June 2020 - 131
IEEE Robotics & Automation Magazine - June 2020 - 132
IEEE Robotics & Automation Magazine - June 2020 - 133
IEEE Robotics & Automation Magazine - June 2020 - 134
IEEE Robotics & Automation Magazine - June 2020 - 135
IEEE Robotics & Automation Magazine - June 2020 - 136
IEEE Robotics & Automation Magazine - June 2020 - 137
IEEE Robotics & Automation Magazine - June 2020 - 138
IEEE Robotics & Automation Magazine - June 2020 - 139
IEEE Robotics & Automation Magazine - June 2020 - 140
IEEE Robotics & Automation Magazine - June 2020 - 141
IEEE Robotics & Automation Magazine - June 2020 - 142
IEEE Robotics & Automation Magazine - June 2020 - 143
IEEE Robotics & Automation Magazine - June 2020 - 144
IEEE Robotics & Automation Magazine - June 2020 - 145
IEEE Robotics & Automation Magazine - June 2020 - 146
IEEE Robotics & Automation Magazine - June 2020 - 147
IEEE Robotics & Automation Magazine - June 2020 - 148
IEEE Robotics & Automation Magazine - June 2020 - 149
IEEE Robotics & Automation Magazine - June 2020 - 150
IEEE Robotics & Automation Magazine - June 2020 - 151
IEEE Robotics & Automation Magazine - June 2020 - 152
IEEE Robotics & Automation Magazine - June 2020 - 153
IEEE Robotics & Automation Magazine - June 2020 - 154
IEEE Robotics & Automation Magazine - June 2020 - 155
IEEE Robotics & Automation Magazine - June 2020 - 156
IEEE Robotics & Automation Magazine - June 2020 - 157
IEEE Robotics & Automation Magazine - June 2020 - 158
IEEE Robotics & Automation Magazine - June 2020 - 159
IEEE Robotics & Automation Magazine - June 2020 - 160
IEEE Robotics & Automation Magazine - June 2020 - 161
IEEE Robotics & Automation Magazine - June 2020 - 162
IEEE Robotics & Automation Magazine - June 2020 - 163
IEEE Robotics & Automation Magazine - June 2020 - 164
IEEE Robotics & Automation Magazine - June 2020 - 165
IEEE Robotics & Automation Magazine - June 2020 - 166
IEEE Robotics & Automation Magazine - June 2020 - 167
IEEE Robotics & Automation Magazine - June 2020 - 168
IEEE Robotics & Automation Magazine - June 2020 - 169
IEEE Robotics & Automation Magazine - June 2020 - 170
IEEE Robotics & Automation Magazine - June 2020 - 171
IEEE Robotics & Automation Magazine - June 2020 - 172
IEEE Robotics & Automation Magazine - June 2020 - 173
IEEE Robotics & Automation Magazine - June 2020 - 174
IEEE Robotics & Automation Magazine - June 2020 - 175
IEEE Robotics & Automation Magazine - June 2020 - 176
IEEE Robotics & Automation Magazine - June 2020 - 177
IEEE Robotics & Automation Magazine - June 2020 - 178
IEEE Robotics & Automation Magazine - June 2020 - 179
IEEE Robotics & Automation Magazine - June 2020 - 180
IEEE Robotics & Automation Magazine - June 2020 - 181
IEEE Robotics & Automation Magazine - June 2020 - 182
IEEE Robotics & Automation Magazine - June 2020 - 183
IEEE Robotics & Automation Magazine - June 2020 - 184
IEEE Robotics & Automation Magazine - June 2020 - Cover3
IEEE Robotics & Automation Magazine - June 2020 - Cover4
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2023
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2023
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2023
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2023
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2022
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2022
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2022
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2022
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2021
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2021
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2021
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2021
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2020
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2020
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2020
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2020
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2019
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2019
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2019
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2019
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2018
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2018
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2018
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2018
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2017
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2017
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2017
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2017
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2016
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2016
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2016
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2016
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2015
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2015
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2015
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2015
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2014
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2014
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2014
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2014
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2013
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2013
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2013
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2013
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2012
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2012
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2012
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2012
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2011
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2011
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_june2011
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_march2011
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_december2010
https://www.nxtbook.com/nxtbooks/ieee/roboticsautomation_september2010
https://www.nxtbookmedia.com