|
VPI - Vision Programming Interface
0.4.4 Release
|
Overview
This application detects Harris corners from a given input image. The corners are then drawn onto the input image and the result is saved to harris.png.
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 1D array where corners output will be written to.
- Create a properly-configured Harris algorithm and submit it to the stream.
- Simple stream synchronization.
- Array locking to access its contents from CPU side.
- Error handling.
- Environment clean up.
Instructions
The usage is:
./vpi_sample_03_harris_corners <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.
Once the sample is built, you can run it with the following command:
./vpi_sample_03_harris_corners pva ../assets/kodim08.png
This is using the PVA backend and one of the provided sample images. You can try with other input images, respecting the constraints imposed by the algorithm. If your stream doesn't support PVA, an error is printed. In this case, just try another backend.
Results
Input image | Output image, Harris keypoints |
 | |
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/contrib/contrib.hpp>
34 # include <opencv2/highgui/highgui.hpp>
37 #include <opencv2/imgproc/imgproc.hpp>
50 #define CHECK_STATUS(STMT) \
53 VPIStatus status = (STMT); \
54 if (status != VPI_SUCCESS) \
56 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
57 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
58 std::ostringstream ss; \
59 ss << vpiStatusGetName(status) << ": " << buffer; \
60 throw std::runtime_error(ss.str()); \
64 static cv::Mat DrawKeypoints(cv::Mat img,
VPIKeypoint *kpts, uint32_t *scores,
int numKeypoints)
67 img.convertTo(out, CV_8UC1);
68 cvtColor(out, out, cv::COLOR_GRAY2BGR);
70 if (numKeypoints == 0)
76 cv::Mat cmap(1, 256, CV_8UC3);
78 cv::Mat gray(1, 256, CV_8UC1);
79 for (
int i = 0; i < 256; ++i)
81 gray.at<
unsigned char>(0, i) = i;
83 applyColorMap(gray, cmap, cv::COLORMAP_HOT);
86 float maxScore = *std::max_element(scores, scores + numKeypoints);
88 for (
int i = 0; i < numKeypoints; ++i)
90 cv::Vec3b color = cmap.at<cv::Vec3b>(scores[i] / maxScore * 255);
91 circle(out, cv::Point(kpts[i].x, kpts[i].y), 3, cv::Scalar(color[0], color[1], color[2]), -1);
97 int main(
int argc,
char *argv[])
111 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <cpu|pva|cuda> <input image>");
114 std::string strBackend = argv[1];
115 std::string strInputFileName = argv[2];
118 cv::Mat cvImage = cv::imread(strInputFileName, cv::IMREAD_GRAYSCALE);
121 throw std::runtime_error(
"Can't open '" + strInputFileName +
"'");
125 cvImage.convertTo(cvImage, CV_16SC1);
130 if (strBackend ==
"cpu")
134 else if (strBackend ==
"cuda")
138 else if (strBackend ==
"pva")
144 throw std::runtime_error(
"Backend '" + strBackend +
145 "' not recognized, it must be either cpu, cuda or pva.");
155 memset(&imgData, 0,
sizeof(imgData));
201 uint32_t *outScores = (uint32_t *)outScoresData.
data;
203 printf(
"%d keypoints found\n", outKeypointsData.
size);
205 cv::Mat outImage = DrawKeypoints(cvImage, outKeypoints, outScores, outKeypointsData.
size);
207 imwrite(
"harris_corners_" + strBackend +
".png", outImage);
214 catch (std::exception &e)
216 std::cerr << e.what() << std::endl;
uint32_t height
Height of this plane in pixels.
void vpiArrayDestroy(VPIArray array)
Destroy an array instance.
float strengthThresh
Specifies the minimum threshold with which to eliminate Harris Corner scores.
uint32_t width
Width of this plane in pixels.
VPIStatus vpiArrayCreate(uint32_t capacity, VPIArrayType fmt, uint32_t flags, VPIArray *array)
Create an empty array instance.
VPIStatus vpiStreamCreate(uint32_t flags, VPIStream *stream)
Create a stream instance.
VPIBackend
VPI Backend types.
@ VPI_LOCK_READ
Lock memory only for reading.
VPIStatus vpiCreateHarrisCornerDetector(VPIBackend backend, uint32_t inputWidth, uint32_t inputHeight, VPIPayload *payload)
Creates a Harris Corner Detector payload.
VPIStatus vpiArrayUnlock(VPIArray array)
Releases the lock on array object.
Functions and structures for dealing with VPI arrays.
void vpiPayloadDestroy(VPIPayload payload)
Deallocates the payload object and all associated resources.
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.
uint32_t size
Number of elements in the array.
void vpiStreamDestroy(VPIStream stream)
Destroy a stream instance and deallocate all HW resources.
void * data
Points to the first element of the array.
VPIImagePlane planes[VPI_MAX_PLANE_COUNT]
Data of all image planes.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
uint32_t gradientSize
Gradient window size.
@ VPI_ARRAY_TYPE_KEYPOINT
VPIKeypoint element.
VPIStatus vpiSubmitHarrisCornerDetector(VPIStream stream, VPIPayload payload, VPIImage input, VPIArray outFeatures, VPIArray outScores, const VPIHarrisCornerDetectorParams *params)
Submits Harris Corner Detector operation to the stream associated with the payload.
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.
Stores information about array characteristics and content.
struct VPIPayloadImpl * VPIPayload
A handle to an algorithm payload.
float sensitivity
Specifies sensitivity threshold from the Harris-Stephens equation.
Declares functions that implement the Harris Corner Detector algorithm.
float minNMSDistance
Non-maximum suppression radius, set to 0 to disable it.
uint32_t blockSize
Block window size used to compute the Harris Corner score.
Structure that defines the parameters for vpiSubmitHarrisCornerDetector.
VPIImageFormat type
Image type.
Declaration of VPI status codes handling functions.
VPIStatus vpiArrayLock(VPIArray array, VPILockMode mode, VPIArrayData *arrayData)
Acquires the lock on array object and returns a pointer to array data.
@ VPI_BACKEND_CPU
CPU backend.
struct VPIArrayImpl * VPIArray
A handle to an array.
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_ARRAY_TYPE_U32
unsigned 32-bit.
@ VPI_BACKEND_PVA
PVA backend.
Stores a keypoint coordinate.