|
VPI - Vision Programming Interface
0.4.4 Release
|
Overview
The 2D Image Convolution application outputs an image with the edges of the input image, saving the result into edges.png. 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.
This sample shows the following:
- Creating and destroying a VPI stream.
- Wrapping an image hosted on CPU (the input) to be used by VPI.
- Creating a VPI-managed 2D image where output will be written to.
- Create a Convolve2D algorithm and call it, passing a custom kernel.
- Simple stream synchronization.
- Image locking to access its contents from CPU side.
- Error handling.
- Environment clean up.
Instructions
The usage is:
./vpi_sample_01_convolve_2d <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:
./vpi_sample_01_convolve_2d cpu ../assets/kodim8.png
This is using the CUDA backend and one of the provided sample images. You can try with other images, respecting the constraints imposed by the algorithm.
Results
Input image | Output image, edges |
| |
Source code
For convenience, here's the code that is also installed in the samples directory.
29 #include <opencv2/core/version.hpp>
30 #if CV_MAJOR_VERSION >= 3
31 # include <opencv2/imgcodecs.hpp>
33 # include <opencv2/highgui/highgui.hpp>
45 #define CHECK_STATUS(STMT) \
48 VPIStatus status = (STMT); \
49 if (status != VPI_SUCCESS) \
51 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
52 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
53 std::ostringstream ss; \
54 ss << vpiStatusGetName(status) << ": " << buffer; \
55 throw std::runtime_error(ss.str()); \
59 int main(
int argc,
char *argv[])
71 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <cpu|pva|cuda> <input image>");
74 std::string strBackend = argv[1];
75 std::string strInputFileName = argv[2];
78 cv::Mat cvImage = cv::imread(strInputFileName, cv::IMREAD_GRAYSCALE);
81 throw std::runtime_error(
"Can't open '" + strInputFileName +
"'");
84 assert(cvImage.type() == CV_8UC1);
89 if (strBackend ==
"cpu")
93 else if (strBackend ==
"cuda")
97 else if (strBackend ==
"pva")
103 throw std::runtime_error(
"Backend '" + strBackend +
104 "' not recognized, it must be either cpu, cuda or pva.");
114 memset(&imgData, 0,
sizeof(imgData));
131 float kernel[3 * 3] = {1, 0, -1, 0, 0, 0, -1, 0, 1};
134 for (
int i = 0; i < 9; ++i)
136 kernel[i] /= 1 + FLT_EPSILON;
153 imwrite(
"edges_" + strBackend +
".png", cvOut);
159 catch (std::exception &e)
161 std::cerr << e.what() << std::endl;
uint32_t height
Height of this plane in pixels.
uint32_t width
Width of this plane in pixels.
VPIStatus vpiSubmitConvolution(VPIStream stream, VPIBackend backend, VPIImage input, VPIImage output, const float *kernelData, uint32_t kernelWidth, uint32_t kernelHeight, VPIBoundaryCond boundary)
Runs a generic 2D convolution over an image.
VPIStatus vpiStreamCreate(uint32_t flags, VPIStream *stream)
Create a stream instance.
VPIBackend
VPI Backend types.
Declares functions to perform image filtering with convolution kernels.
@ VPI_LOCK_READ
Lock memory only for reading.
VPIStatus vpiImageUnlock(VPIImage img)
Releases the lock on an image object.
VPIStatus vpiStreamSync(VPIStream stream)
Blocks the calling thread until all submitted commands in this stream queue are done (queue is empty)...
@ VPI_BACKEND_CUDA
CUDA backend.
Stores information about image characteristics and content.
struct VPIStreamImpl * VPIStream
A handle to a stream.
void vpiStreamDestroy(VPIStream stream)
Destroy a stream instance and deallocate all HW resources.
VPIImagePlane planes[VPI_MAX_PLANE_COUNT]
Data of all image planes.
VPIStatus vpiImageCreate(uint32_t width, uint32_t height, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
Functions and structures for dealing with VPI images.
uint32_t pitchBytes
Difference in bytes of beginning of one row and the beginning of the previous.
struct VPIImageImpl * VPIImage
A handle to an image.
int32_t numPlanes
Number of planes.
@ VPI_BOUNDARY_COND_ZERO
All pixels outside the image are considered to be zero.
VPIStatus vpiImageLock(VPIImage img, VPILockMode mode, VPIImageData *hostData)
Acquires the lock on an image object and returns a pointer to the image planes.
VPIImageFormat type
Image type.
Declaration of VPI status codes handling functions.
@ VPI_BACKEND_CPU
CPU backend.
VPIStatus vpiImageCreateHostMemWrapper(const VPIImageData *hostData, uint32_t flags, VPIImage *img)
Create an image object by wrapping around an existing host memory block.
Declares functions dealing with VPI streams.
void * data
Pointer to the first row of this plane.
@ VPI_BACKEND_PVA
PVA backend.