opencv c++ feature detection
#include <opencv2/opencv.hpp>
int main() {
// Read input images
cv::Mat img1 = cv::imread("image1.jpg");
cv::Mat img2 = cv::imread("image2.jpg");
// Initialize keypoint vectors to store detected keypoints
std::vector<cv::KeyPoint> keypoints1, keypoints2;
// Initialize descriptor matrices to store computed descriptors
cv::Mat descriptors1, descriptors2;
// Create ORB (Oriented FAST and Rotated BRIEF) object
cv::Ptr<cv::ORB> orb = cv::ORB::create();
// Detect keypoints and compute descriptors for the first image
orb->detectAndCompute(img1, cv::noArray(), keypoints1, descriptors1);
// Detect keypoints and compute descriptors for the second image
orb->detectAndCompute(img2, cv::noArray(), keypoints2, descriptors2);
// Initialize a BFMatcher object for matching descriptors using Brute Force method
cv::BFMatcher matcher(cv::NORM_HAMMING);
// Match descriptors of the two images
std::vector<cv::DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
// Draw matches on a new image
cv::Mat img_matches;
cv::drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
// Display the matches
cv::imshow("Matches", img_matches);
cv::waitKey(0);
return 0;
}
This C++ code snippet demonstrates feature detection and matching using OpenCV's ORB (Oriented FAST and Rotated BRIEF) algorithm. It reads two input images, detects keypoints, computes descriptors, matches them, and displays the resulting matches using the Brute Force matcher.