OpenCV:基于特征检测的对象检测和跟踪

时间:2013-02-19 20:58:50

标签: c++ opencv implementation tracking video-processing

我刚刚尝试了计算机视觉,试图揭开它的各种错综复杂的神秘面纱。我正在尝试使用冲浪功能检测器来增强卡尔曼滤波器。但我不明白如何在使用冲浪特征在检测到的帧上构造单应性和有界矩形之后调用和使用kalman方法。我已经检测到这些特征,并在与输入帧进行比较后使用参考图像提取关键点。然后我用了flann matcher。

现在,使用卡尔曼滤波器是否可行,因为我想跟踪运动并获得预测的运动。我搜索了很多,但没有发现冲浪功能可以与卡尔曼滤波器一起使用。我得到的只是建议使用cvBlobs进行跟踪。但是,理论上卡尔曼滤波器用于跟踪目的。但是,我很困惑,因为使用冲浪的几种基于视频的跟踪实现表明冲浪本身可以用于跟踪。但我的问题是

  • 如果卡尔曼滤波器不能与冲浪一起使用,那么如何实现矩量来获取坐标测量值,因为我需要运动预测的信息。

  • 可以与kalman过滤器一起使用冲浪进行跟踪,如果是,则在检测到对象并使用以下代码将其与矩形绑定后如何实现它。

    示例:要跟踪的对象book1.png。一些框架frame1 rame2

    / *视频对象检测和识别* /

    int main()     {        Mat object = imread(“book1.png”,CV_LOAD_IMAGE_GRAYSCALE);

    if( !object.data )
    {
        std::cout<< "Error reading object " << std::endl;
        return -1;
    }
    
    //Detect the keypoints using SURF Detector
    int minHessian = 500;
    
    SurfFeatureDetector detector( minHessian );
    std::vector<KeyPoint> kp_object;
    
    detector.detect( object, kp_object );
    
    //Calculate descriptors (feature vectors)
    SurfDescriptorExtractor extractor;
    Mat des_object;
    
    extractor.compute( object, kp_object, des_object );
    
    FlannBasedMatcher matcher;        
    
    namedWindow("Good Matches");
    namedWindow("Tracking");
    
    std::vector<Point2f> obj_corners(4);
    
    //Get the corners from the object
    obj_corners[0] = cvPoint(0,0);
    obj_corners[1] = cvPoint( object.cols, 0 );
    obj_corners[2] = cvPoint( object.cols, object.rows );
    obj_corners[3] = cvPoint( 0, object.rows );
    
    char key = 'a';
    int framecount = 0;
    VideoCapture cap("booksvideo.avi");
    
    for(; ;)
    {
        Mat frame;
        cap >> frame;
        imshow("Good Matches", frame);
    
    
        Mat des_image, img_matches;
        std::vector<KeyPoint> kp_image;
        std::vector<vector<DMatch > > matches;
        std::vector<DMatch > good_matches;
        std::vector<Point2f> obj;
        std::vector<Point2f> scene;
        std::vector<Point2f> scene_corners(4);
        Mat H;
        Mat image;
    
        //cvtColor(frame, image, CV_RGB2GRAY);
    
        detector.detect( image, kp_image );
        extractor.compute( image, kp_image, des_image );
    
        matcher.knnMatch(des_object, des_image, matches, 2);
    
        //THIS  LOOP IS SENSITIVE TO SEGFAULTS
        for(int i = 0; i < min(des_image.rows-1,(int) matches.size()); i++) 
        {
            if((matches[i][0].distance < 0.6*(matches[i][4].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0))
            {
                good_matches.push_back(matches[i][0]);
            }
        }
    
        //Draw only "good" matches
        drawMatches( object, kp_object, image, kp_image, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
    
        if (good_matches.size() >= 4)
        {
            for( int i = 0; i < good_matches.size(); i++ )
            {
                //Get the keypoints from the good matches
                obj.push_back( kp_object[ good_matches[i].queryIdx ].pt );
                scene.push_back( kp_image[ good_matches[i].trainIdx ].pt );
            }
    
            H = findHomography( obj, scene, CV_RANSAC );
    
            perspectiveTransform( obj_corners, scene_corners, H);
    
            //Draw lines between the corners (the mapped object in the scene image )
            line( img_matches, scene_corners[0] + Point2f( object.cols, 0), scene_corners[1] + Point2f( object.cols, 0), Scalar(0, 255, 0), 4 );
            line( img_matches, scene_corners[1] + Point2f( object.cols, 0), scene_corners[2] + Point2f( object.cols, 0), Scalar( 0, 255, 0), 4 );
            line( img_matches, scene_corners[2] + Point2f( object.cols, 0), scene_corners[3] + Point2f( object.cols, 0), Scalar( 0, 255, 0), 4 );
            line( img_matches, scene_corners[3] + Point2f( object.cols, 0), scene_corners[0] + Point2f( object.cols, 0), Scalar( 0, 255, 0), 4 );
    
        }
    
        //Show detected matches
        imshow( "Good Matches", img_matches );
        for( int i = 0; i < good_matches.size(); i++ )
        { 
            printf( "-- Good Match [%d] Keypoint 1: %d  -- Keypoint 2: %d  \n", i,    good_matches[i].queryIdx, good_matches[i].trainIdx ); 
        }
    
        waitKey(0);
    

    }

    返回0;

    }

1 个答案:

答案 0 :(得分:2)

我还没有看到您正在描述的功能匹配和过滤的组合。我的一个想法是使用卡尔曼滤波器跟踪质心(和大小),并在下一帧运行特征匹配之前使用该信息来屏蔽外部区域。我不确定您的约束是什么,但您可以考虑模板匹配或camshift类型跟踪,也可以使用卡尔曼滤波器来帮助搜索。