我试图自动决定两个数字是否相似。你可以在这里找到三个例子: http://imgur.com/N5SeaQW,IZYszx5,4RxLy4o
第一张图片与第二张和第三张图片不同,但第二张和第三张图片相同(不相同,如果仔细观察,会有一些小的差异)
我的代码(大部分来自OpenCV文档中的特征单应性示例)读取两个不同的图像,计算它们的关键点和描述符(使用SIFT,但我也尝试过SURF和ORB),使用FlannBasedMatcher匹配它们,计算单应矩阵。从可选状态向量中,我计算内点与关键点总数之比,以计算“距离”。
但是,计算的距离没有做出正确的区分:
d(img1, img2) = 0.296296
d(img1, img3) = 0.407407
d(img2, img3) = 0.362069
所以我无法得出结论,图像1与图像2不同,图像1与图像2不同,图像2等于图像3。
是否有更好的方法或指标来决定哪些图像相似?
Mat img_object = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
Mat img_scene = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );
//-- Step 1: Detect keypoints using SIFT
Ptr<FeatureDetector > detector;
detector = new SiftFeatureDetector();
std::vector<KeyPoint> keypoints_object, keypoints_scene;
detector->detect( img_object, keypoints_object );
detector->detect( img_scene, keypoints_scene );
//-- Step 2: Calculate descriptors (feature vectors)
Ptr<DescriptorExtractor > extractor;
extractor = new SiftDescriptorExtractor;
Mat descriptors_object, descriptors_scene;
extractor->compute( img_object, keypoints_object, descriptors_object );
extractor->compute( img_scene, keypoints_scene, descriptors_scene );
//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_object, descriptors_scene, matches );
//-- Step 4: Compute homography matrix and status vector
std::vector<Point2f> obj;
std::vector<Point2f> scene;
for( int i = 0; i<matches.size(); i++){
obj.push_back( keypoints_object[ matches[i].queryIdx ].pt );
scene.push_back( keypoints_scene[ matches[i].trainIdx ].pt );
}
std::vector<uchar> status;
Mat H = findHomography( obj, scene, status, CV_RANSAC, 3 );
//-- Step 5: Compute inliers/status.size
int inliers = 0;
for(int i = 0; i<status.size(); i++){
inliers += status[i];
}
printf("Percentage of inliers: %f \n",( (float) inliers)/( (float) status.size() ));
答案 0 :(得分:0)
是否有更好的方法或指标来决定哪些图像相似?
为什么不找到所有圆圈并计算它们之间的距离?
这是代码:
Point2i center(vector<Point2i> contour)
{
Moments m = moments(contour);
return Point2i(m.m10/m.m00, m.m01/m.m00);
}
vector<Point2i> getCenters(Mat img)
{
vector<vector<Point2i> > contours;
threshold(img, img, 0, 255, THRESH_OTSU);
findContours(img, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
vector<Point2i> result(contours.size());
for (int i=0; i<contours.size(); i++)
{
result[i] = center(contours[i]);
}
return result;
}
double distanceCenter(Point2i center1, Point2i center2)
{
return (center1.x - center2.x)*(center1.x - center2.x) + (center1.y - center2.y)*(center1.y - center2.y);
}
double distanceCenters(vector<Point2i> centers1, vector<Point2i> centers2)
{
if (centers1.size() != centers2.size())
{
return -1;
}
else
{
double result = 0;
for (int i=0; i<centers1.size(); i++)
{
double min = INT_MAX;
for (int j=0; j<centers2.size(); j++)
{
double dist = distanceCenter(centers1[i], centers2[j]);
if (dist < min)
{
min = dist;
}
}
result += min;
}
return result;
}
}
int main()
{
Mat img1 = imread("image1.png", CV_LOAD_IMAGE_GRAYSCALE),
img2 = imread("image2.png", CV_LOAD_IMAGE_GRAYSCALE);
cout << distanceCenters(getCenters(img1), getCenters(img2)) << endl;
return 0;
}
结果:
1到2之间的距离:14676
2到3之间的距离:393