比较图像的相似性

Compare similarity of images

本文关键字:相似性 图像 比较      更新时间:2023-10-16

我正试图自动判断这两个数字是否相似。你可以在这里找到三个例子:https://i.stack.imgur.com/09Zqf.png

第一个图像与第二个和第三个图像不同,但第二个与第三个是相同的(不完全相同,如果你仔细观察,会有一些小的差异)

我的代码(大部分来自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() ));

有没有更好的方法或指标来决定哪些图像相似?

你为什么不找出所有的圆并计算它们之间的距离呢?

这里的代码:

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