使用openCV匹配图像

时间:2015-02-05 14:01:03

标签: c++ opencv image-processing pattern-matching

首先,我对匹配技术比较陌生,所以请耐心等待:

我正在开发一个应用程序,将训练图像与收集的图像(单个细胞样本)相匹配。

我已经使用SIFT检测器和SURF检测器与基于FLANN的匹配来匹配一组训练数据到收集的图像。但我得到的结果真的很差。我使用与openCV文档中相同的代码:

    void foramsMatching(Mat img_object, Mat img_scene){
    int minHessian = 400;

    SiftFeatureDetector detector(minHessian);

    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)
    SurfDescriptorExtractor extractor;

    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;
    //BFMatcher matcher;
    std::vector< DMatch > matches;
    matcher.match(descriptors_object, descriptors_scene, matches);


    double max_dist = 0; double min_dist = 100;

    //-- Quick calculation of max and min distances between keypoints
    for (int i = 0; i < descriptors_object.rows; i++)
    {
        double dist = matches[i].distance;
        if (dist < min_dist) min_dist = dist;
        if (dist > max_dist) max_dist = dist;
    }

    printf("-- Max dist : %f \n", max_dist);
    printf("-- Min dist : %f \n", min_dist);

    //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
    std::vector< DMatch > good_matches;

    for (int i = 0; i < descriptors_object.rows; i++)
    {
        if (matches[i].distance < 3 * min_dist)
        {
            good_matches.push_back(matches[i]);
        }
    }

    Mat img_matches;
    drawMatches(img_object, keypoints_object, img_scene, keypoints_scene,
    good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
    vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);

    //-- Localize the object
    std::vector<Point2f> obj;
    std::vector<Point2f> scene;

    for (int i = 0; i < good_matches.size(); i++)
    {
        //-- Get the keypoints from the good matches
        obj.push_back(keypoints_object[good_matches[i].queryIdx].pt);
        scene.push_back(keypoints_scene[good_matches[i].trainIdx].pt);
    }

    Mat H = findHomography(obj, scene, CV_RANSAC);

    //-- Get the corners from the image_1 ( the object to be "detected" )
    std::vector<Point2f> obj_corners(4);
    obj_corners[0] = cvPoint(0, 0); obj_corners[1] = cvPoint(img_object.cols, 0);
    obj_corners[2] = cvPoint(img_object.cols, img_object.rows); obj_corners[3] = cvPoint(0, img_object.rows);
    std::vector<Point2f> scene_corners(4);

    perspectiveTransform(obj_corners, scene_corners, H);

    //-- Draw lines between the corners (the mapped object in the scene - image_2 )
    line(img_matches, scene_corners[0] + Point2f(img_object.cols, 0), scene_corners[1] + Point2f(img_object.cols, 0), Scalar(0, 255, 0), 4);
    line(img_matches, scene_corners[1] + Point2f(img_object.cols, 0), scene_corners[2] + Point2f(img_object.cols, 0), Scalar(0, 255, 0), 4);
    line(img_matches, scene_corners[2] + Point2f(img_object.cols, 0), scene_corners[3] + Point2f(img_object.cols, 0), Scalar(0, 255, 0), 4);
    line(img_matches, scene_corners[3] + Point2f(img_object.cols, 0), scene_corners[0] + Point2f(img_object.cols, 0), Scalar(0, 255, 0), 4);

    //-- Show detected matches
    namedWindow("Good Matches & Object detection", CV_WINDOW_NORMAL);
    imshow("Good Matches & Object detection", img_matches);
    //imwrite("../../Samples/Matching.jpg", img_matches);
}

以下是结果 - Matching Two Images

与我使用这些方法看到的其他一些结果相比,它们真的很差。屏幕底部的两个blob(单元格)应该有两个匹配。

关于我做错了什么或如何改善这些结果的任何想法? 我正在考虑编写自己的Matcher / Discription Extractor,因为我的训练图像并不是我要查询的细胞的精确复制品。 这是一个好主意吗?如果是这样,我应该看看任何教程?

此致

1 个答案:

答案 0 :(得分:0)

将评论转换为答案:

在运行SIFT / SURF之前,您应该使用可用的知识进行某种预处理,以便找到感兴趣的区域并消除噪音。这是一般的想法:

  1. 执行细分
  2. 检查细分特定标准(*)并选择有趣的候选人。
  3. 对候选段进行匹配。
  4. (*)您可以用于此步骤的内容包括:区域大小,形状,颜色分布等。从您提供的示例中,它可以例如可以看出你的物体是圆形的并且具有一定的最小尺寸。使用您拥有的任何知识来消除进一步的误报。当然,您需要进行一些调整,以使您的规则集不会过于严格,即保持真正的正面。