29 #include <opencv2/core/version.hpp>
30 #if CV_MAJOR_VERSION >= 3
31 # include <opencv2/imgcodecs.hpp>
33 # include <opencv2/highgui/highgui.hpp>
35 #include <opencv2/imgproc/imgproc.hpp>
51 #define CHECK_STATUS(STMT) \
54 VPIStatus status = (STMT); \
55 if (status != VPI_SUCCESS) \
57 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
58 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
59 std::ostringstream ss; \
60 ss << vpiStatusGetName(status) << ": " << buffer; \
61 throw std::runtime_error(ss.str()); \
65 int main(
int argc,
char *argv[])
79 int originX, originY, templWidth, templHeight;
81 int outWidth, outHeight;
98 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <cpu|cuda> <input image> <x,y,w,h> ");
101 std::string strBackend = argv[1];
102 std::string strInputFileName = argv[2];
104 if (sscanf(argv[3],
"%d,%d,%d,%d", &originX, &originY, &templWidth, &templHeight) != 4)
106 throw std::runtime_error(
107 "Invalid data format. Specify the bounding box of the input image as template image.");
111 cvImage = cv::imread(strInputFileName);
114 throw std::runtime_error(
"Can't open '" + strInputFileName +
"'");
117 assert(cvImage.type() == CV_8UC3);
120 cvtColor(cvImage, cvImageU8, cv::COLOR_BGR2GRAY);
122 if (originX + templWidth > cvImage.cols || originY + templHeight > cvImage.rows)
124 throw std::runtime_error(
"Bounding box is out of range of input image size");
127 cv::Rect templROI(originX, originY, templWidth, templHeight);
128 cv::Mat croppedRef(cvImageU8, templROI);
129 croppedRef.copyTo(cvTempl);
134 if (strBackend ==
"cpu")
138 else if (strBackend ==
"cuda")
144 throw std::runtime_error(
"Backend '" + strBackend +
"' not recognized, it must be either cpu, cuda");
160 outWidth = cvImage.cols - templWidth + 1;
161 outHeight = cvImage.rows - templHeight + 1;
185 CHECK_STATUS(
vpiSubmitMinMaxLoc(stream, backend, payloadMinMax, output, minCoords, maxCoords));
205 int max_i = max_coords[0].y;
206 int max_j = max_coords[0].x;
217 cv::Rect rect(max_i - 20, max_j - 20, 40, 40);
218 rectangle(cvOut, rect, 255, 2);
219 imwrite(
"template_matching_score_" + strBackend +
".png", cvOut);
221 printf(
"Provided coord of bounding box for the template is [%d, %d] with w=%d and h=%d \n", originY,
222 originX, templWidth, templHeight);
223 printf(
"Template matching location coord is [%d, %d] \n", max_j, max_i);
230 catch (std::exception &e)
232 std::cerr << e.what() << std::endl;
Functions and structures for dealing with VPI arrays.
Functions and structures for dealing with VPI images.
Declares functions to perform minimum and maximum location finding in images.
Functions for handling OpenCV interoperability with VPI.
Declaration of VPI status codes handling functions.
Declares functions dealing with VPI streams.
Declares functions that implement the template matching algorithm.
void * data
Points to the first element of the array.
VPIArrayBuffer buffer
Stores the array contents.
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_KEYPOINT_F32
VPIKeypointF32 element.
@ VPI_ARRAY_BUFFER_HOST_AOS
Host-accessible array-of-structures.
Stores information about array characteristics and contents.
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 vpiCreateMinMaxLoc(uint64_t backends, int32_t imageWidth, int32_t imageHeight, VPIImageFormat imageFormat, VPIPayload *payload)
Creates payload for vpiSubmitMinMaxLoc.
VPIStatus vpiSubmitMinMaxLoc(VPIStream stream, uint64_t backend, VPIPayload payload, VPIImage input, VPIArray minCoords, VPIArray maxCoords)
Finds minimum and maximum value locations in an image.
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_CPU
CPU backend.
VPIStatus vpiCreateTemplateMatching(uint64_t backends, int32_t imageWidth, int32_t imageHeight, VPIPayload *payload)
Creates payload for vpiSubmitTemplateMatching.
VPIStatus vpiTemplateMatchingSetSourceImage(VPIStream stream, uint64_t backend, VPIPayload payload, VPIImage srcImage)
Set the source image.
VPIStatus vpiTemplateMatchingSetTemplateImage(VPIStream stream, uint64_t backend, VPIPayload payload, VPIImage templImage, VPIImage mask)
Set the template image.
VPIStatus vpiSubmitTemplateMatching(VPIStream stream, uint64_t backend, VPIPayload payload, VPIImage output, VPITemplateMatchingMethod method)
Runs the template matching algorithm with provided template.
@ VPI_TEMPLATE_MATCHING_NCC
Normalized cross correlation.
@ 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.