Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Hands-On GPU-Accelerated Computer Vision with OpenCV and CUDA
Hands-On GPU-Accelerated Computer Vision with OpenCV and CUDA

Hands-On GPU-Accelerated Computer Vision with OpenCV and CUDA: Effective techniques for processing complex image data in real time using GPUs

eBook
Can$38.99 Can$55.99
Paperback
Can$69.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Hands-On GPU-Accelerated Computer Vision with OpenCV and CUDA

Parallel Programming using CUDA C

In the last chapter, we saw how easy it is to install CUDA and write a program using it. Though the example was not impressive, it was shown to convince you that it is very easy to get started with CUDA. In this chapter, we will build upon this concept. It teaches you to write advance programs using CUDA for GPUs in detail. It starts with a variable addition program and then incrementally builds towards complex vector manipulation examples in CUDA C. It also covers how the kernel works and how to use device properties in CUDA programs. The chapter discusses how vectors are operated upon in CUDA programs and how CUDA can accelerate vector operations compared to CPU processing. It also discusses terminologies associated with CUDA programming.

The following topics will be covered in this chapter:

  • The concept of the kernel call
  • Creating kernel functions...

Technical requirements

CUDA program structure

We have seen a very simple Hello, CUDA! program earlier, that showcased some important concepts related to CUDA programs. A CUDA program is a combination of functions that are executed either on the host or on the GPU device. The functions that do not exhibit parallelism are executed on the CPU, and the functions that exhibit data parallelism are executed on the GPU. The GPU compiler segregates these functions during compilation. As seen in the previous chapter, functions meant for execution on the device are defined using the __global__ keyword and compiled by the NVCC compiler, while normal C host code is compiled by the C compiler. A CUDA code is basically the same ANSI C code with the addition of some keywords needed for exploiting data parallelism.

So, in this section, a simple two-variable addition program is taken to explain important concepts related...

Executing threads on a device

We have seen that, while configuring kernel parameters, we can start multiple blocks and multiple threads in parallel. So, in which order do these blocks and threads start and finish their execution? It is important to know this if we want to use the output of one thread in other threads. To understand this, we have modified the kernel in the hello,CUDA! program we saw in the first chapter, by including a print statement in the kernel call, which prints the block number. The modified code is as follows:

#include <iostream>
#include <stdio.h>
__global__ void myfirstkernel(void)
{
//blockIdx.x gives the block number of current kernel
printf("Hello!!!I'm thread in block: %d\n", blockIdx.x);
}
int main(void)
{
//A kernel call with 16 blocks and 1 thread per block
myfirstkernel << <16,1>> >();

//Function...

Accessing GPU device properties from CUDA programs

CUDA provides a simple interface to find the information such as determining which CUDA-enabled GPU devices (if any) are present and what capabilities each device supports. First, it is important to get a count of how many CUDA-enabled devices are present on the system, as a system may contain more than one GPU-enabled device. This count can be determined by the CUDA API cudaGetDeviceCount(). The program for getting a number of CUDA enabled devices on the system is shown here:

#include <memory>
#include <iostream>
#include <cuda_runtime.h>
// Main Program
int main(void)
{
int device_Count = 0;
cudaGetDeviceCount(&device_Count);
// This function returns count of number of CUDA enable devices and 0 if there are no CUDA capable devices.
if (device_Count == 0)
{
printf("There are no available device...

Vector operations in CUDA

Until now, the programs that we have seen were not leveraging any advantages of the parallel-processing capabilities of GPU devices. They were just written to get you familiar with the programming concepts in CUDA. From this section, we will start utilizing the parallel-processing capabilities of the GPU by performing vector or array operations on it.

Two-vector addition program

To understand vector operation on the GPU, we will start by writing a vector addition program on the CPU and then modify it to utilize the parallel structure of GPU. We will take two arrays of some numbers and store the answer of element-wise addition in the third array. The vector addition function on CPU is shown here...

Parallel communication patterns

When several thread is executed in parallel, they follow a certain communication pattern that indicates where it is taking inputs and where it is writing its output in memory. We will discuss each communication pattern one by one. It will help you to identify communication patterns related to your application and how to write code for that.

Map

In this communication pattern, each thread or task takes a single input and produces a single output. Basically, it is a one-to-one operation. The vector addition program and element-wise squaring program, seen in the previous sections, are examples of the map pattern. The code of the map pattern will look as follows:

d_out[i] = d_in[i] * 2
...

Summary

To summarize, in this chapter, you were introduced to programming concepts in CUDA C and how parallel computing can be done using CUDA. It was shown that CUDA programs can run on any NVIDIA GPU hardware efficiently and in parallel. So, CUDA is both efficient and scalable. The CUDA API functions over and above existing ANSI C functions needed for parallel data computations were discussed in detail. How to call device code from the host code via a kernel call, configuring of kernel parameters, and a passing of parameters to the kernel were also discussed by taking a simple two-variable addition example. It was also shown that CUDA does not guarantee the order in which the blocks or thread will run and which block is assigned to which multi-processor in hardware. Moreover, vector operations, which take advantage of parallel-processing capabilities of GPU and CUDA, were discussed...

Questions

  1. Write a CUDA program to subtract two numbers. Pass parameters by value in the kernel function.
  2. Write a CUDA program to multiply two numbers. Pass parameters by reference in the kernel function.
  3. Suppose you want to launch 5,000 threads in parallel. Configure kernel parameters in three different ways to accomplish this. Maximum 512 threads are possible per block.
  4. True or false: The programmer can decide in which order blocks will execute on the device, and blocks will be assigned to which streaming multiprocessor?
  5. Write a CUDA program to find out that your system contains a GPU device that has a major-minor version of 5.0 or greater.
  1. Write a CUDA program to find a cube of a vector that contains numbers from 0 to 49.
  2. For the following applications, which communication pattern is useful?
    1. Image processing
    2. Moving average
    3. Sorting array in ascending order
    4. Finding cube of...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Explore examples to leverage the GPU processing power with OpenCV and CUDA
  • Enhance the performance of algorithms on embedded hardware platforms
  • Discover C++ and Python libraries for GPU acceleration

Description

Computer vision has been revolutionizing a wide range of industries, and OpenCV is the most widely chosen tool for computer vision with its ability to work in multiple programming languages. Nowadays, in computer vision, there is a need to process large images in real time, which is difficult to handle for OpenCV on its own. This is where CUDA comes into the picture, allowing OpenCV to leverage powerful NVDIA GPUs. This book provides a detailed overview of integrating OpenCV with CUDA for practical applications. To start with, you’ll understand GPU programming with CUDA, an essential aspect for computer vision developers who have never worked with GPUs. You’ll then move on to exploring OpenCV acceleration with GPUs and CUDA by walking through some practical examples. Once you have got to grips with the core concepts, you’ll familiarize yourself with deploying OpenCV applications on NVIDIA Jetson TX1, which is popular for computer vision and deep learning applications. The last chapters of the book explain PyCUDA, a Python library that leverages the power of CUDA and GPUs for accelerations and can be used by computer vision developers who use OpenCV with Python. By the end of this book, you’ll have enhanced computer vision applications with the help of this book's hands-on approach.

Who is this book for?

This book is a go-to guide for you if you are a developer working with OpenCV and want to learn how to process more complex image data by exploiting GPU processing. A thorough understanding of computer vision concepts and programming languages such as C++ or Python is expected.

What you will learn

  • Understand how to access GPU device properties and capabilities from CUDA programs
  • Learn how to accelerate searching and sorting algorithms
  • Detect shapes such as lines and circles in images
  • Explore object tracking and detection with algorithms
  • Process videos using different video analysis techniques in Jetson TX1
  • Access GPU device properties from the PyCUDA program
  • Understand how kernel execution works

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 26, 2018
Length: 380 pages
Edition : 1st
Language : English
ISBN-13 : 9781789348293
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Sep 26, 2018
Length: 380 pages
Edition : 1st
Language : English
ISBN-13 : 9781789348293
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Can$6 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Can$6 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Can$ 193.97
Hands-On GPU Programming with Python and CUDA
Can$61.99
Hands-On GPU-Accelerated Computer Vision with OpenCV and CUDA
Can$69.99
OpenCV 3 Computer Vision with Python Cookbook
Can$61.99
Total Can$ 193.97 Stars icon
Banner background image

Table of Contents

14 Chapters
Introducing CUDA and Getting Started with CUDA Chevron down icon Chevron up icon
Parallel Programming using CUDA C Chevron down icon Chevron up icon
Threads, Synchronization, and Memory Chevron down icon Chevron up icon
Advanced Concepts in CUDA Chevron down icon Chevron up icon
Getting Started with OpenCV with CUDA Support Chevron down icon Chevron up icon
Basic Computer Vision Operations Using OpenCV and CUDA Chevron down icon Chevron up icon
Object Detection and Tracking Using OpenCV and CUDA Chevron down icon Chevron up icon
Introduction to the Jetson TX1 Development Board and Installing OpenCV on Jetson TX1 Chevron down icon Chevron up icon
Deploying Computer Vision Applications on Jetson TX1 Chevron down icon Chevron up icon
Getting Started with PyCUDA Chevron down icon Chevron up icon
Working with PyCUDA Chevron down icon Chevron up icon
Basic Computer Vision Applications Using PyCUDA Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(5 Ratings)
5 star 60%
4 star 20%
3 star 20%
2 star 0%
1 star 0%
Eduardo Hiroshi Nakamura Nov 05, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Conteúdo condiz com o titulo.
Amazon Verified review Amazon
syu Nov 13, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
この本は、CUDA,OpenCV,Jetson,PyCUDAについて解説されていますが、Jetsonは使用予定がないので評価していません。環境構築、基本的な仕組み・使い方までわかりやすく解説されています。CUDAについては比較的新しい情報が記載されているので、C/C++の知識があることが前提ですがCUDAの基礎知識を得るのには適していると思います。CUDAを使用すれば必ず高速化するわけではないので、特にカーネルパラメータの設定、スレッド、メモリ特性、CUDAストリームの内容はパフォーマンスを向上させるのに役に立ちます。より最新または高度な情報が知りたいときはNVIDIAが無料で公開しているドキュメントを参考にする必要がありますが、基礎知識があれば時間の節約になります。OpenCVについてはいくつかの関数が紹介されていますが、使い方のパターンがあるので似たようなものは簡単に試すことができます。画像処理の効果については主要なものは解説されています。それ以外は実験が必要です。OpenCVでCUDAを使用する場合は、OpenCVに定義されている場合はそれを使用し、そうでない場合はCUDAで自作するとよいでしょう。ちなみに、CUDA10.1+OpenCV4.4(VS2019でビルド)+GeForce RTX 2070の環境でサンプルコードが動作することは確認できました。紙面のサンプルコードは後半になるほど雑になってるので、注意が必要です。PyCUDAについてはサンプル数は少ないけれど、前半で紹介したようなことがPythonでもできることが解説されています。(C/C++を使用しない人はこの本はお勧めしません。)試しに機械学習で使用しているPython3.7の環境にインストールしてみたが、動作しなかった!!もしかしたらダウングレードが必要かもしれないが、実際に使うときに調べようと思う。
Amazon Verified review Amazon
Robin T. Wernick Feb 28, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I was looking for just this kind of concise introduction to Image analysis on several target areas. The documentation for combining these two technologies is sparse to say the least and this book not only had a precise introduction but also several detailed examples.I now know how to proceed with the object recognition that I was looking to apply to silicon disk defect analysis, but I also know how to speed it up by several hundred percent. I have several department managers in mind that I would love to tantalize with this information.This book should make it easier to make better technology for Computer Vision applications an I wish all the readers more success by reading it.
Amazon Verified review Amazon
Force Commander Oct 24, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The book delivers a detailed introduction to CUDA and OpenCV on the Jetson Tx1 that is also applicable to other Nvidia GPUs.
Amazon Verified review Amazon
Amazon Customer Dec 21, 2018
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
The book is a introduction to CUDA programming. The most important concepts are explained but not in detail. Performance optimization of CUDA programs are only briefly explained.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.