这是我的代码(从zindarod获取输入后工作)
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace cv;
static void help()
{
printf("\nThis program demonstrates using features2d detector, descriptor extractor and simple matcher\n"
"Using the sift desriptor:\n"
"\n"
"Usage:\n matcher_simple <image1> <image2>\n");
}
int main(int argc, char** argv)
{
if(argc != 3)
{
help();
return -1;
}
Mat img1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
Rect regionone(151, 115, 42, 27);
Rect regiontwo(141, 105, 52, 37);
Mat dst,mask;
Rect rect(151, 115, 42, 27);
mask = Mat::zeros(img1.size(),CV_8UC1);
mask(Rect(151,115,42,27)) = 1;
img1.copyTo(dst,mask);
if(img1.empty() || img2.empty())
{
printf("Can't read one of the images\n");
return -1;
}
// detecting keypoints
SiftFeatureDetector detector(400);
vector<KeyPoint> keypoints1, keypoints2;
detector.detect(dst, keypoints1);
detector.detect(img2, keypoints2);
// computing descriptors
SiftDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(dst, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2);
// matching descriptors
BFMatcher matcher(NORM_L2);
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
// drawing the results
namedWindow("matches", 1);
Mat img_matches;
drawMatches(dst, keypoints1, img2, keypoints2, matches, img_matches);
imshow("masked image",dst);
//imshow("matches", img_matches);
waitKey(0);
return 0;
}
我的目标是比较两个不同图像的两个不同部分。 使用
后,您可以运行上面的代码 g++ above_code.cpp -o bincode -I /usr/include/ `pkg-config --libs --cflags opencv`
./bincode image1.png image2.png
似乎我将一个矩形区域传递给关键点检测器,因此keypoints1以相对于151,115的坐标保存。
所以,我应该将一个蒙面图像传递给关键点检测器。 如何创建一个填充零(或255)但矩形区域为151,115的矩阵从img1复制?
感谢。
答案 0 :(得分:0)
以下内容根据蒙版将源图像复制到目标图像。
Mat src = imread("source.jpg",-1),dst,mask;
Rect rect(151, 115, 42, 27);
mask = Mat::zeros(src.Size(),CV_8UC1);
rectangle(mask, Point(rect.x,rect.y),Point(rect.x+rect.width,rect.y+rect.height),Scalar(255),-1);
src.copyTo(dst,mask);
虽然有更好的解决方法,但您可以将关键点转换为原始图像的大小。