Opencv implements image search

1. Steps

1.1 Import the OpenCV library:

In your C++ code, you first need to import the OpenCV library. You can import core modules with the following statement:

#include <opencv2/core/core.hpp>

1.2 Loading images

Use OpenCV's imread function to load the image to be searched and the target image. For example, assuming the image you are searching for is "search_image.jpg" and the target image is "target_image.jpg", you can load them with:
cpp

cv::Mat searchImage = cv::imread("search_image.jpg");
cv::Mat targetImage = cv::imread("target_image.jpg");

1.3 Feature extraction

Extract features from the target image using OpenCV's feature extraction methods such as SIFT, SURF, or ORB. For example, using the SIFT algorithm to extract features, you can use the following code:

cv::Ptr<cv::SIFT> sift = cv::SIFT::create();
cv::Mat targetDescriptors;
std::vector<cv::KeyPoint> targetKeypoints;
sift->detectAndCompute(targetImage, cv::noArray(), targetKeypoints, targetDescriptors);

1.4 Matching Features

Use the extracted features to find matches in the search image. You can use OpenCV's feature matching methods like FLANN or Brute-Force for matching. Here is an example using the Brute-Force matcher:

cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::BRUTEFORCE);
std::vector<cv::DMatch> matches;
matcher->match(searchDescriptors, targetDescriptors, matches);

1.5 Display Results

Depending on the matching results, you can choose to draw the matching keypoints or bounding boxes on the search image. Here is a simple example for plotting matching keypoints on a search image:

cv::Mat outputImage;
cv::drawMatches(searchImage, searchKeypoints, targetImage, targetKeypoints, matches, outputImage);
cv::imshow("Matches", outputImage);
cv::waitKey(0);

2. Complete code

#include <opencv2/core/core.hpp>

int search_pic_by_pic()
{
    
    
	// 加载查询图像和目标图像
	cv::Mat queryImage = cv::imread("E:\\code\\Yolov5_Tensorrt_Win10-master\\pictures\\search_pic_by_pic\\1.png");
	cv::Mat targetImage = cv::imread("E:\\code\\Yolov5_Tensorrt_Win10-master\\pictures\\search_pic_by_pic\\2.png");

	// 特征提取
	cv::Ptr<cv::Feature2D> featureExtractor = cv::SIFT::create();
	cv::Mat queryDescriptors, targetDescriptors;
	std::vector<cv::KeyPoint> queryKeypoints, targetKeypoints;
	featureExtractor->detectAndCompute(queryImage, cv::noArray(), queryKeypoints, queryDescriptors);
	featureExtractor->detectAndCompute(targetImage, cv::noArray(), targetKeypoints, targetDescriptors);

	// 特征匹配
	cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::FLANNBASED);
	std::vector<cv::DMatch> matches;
	matcher->match(queryDescriptors, targetDescriptors, matches);

	// 根据匹配结果进行排序
	std::sort(matches.begin(), matches.end(), [](const cv::DMatch& a, const cv::DMatch& b) {
    
    
		return a.distance < b.distance;
		});

	float threshold = 200.0;
	int numMatches = 0;
	int matches_size = matches.size();
	vector< cv::DMatch>::iterator it = matches.begin();
	for (it; it != matches.end();) {
    
    
		if (it->distance < threshold) {
    
    
			numMatches++;
			it++;
		}
		else {
    
    
			it = matches.erase(it);
		}
	}

	float matchRate = static_cast<float>(numMatches) / matches_size * 100.0;
	std::cout << "Match Rate: " << matchRate << "%" << std::endl;

	// 显示匹配结果
	cv::Mat matchedImage;
	cv::drawMatches(queryImage, queryKeypoints, targetImage, targetKeypoints, matches, matchedImage);
	cv::imshow("Matched Image", matchedImage);
	cv::waitKey(0);

	return 0;
}

int main()
{
    
    
	search_pic_by_pic();
	return 0;
}

3. Test pictures and effects

insert image description here
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/wyw0000/article/details/132289302