This tutorial provides step-by-step procedures for building an application that demonstrates VPI programming concepts. With this tutorial a simple grayscale image blurring command line application that runs on CUDA is created.
Requirements
Creating the application requires:
- c++ compiler (g++ or clang++ works, testing was performed with version g++-7 and clang++-7)
- cmake >= 3.8, to build the project
- OpenCV >= 3.0, for image Input/Output
On Ubuntu 18.04, install these packages:
apt-get install g++ cmake libopencv-dev
Creating a CMake Project
Create the cmake project to build the application as follows.
1 cmake_minimum_required(VERSION 3.5)
5 # This instructs cmake to look for the most recent
6 # vpi instance installed on the system.
7 find_package(vpi REQUIRED)
8 find_package(OpenCV REQUIRED)
10 # Creates the blur executable target
11 add_executable(vpi_blur main.cpp)
13 # It uses vpi and opencv. CMake will automatically
14 # set up the correct header and library directories,
15 # and make hello_work link to these libraries.
16 target_link_libraries(vpi_blur vpi opencv_core)
18 # OpenCV < 3 uses a different library for image i/o
19 if(OpenCV_VERSION VERSION_LESS 3)
20 target_link_libraries(vpi_blur opencv_highgui)
22 target_link_libraries(vpi_blur opencv_imgcodecs)
Note that cmake automatically sets up the VPI header and library paths for the executable. It is not necessary to manually define them.
Writing an Image Blurring Application
Write the C++ code that gets one image file from command line, blurs it, and writes the results back to disk.
Copy the provided code to a file named main.cpp. For simplicity, error checking is purposely left out. Consult the 2D convolution sample for a complete application with proper error handling.
30 #include <opencv2/core/version.hpp>
31 #if CV_MAJOR_VERSION >= 3
32 # include <opencv2/imgcodecs.hpp>
34 # include <opencv2/highgui/highgui.hpp>
46 int main(
int argc,
char *argv[])
50 std::cerr <<
"Must pass an input image to be blurred" << std::endl;
57 cv::Mat cvImage = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
58 if (cvImage.data == NULL)
60 std::cerr <<
"Can't open input image" << std::endl;
131 imwrite(
"tutorial_blurred.png", cvOut);
Building and Testing the Application
With everything set in place, execute:
The executable vpi_blur is created in the same directory.
To run the application, execute:
./vpi_blur <image file name>
substituting <image file name> by some image on disk.
A sample input image and the resulting blurred image is displayed. Note that although the input in in color, OpenCV converts the to grayscale prior passing it to VPI which results in grayscale output.
Input image | Output image, blurred |
| |