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>
52 #define CHECK_STATUS(STMT) \
55 VPIStatus status = (STMT); \
56 if (status != VPI_SUCCESS) \
58 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
59 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
60 std::ostringstream ss; \
61 ss << vpiStatusGetName(status) << ": " << buffer; \
62 throw std::runtime_error(ss.str()); \
66 static cv::Mat DrawKeypoints(cv::Mat img,
VPIKeypointF32 *kpts, uint32_t *scores,
int numKeypoints)
69 img.convertTo(out, CV_8UC1);
70 cvtColor(out, out, cv::COLOR_GRAY2BGR);
72 if (numKeypoints == 0)
78 cv::Mat cmap(1, 256, CV_8UC3);
80 cv::Mat gray(1, 256, CV_8UC1);
81 for (
int i = 0; i < 256; ++i)
83 gray.at<
unsigned char>(0, i) = i;
85 applyColorMap(gray, cmap, cv::COLORMAP_HOT);
88 float maxScore = *std::max_element(scores, scores + numKeypoints);
90 for (
int i = 0; i < numKeypoints; ++i)
92 cv::Vec3b color = cmap.at<cv::Vec3b>(scores[i] / maxScore * 255);
93 circle(out, cv::Point(kpts[i].x, kpts[i].y), 3, cv::Scalar(color[0], color[1], color[2]), -1);
99 int main(
int argc,
char *argv[])
122 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <cpu|pva|cuda> <input image>");
125 std::string strBackend = argv[1];
126 std::string strInputFileName = argv[2];
131 if (strBackend ==
"cpu")
135 else if (strBackend ==
"cuda")
139 else if (strBackend ==
"pva")
145 throw std::runtime_error(
"Backend '" + strBackend +
146 "' not recognized, it must be either cpu, cuda or pva.");
152 cvImage = cv::imread(strInputFileName);
155 throw std::runtime_error(
"Can't open '" + strInputFileName +
"'");
210 uint32_t *outScores = (uint32_t *)outScoresData.
buffer.
aos.
data;
217 cv::Mat outImage = DrawKeypoints(img, outKeypoints, outScores, *outKeypointsData.
buffer.
aos.
sizePointer);
219 imwrite(
"harris_corners_" + strBackend +
".png", outImage);
226 catch (std::exception &e)
228 std::cerr << e.what() << std::endl;
Functions and structures for dealing with VPI arrays.
Declares functions that implement the Harris Corner Detector algorithm.
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.
void * data
Points to the first element of the array.
VPIArrayBuffer buffer
Stores the array contents.
int32_t * sizePointer
Points to the number of elements in the array.
VPIArrayBufferAOS aos
Array stored in array-of-structures layout.
VPIStatus vpiArrayUnlock(VPIArray array)
Releases the lock on array object.
VPIStatus vpiArrayLockData(VPIArray array, VPILockMode mode, VPIArrayBufferType bufType, VPIArrayData *data)
Acquires the lock on an array object and returns the array contents.
void vpiArrayDestroy(VPIArray array)
Destroy an array instance.
VPIStatus vpiArrayCreate(int32_t capacity, VPIArrayType type, uint64_t flags, VPIArray *array)
Create an empty array instance.
struct VPIArrayImpl * VPIArray
A handle to an array.
@ VPI_ARRAY_TYPE_U32
Unsigned 32-bit.
@ VPI_ARRAY_TYPE_KEYPOINT_F32
VPIKeypointF32 element.
@ VPI_ARRAY_BUFFER_HOST_AOS
Host-accessible array-of-structures.
Stores information about array characteristics and contents.
float sensitivity
Specifies sensitivity threshold from the Harris-Stephens equation.
VPIStatus vpiSubmitHarrisCornerDetector(VPIStream stream, uint64_t backend, VPIPayload payload, VPIImage input, VPIArray outFeatures, VPIArray outScores, const VPIHarrisCornerDetectorParams *params)
Submits a Harris Corner Detector operation to the stream.
VPIStatus vpiInitHarrisCornerDetectorParams(VPIHarrisCornerDetectorParams *params)
Initializes VPIHarrisCornerDetectorParams with default values.
VPIStatus vpiCreateHarrisCornerDetector(uint64_t backends, int32_t inputWidth, int32_t inputHeight, VPIPayload *payload)
Creates a Harris Corner Detector payload.
Structure that defines the parameters for vpiSubmitHarrisCornerDetector.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
struct VPIImageImpl * VPIImage
A handle to an image.
VPIStatus vpiImageLockData(VPIImage img, VPILockMode mode, VPIImageBufferType bufType, VPIImageData *data)
Acquires the lock on an image object and returns the image contents.
VPIStatus vpiImageCreate(int32_t width, int32_t height, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
VPIStatus vpiImageUnlock(VPIImage img)
Releases the lock on an image object.
@ VPI_IMAGE_BUFFER_HOST_PITCH_LINEAR
Host-accessible with planes in pitch-linear memory layout.
Stores information about image characteristics and content.
VPIStatus vpiImageCreateWrapperOpenCVMat(const cv::Mat &mat, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
Wraps a cv::Mat in an VPIImage with the given image format.
VPIStatus vpiImageDataExportOpenCVMat(const VPIImageData &imgData, cv::Mat *mat)
Fills an existing cv::Mat with data from VPIImageData coming from a locked VPIImage.
struct VPIPayloadImpl * VPIPayload
A handle to an algorithm payload.
void vpiPayloadDestroy(VPIPayload payload)
Deallocates the payload object and all associated resources.
struct VPIStreamImpl * VPIStream
A handle to a stream.
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.
void vpiStreamDestroy(VPIStream stream)
Destroy a stream instance and deallocate all HW resources.
VPIStatus vpiStreamCreate(uint64_t flags, VPIStream *stream)
Create a stream instance.
@ VPI_BACKEND_CUDA
CUDA backend.
@ VPI_BACKEND_PVA
PVA backend.
@ VPI_BACKEND_CPU
CPU backend.
@ VPI_LOCK_READ
Lock memory only for reading.
Stores a float32 keypoint coordinate The coordinate is relative to the top-left corner of an image.