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 VPI features:
- Creating and destroying a VPI device.
- 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 device 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.
Source code
For convenience, here's the code that is also installed in the samples directory.
#include <cstring>
#include <iostream>
#include <opencv2/imgcodecs.hpp>
#define CHECK_STATUS(STMT) \
do \
{ \
VPIStatus status = (STMT); \
if (status != VPI_SUCCESS) \
{ \
throw std::runtime_error(vpiStatusGetName(status)); \
} \
} while (0);
int main(int argc, char *argv[])
{
int retval = 0;
try
{
if (argc != 3)
{
throw std::runtime_error(std::string("Usage: ") + argv[0] + " <cpu|pva|cuda> <input image>");
}
std::string strDevType = argv[1];
std::string strInputFileName = argv[2];
cv::Mat cvImage = cv::imread(strInputFileName, cv::IMREAD_GRAYSCALE);
if (cvImage.empty())
{
throw std::runtime_error("Can't open '" + strInputFileName + "'");
}
assert(cvImage.type() == CV_8UC1);
if(strDevType == "cpu")
{
}
else if(strDevType == "cuda")
{
}
else if(strDevType == "pva")
{
}
else
{
throw std::runtime_error("Backend '" + strDevType + "' not recognized, it must be either cpu, cuda or pva.");
}
{
memset(&imgData, 0, sizeof(imgData));
}
float kernel[3 * 3] = {1,0,-1,
0,0,0,
-1,0,1};
for (int i = 0; i < 9; ++i)
{
kernel[i] /= 1+FLT_EPSILON;
}
{
imwrite("edges_"+strDevType+".png", cvOut);
}
}
catch (std::exception &e)
{
std::cerr << e.what() << std::endl;
retval = 1;
}
return retval;
}
Results
Input image | Output image, edges |
 | |
Known Issues
- This sample doesn't work currently for PVA backend due to a bug in the Image Convolution code implementation.