0

我试图从 USB 摄像头的视频中找到一个对象。代码:

for (;;)
{
    Mat frame;
    cap >> frame; 

    Mat img_scene = frame;

    if (!img_object.data || !img_scene.data) 
    {
        string message = " Error reading ";
        readme(message);
    }

    int minHessian = 400;

    SurfFeatureDetector detector(minHessian);

    std::vector<KeyPoint> keypoints_object, keypoints_scene;

    detector.detect(img_object, keypoints_object);
    detector.detect(img_scene, keypoints_scene);
    SurfDescriptorExtractor extractor;

    Mat descriptors_object, descriptors_scene;

    extractor.compute(img_object, keypoints_object, descriptors_object);
    extractor.compute(img_scene, keypoints_scene, descriptors_scene);


    FlannBasedMatcher matcher;
    vector< DMatch > matches;

    descriptors_object.convertTo(descriptors_object, CV_32F);
    descriptors_scene.convertTo(descriptors_scene, CV_32F);
    if (descriptors_object.empty())
        cvError(0, "MatchFinder", "1st descriptor empty", __FILE__, __LINE__);
    if (descriptors_scene.empty())
        cvError(0, "MatchFinder", "2nd descriptor empty", __FILE__, __LINE__);
    matcher.match(descriptors_object, descriptors_scene, matches);

重新启动 PC 后,此代码可以正常工作。但是,下一次尝试不成功,因为 descriptors_scene 为空。如果发表评论if (descriptors_scene.empty()),我收到一个错误: cv::flann::buildIndex 中不支持的格式或格式组合 (type=0)... 我试图找到一个解决方案,我发现了两个建议:1)转换为另一种类型(使用descriptors_object.convertTo(descriptors_object, CV_32F);) - 完成,但没有运气。2)重启电脑。它有效且荒谬。

我很感激你的帮助。提前致谢。

4

0 回答 0