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 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 device doesn't support PVA, an error is printed. In this case, just try another backend.
For convenience, here's the code that is also installed in the samples directory.
#include <cstring>
#include <iostream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#define CHECK_STATUS(STMT) \
do \
{ \
VPIStatus status = (STMT); \
if (status != VPI_SUCCESS) \
{ \
throw std::runtime_error(vpiStatusGetName(status)); \
} \
} while (0);
static cv::Mat DrawKeypoints(cv::Mat img,
VPIKeypoint *kpts, uint32_t *scores,
int numKeypoints)
{
cv::Mat out;
img.convertTo(out, CV_8UC1);
cvtColor(out, out, cv::COLOR_GRAY2BGR);
if (numKeypoints == 0)
{
return out;
}
cv::Mat cmap(1, 255, CV_8UC3);
{
cv::Mat gray(1, 255, CV_8UC1);
for (int i = 0; i < 255; ++i)
{
gray.at<unsigned char>(0, i) = i;
}
applyColorMap(gray, cmap, cv::COLORMAP_HOT);
}
float maxScore = *std::max_element(scores, scores + numKeypoints);
for (int i = 0; i < numKeypoints; ++i)
{
cv::Vec3b color = cmap.at<cv::Vec3b>(scores[i] / maxScore * 255);
circle(out, cv::Point(kpts[i].x, kpts[i].y), 3, cv::Scalar(color[0], color[1], color[2]), -1);
}
return out;
}
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 + "'");
}
cvImage.convertTo(cvImage, CV_16SC1);
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));
}
{
}
{
uint32_t *outScores = (uint32_t *)outScoresData.
data;
printf(
"%d keypoints found\n", outKeypointsData.
size);
cv::Mat outImage = DrawKeypoints(cvImage, outKeypoints, outScores, outKeypointsData.
size);
imwrite("harris_keypoints_"+strDevType+".png", outImage);
}
}
catch (std::exception &e)
{
std::cerr << e.what() << std::endl;
retval = 1;
}
return retval;
}