【问题标题】:Runtime error Visual Studio and OpenCV Error while using SURF使用 SURF 时出现运行时错误 Visual Studio 和 OpenCV 错误
【发布时间】:2013-01-14 19:25:34
【问题描述】:

以下代码应该在 Visual Studio 2010 上使用OpenCV2.3 中的 SURF 执行特征检测。图像“sample.jpg”是一本书的彩色图像 (RGB),捕获的视频将包含几本不同的书籍.没有编译错误,但在 CTRL F5 上,控制台显示 Native' has exited with code -1073741811 (0xc000000d)。这很奇怪,因为其他程序运行良好。我通过使用显示图像的简单代码删除此功能检测代码来对其进行测试,这似乎工作正常。只有当我运行此代码时,它才会引发此错误。我在附加依赖项下包含了以下库:

opencv_core230d.lib
opencv_highgui230d.lib
opencv_ml230d.lib
opencv_legacy230d.lib
opencv_imgproc230d.lib
opencv_features2d230d.lib
opencv_calib3d230d.lib
opencv_flann230d.lib

我是否需要为 SURF 添加一个库或其他东西,因为这是我第一次使用 Surf。请帮忙。

int main()
{
    Mat object = imread( "C:\\OpenCV2.3\\sample.jpg");

    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;

    VideoCapture cap(0);
if( !cap.isOpened() ) return -1;

    namedWindow("Good Matches");

    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;
    while (key != 27)
    {
        Mat frame;
        cap >> frame;

        if (framecount < 5)
        {
            framecount++;
            continue;
        }

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

        for(int i = 0; i < min(des_image.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches[i][0].distance < 0.6*(matches[i][3].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 );

        key = waitKey(1);
    }
    return 0;
}

【问题讨论】:

    标签: visual-studio-2010 opencv runtime-error surf object-detection


    【解决方案1】:

    我相信 SURF 检测器不适用于彩色图像。您将需要使用灰度加载它们

    Mat object = imread( "C:\\OpenCV2.3\\sample.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    

    【讨论】:

    • 感谢您的回复。我将图像更改为灰度Lena,仍然存在错误!即使我不使用视频并仅在2个灰度图像之间进行比较,也会抛出相同的错误.
    • 这是引发相同错误的图像问题stackoverflow.com/questions/14638928/…
    • 好吧,我无法测试您遇到的完全相同的情况,因为我的笔记本电脑上只有 opencv 2.49 的发布版本(我的 ssd 上的空间限制......)。不过,代码可以在我的笔记本电脑上正确构建和运行。您是在发布模式还是调试模式下构建?我注意到您正在链接到调试库。
    • 好吧,我所有其他程序都在调试模式下运行。当我更改为发布模式时,所有包含标题下都有红色曲线,并且出现错误说没有这样的文件或目录。请原谅我对于此类非技术术语,您是否建议在发布模式下运行?如果是,那么需要做什么?
    • 您需要为发布版本再次设置包含和库目录,然后再次指定依赖项。在 opencv 中,库的调试版本与发布版本的区别在于文件名末尾的“d”。要链接到发布版本,请指定不带“d”的文件名。
    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    相关资源
    最近更新 更多