VPI - Vision Programming Interface

1.2 Release

2D Image Convolution

Overview

The 2D Image Convolution application outputs an image with the edges of the input image, saving the result as an image file on disk. The user can define what backend will be used for processing.

Note
The output will be in grayscale as convolution is currently only supported for single-channel images.

Instructions

The command line parameters are:

<backend> <input image>

where

  • backend: either cpu, cuda or pva; it defines the backend that will perform the processing.
  • input image: input image file name; it accepts png, jpeg and possibly others.

Here's one example:

  • C++
    ./vpi_sample_01_convolve_2d cpu ../assets/kodim8.png
  • Python
    python main.py cpu ../assets/kodim8.png

This is using the CPU backend and one of the provided sample images. You can try with other images, respecting the constraints imposed by the algorithm.

Results

Input imageOutput image, edges

Source Code

For convenience, here's the code that is also installed in the samples directory.

Language:
27 import sys
28 import vpi
29 import numpy as np
30 from PIL import Image
31 from argparse import ArgumentParser
32 
33 # Parse command line arguments
34 parser = ArgumentParser()
35 parser.add_argument('backend', choices=['cpu','cuda','pva'],
36  help='Backend to be used for processing')
37 
38 parser.add_argument('input',
39  help='Image to be used as input')
40 
41 args = parser.parse_args();
42 
43 if args.backend == 'cpu':
44  backend = vpi.Backend.CPU
45 elif args.backend == 'cuda':
46  backend = vpi.Backend.CUDA
47 else:
48  assert args.backend == 'pva'
49  backend = vpi.Backend.PVA
50 
51 # Load input into a vpi.Image
52 input = vpi.asimage(np.asarray(Image.open(args.input)))
53 
54 # Convert it to grayscale
55 input = input.convert(vpi.Format.U8, backend=vpi.Backend.CUDA)
56 
57 # Define a simple edge detection kernel
58 kernel = [[ 1, 0, -1],
59  [ 0, 0, 0],
60  [-1, 0, 1]]
61 
62 # Using the chosen backend,
63 with backend:
64  # Run input through the convolution filter
65  output = input.convolution(kernel, border=vpi.Border.ZERO)
66 
67 # Save result to disk
68 Image.fromarray(output.cpu()).save('edges_python'+str(sys.version_info[0])+'_'+args.backend+'.png')
69 
70 # vim: ts=8:sw=4:sts=4:et:ai
29 #include <opencv2/core/version.hpp>
30 #include <opencv2/imgproc/imgproc.hpp>
31 #if CV_MAJOR_VERSION >= 3
32 # include <opencv2/imgcodecs.hpp>
33 #else
34 # include <opencv2/highgui/highgui.hpp>
35 #endif
36 
37 #include <vpi/OpenCVInterop.hpp>
38 
39 #include <vpi/Image.h>
40 #include <vpi/Status.h>
41 #include <vpi/Stream.h>
43 #include <vpi/algo/Convolution.h>
44 
45 #include <cstring> // for memset
46 #include <iostream>
47 #include <sstream>
48 
49 #define CHECK_STATUS(STMT) \
50  do \
51  { \
52  VPIStatus status = (STMT); \
53  if (status != VPI_SUCCESS) \
54  { \
55  char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
56  vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
57  std::ostringstream ss; \
58  ss << vpiStatusGetName(status) << ": " << buffer; \
59  throw std::runtime_error(ss.str()); \
60  } \
61  } while (0);
62 
63 int main(int argc, char *argv[])
64 {
65  // OpenCV image that will be wrapped by a VPIImage.
66  // Define it here so that it's destroyed *after* wrapper is destroyed
67  cv::Mat cvImage;
68 
69  // VPI objects that will be used
70  VPIImage image = NULL;
71  VPIImage imageBGR = NULL;
72  VPIImage gradient = NULL;
73  VPIStream stream = NULL;
74 
75  int retval = 0;
76 
77  try
78  {
79  if (argc != 3)
80  {
81  throw std::runtime_error(std::string("Usage: ") + argv[0] + " <cpu|pva|cuda> <input image>");
82  }
83 
84  std::string strBackend = argv[1];
85  std::string strInputFileName = argv[2];
86 
87  // Load the input image
88  cvImage = cv::imread(strInputFileName);
89  if (cvImage.empty())
90  {
91  throw std::runtime_error("Can't open '" + strInputFileName + "'");
92  }
93 
94  // We can't use cv::IMREAD_GRAYSCALE when opening the input file because the
95  // color to grayscale conversion used differs between OpenCV-2.4 and OpenCV>=3.0,
96  // yielding different image content.
97 
98  // Now parse the backend
99  VPIBackend backend;
100 
101  if (strBackend == "cpu")
102  {
103  backend = VPI_BACKEND_CPU;
104  }
105  else if (strBackend == "cuda")
106  {
107  backend = VPI_BACKEND_CUDA;
108  }
109  else if (strBackend == "pva")
110  {
111  backend = VPI_BACKEND_PVA;
112  }
113  else
114  {
115  throw std::runtime_error("Backend '" + strBackend +
116  "' not recognized, it must be either cpu, cuda or pva.");
117  }
118 
119  // Create the stream for any backend.
120  CHECK_STATUS(vpiStreamCreate(0, &stream));
121 
122  // We now wrap the loaded image into a VPIImage object to be used by VPI.
123  CHECK_STATUS(vpiImageCreateOpenCVMatWrapper(cvImage, 0, &imageBGR));
124 
125  // Now create the input image as a single unsigned 8-bit channel
126  CHECK_STATUS(vpiImageCreate(cvImage.cols, cvImage.rows, VPI_IMAGE_FORMAT_U8, 0, &image));
127 
128  // Convert the loaded image to grayscale
129  CHECK_STATUS(vpiSubmitConvertImageFormat(stream, VPI_BACKEND_CUDA, imageBGR, image, NULL));
130 
131  // Now create the output image, single unsigned 8-bit channel.
132  CHECK_STATUS(vpiImageCreate(cvImage.cols, cvImage.rows, VPI_IMAGE_FORMAT_U8, 0, &gradient));
133 
134  // Define the convolution filter, a simple edge detector.
135  float kernel[3 * 3] = {1, 0, -1, 0, 0, 0, -1, 0, 1};
136 
137  // Submit it for processing passing the input image the result image that will store the gradient.
138  CHECK_STATUS(vpiSubmitConvolution(stream, backend, image, gradient, kernel, 3, 3, VPI_BORDER_ZERO));
139 
140  // Wait until the algorithm finishes processing
141  CHECK_STATUS(vpiStreamSync(stream));
142 
143  // Now let's retrieve the output image contents and output it to disk
144  {
145  // Lock output image to retrieve its data on cpu memory
146  VPIImageData outData;
147  CHECK_STATUS(vpiImageLock(gradient, VPI_LOCK_READ, &outData));
148 
149  cv::Mat cvOut(outData.planes[0].height, outData.planes[0].width, CV_8UC1, outData.planes[0].data,
150  outData.planes[0].pitchBytes);
151  imwrite("edges_" + strBackend + ".png", cvOut);
152 
153  // Done handling output image, don't forget to unlock it.
154  CHECK_STATUS(vpiImageUnlock(gradient));
155  }
156  }
157  catch (std::exception &e)
158  {
159  std::cerr << e.what() << std::endl;
160  retval = 1;
161  }
162 
163  // Clean up
164 
165  // Make sure stream is synchronized before destroying the objects
166  // that might still be in use.
167  if (stream != NULL)
168  {
169  vpiStreamSync(stream);
170  }
171 
172  vpiImageDestroy(image);
173  vpiImageDestroy(imageBGR);
174  vpiImageDestroy(gradient);
175 
176  vpiStreamDestroy(stream);
177 
178  return retval;
179 }
180 
181 // vim: ts=8:sw=4:sts=4:et:ai
Declares functions that handle image format conversion.
Declares functions to perform image filtering with convolution kernels.
Functions and structures for dealing with VPI images.
Functions for handling OpenCV interoperability with VPI.
Declaration of VPI status codes handling functions.
Declares functions dealing with VPI streams.
VPIStatus vpiSubmitConvertImageFormat(VPIStream stream, uint32_t backend, VPIImage input, VPIImage output, const VPIConvertImageFormatParams *params)
Converts the image contents to the desired format, with optional scaling and offset.
VPIStatus vpiSubmitConvolution(VPIStream stream, uint32_t backend, VPIImage input, VPIImage output, const float *kernelData, int32_t kernelWidth, int32_t kernelHeight, VPIBorderExtension border)
Runs a generic 2D convolution over an image.
@ VPI_IMAGE_FORMAT_U8
Single plane with one 8-bit unsigned integer channel.
Definition: ImageFormat.h:104
int32_t height
Height of this plane in pixels.
Definition: Image.h:138
int32_t width
Width of this plane in pixels.
Definition: Image.h:137
void * data
Pointer to the first row of this plane.
Definition: Image.h:147
int32_t pitchBytes
Difference in bytes of beginning of one row and the beginning of the previous.
Definition: Image.h:139
VPIImagePlane planes[VPI_MAX_PLANE_COUNT]
Data of all image planes.
Definition: Image.h:166
VPIStatus vpiImageLock(VPIImage img, VPILockMode mode, VPIImageData *hostData)
Acquires the lock on an image object and returns a pointer to the image planes.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
struct VPIImageImpl * VPIImage
A handle to an image.
Definition: Types.h:215
VPIStatus vpiImageCreate(int32_t width, int32_t height, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
VPIStatus vpiImageUnlock(VPIImage img)
Releases the lock on an image object.
Stores information about image characteristics and content.
Definition: Image.h:159
VPIStatus vpiImageCreateOpenCVMatWrapper(const cv::Mat &mat, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Wraps a cv::Mat in an VPIImage with the given image format.
struct VPIStreamImpl * VPIStream
A handle to a stream.
Definition: Types.h:209
VPIStatus vpiStreamSync(VPIStream stream)
Blocks the calling thread until all submitted commands in this stream queue are done (queue is empty)...
VPIBackend
VPI Backend types.
Definition: Types.h:91
void vpiStreamDestroy(VPIStream stream)
Destroy a stream instance and deallocate all HW resources.
VPIStatus vpiStreamCreate(uint32_t flags, VPIStream *stream)
Create a stream instance.
@ VPI_BACKEND_CUDA
CUDA backend.
Definition: Types.h:93
@ VPI_BACKEND_PVA
PVA backend.
Definition: Types.h:94
@ VPI_BACKEND_CPU
CPU backend.
Definition: Types.h:92
@ VPI_BORDER_ZERO
All pixels outside the image are considered to be zero.
Definition: Types.h:237
@ VPI_LOCK_READ
Lock memory only for reading.
Definition: Types.h:383