OpenCV之特征检测(一)

OpenCV的feature2d模块包括了特征检测函数,它的主要目的是在一副图像找找到目标问题,先上代码有个整体的认识,后面再逐步详细分析每一个步骤。
 
OpenCV的feature2d模块包括了特征检测函数,它的主要目的是在一副图像找找到目标问题,先上代码有个整体的认识。

int main(int argc, char** argv)
{
    Mat img_object = imread("./box.png", CV_LOAD_IMAGE_GRAYSCALE);
    Mat img_scene = imread("./box_in_scene.png", CV_LOAD_IMAGE_GRAYSCALE);
    if (!img_object.data || !img_scene.data)
    {
        std::cout << " --(!) Error reading images " << std::endl; return -1;
    }
    //-- Step 1: 计算KeyPoints
    int minHessian = 400;
    Ptr<SurfFeatureDetector> detector = SurfFeatureDetector::create(minHessian);
    std::vector<KeyPoint> keypoints_object, keypoints_scene;
    detector->detect(img_object, keypoints_object);
    detector->detect(img_scene, keypoints_scene);
    //-- Step 2: 计算特征描述子
    Ptr<SurfDescriptorExtractor>  extractor = SurfDescriptorExtractor::create();
    Mat descriptors_object, descriptors_scene;
    extractor->compute(img_object, keypoints_object, descriptors_object);
    extractor->compute(img_scene, keypoints_scene, descriptors_scene);
    //-- Step 3: 匹配特征描述子
    FlannBasedMatcher matcher;
    std::vector< DMatch > matches;
    matcher.match(descriptors_object, descriptors_scene, matches);
     // --Step 4: 寻找好的特征描述子
    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 < 4 * 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);
    }
     // Step5:计算单应矩阵
    Mat H = findHomography(obj, scene, RANSAC);
    //-- Get the corners from the image_1 ( the object to be "detected" )
    std::vector<Point2f> obj_corners(4);
    obj_corners[0] = Point(0, 0); 
     obj_corners[1] = Point(img_object.cols, 0);
    obj_corners[2] =  Point(img_object.cols, img_object.rows); 
     obj_corners[3] = Point(0, img_object.rows);
    std::vector<Point2f> scene_corners(4);
     // Step6: 目标物体四个顶点的透视变换
    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
    imshow("Good Matches & Object detection", img_matches);
    waitKey(0);
    return 0;
}

结果如下:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值