美文网首页
iOS opencv对比两个图的相似度

iOS opencv对比两个图的相似度

作者: FateOfKing | 来源:发表于2019-03-28 09:07 被阅读0次
#include <opencv2/opencv.hpp>

#import "OpenCVTool.h"
using namespace std;
using namespace cv;
const float inlier_threshold = 2.5f; // Distance threshold to identify inliers with homography check
const float nn_match_ratio = 0.8f;


+(BOOL)checkImage:(NSString *)path1 withImage:(NSString *)path2
{
    const char * cpath = [path1 cStringUsingEncoding:NSUTF8StringEncoding];
    const char * cpath1 = [path2 cStringUsingEncoding:NSUTF8StringEncoding];
    Mat img1 = imread(cpath, IMREAD_GRAYSCALE);
    Mat img2 = imread(cpath1, IMREAD_GRAYSCALE);
    Mat homography;
    vector<KeyPoint> kpts1, kpts2;
    Mat desc1, desc2;
    Ptr<AKAZE> akaze = AKAZE::create();
    akaze->detectAndCompute(img1, noArray(), kpts1, desc1);
    akaze->detectAndCompute(img2, noArray(), kpts2, desc2);
    BFMatcher matcher(NORM_HAMMING);
    vector< vector<DMatch> > nn_matches;
    matcher.knnMatch(desc1, desc2, nn_matches, 2);
    //--------------------
    vector<KeyPoint> matched1, matched2;
    vector<Point2f> obj, scene;
    for(size_t i = 0; i < nn_matches.size(); i++) {
        DMatch first = nn_matches[i][0];
        float dist1 = nn_matches[i][0].distance;
        float dist2 = nn_matches[i][1].distance;
        if(dist1 < nn_match_ratio * dist2) {
            matched1.push_back(kpts1[first.queryIdx]);
            matched2.push_back(kpts2[first.trainIdx]);
            //-- Get the keypoints from the good matches
            obj.push_back( kpts1[first.queryIdx].pt );
            scene.push_back( kpts2[first.trainIdx].pt );
        }
    }
    homography = findHomography( obj, scene, RANSAC );
    
    
    vector<DMatch> good_matches;
    vector<KeyPoint> inliers1, inliers2;
    for(size_t i = 0; i < matched1.size(); i++) {
        Mat col = Mat::ones(3, 1, CV_64F);
        col.at<double>(0) = matched1[i].pt.x;
        col.at<double>(1) = matched1[i].pt.y;
        col = homography * col;
        col /= col.at<double>(2);
        double dist = sqrt( pow(col.at<double>(0) - matched2[i].pt.x, 2) +
                           pow(col.at<double>(1) - matched2[i].pt.y, 2));
        if(dist < inlier_threshold) {
            int new_i = static_cast<int>(inliers1.size());
            inliers1.push_back(matched1[i]);
            inliers2.push_back(matched2[i]);
            good_matches.push_back(DMatch(new_i, new_i, 0));
        }
    }
    double inlier_ratio = inliers1.size() / (double) matched1.size();
    double match =  (double) matched1.size()/inliers2.size() ;

    cout << "A-KAZE Matching Results" << endl;
    cout << "*******************************" << endl;
    cout << "# Keypoints 1:                        \t" << kpts1.size() << endl;
    cout << "# Keypoints 2:                        \t" << kpts2.size() << endl;
    cout << "# Matches:                            \t" << matched1.size() << endl;
    cout << "# Inliers:                            \t" << inliers1.size() << endl;
    cout << "# Inliers Ratio:                      \t" << inlier_ratio << endl;
    cout << endl;
    if (inlier_ratio >= 0.7&&match>0.15) {
        return YES;
    }else
    {
        return NO;
    }
}

主要参考例子例子1例子2
其中homography需要自己计算一下

相关文章

网友评论

      本文标题:iOS opencv对比两个图的相似度

      本文链接:https://www.haomeiwen.com/subject/hhjfbqtx.html