bah*_*hti 8 c++ opencv image-processing sift
我正在尝试实现一个程序,它将输入两个图像,一个是单独一个框的图像,另一个是包含场景中的框.基本上,该程序应该在这两个图像中找到关键点,并将显示匹配关键点的图像.最后我希望看到两个输入图像的附加图像以及它们匹配的关键点连接.我的代码如下:
#include <opencv2\opencv.hpp>
#include <iostream>
int main(int argc, const char* argv[]) {
cv::Mat input1 = cv::imread("input.jpg", 1); //Load as grayscale
//cv::cvtColor(input1,input1,CV_BGR2GRAY);
//second input load as grayscale
cv::Mat input2 = cv::imread("input2.jpg",1);
cv::SiftFeatureDetector detector;
//cv::SiftFeatureDetector
detector(
1, 1,
cv::SIFT::CommonParams::DEFAULT_NOCTAVES,
cv::SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS,
cv::SIFT::CommonParams::DEFAULT_FIRST_OCTAVE,
cv::SIFT::CommonParams::FIRST_ANGLE );
std::vector<cv::KeyPoint> keypoints1;
detector.detect(input1, keypoints1);
// Add results to image and save.
cv::Mat output1;
cv::drawKeypoints(input1, keypoints1, output1);
cv::imshow("Sift_result1.jpg", output1);
cv::imwrite("Sift_result1.jpg",output1);
//keypoints array for input 2
std::vector<cv::KeyPoint> keypoints2;
//output array for ouput 2
cv::Mat output2;
//Sift extractor of opencv
cv::SiftDescriptorExtractor extractor;
cv::Mat descriptors1,descriptors2;
cv::BruteForceMatcher<cv::L2<float>> matcher;
cv::vector<cv::DMatch> matches;
cv::Mat img_matches;
detector.detect(input2,keypoints2);
cv::drawKeypoints(input2,keypoints2,output2);
cv::imshow("Sift_result2.jpg",output2);
cv::imwrite("Sift_result2.jpg",output2);
extractor.compute(input1,keypoints1,descriptors1);
extractor.compute(input2,keypoints2,descriptors2);
matcher.match(descriptors1,descriptors2,matches);
//show result
cv::drawMatches(input1,keypoints1,input2,keypoints2,matches,img_matches);
cv::imshow("matches",img_matches);
cv::imwrite("matches.jpg",img_matches);
cv::waitKey();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是有两个比预期更多的比赛.我试图调试程序并查看关键点向量内部的内容,等等,一切看起来都很好,至少我认为它们是关键点,检测方向等.
我正在使用OpenCV v2.3并检查其文档中我正在使用的类的类型,并尝试解决问题,但这不起作用.我在这方面工作了3天并没有太大的改进.
这是我从我的程序得到的输出.
我应该删除图像.
我知道不应该给我太多的匹配,因为我已经用matlab中的另一个实现测试了完全相同的图像,这非常好.