opencv matcher bad_alloc +不正确的库依赖?

时间:2012-04-26 11:41:23

标签: c++ visual-studio-2010 opencv

我一直在尝试使用强力匹配器匹配两个图像上的某些功能,这里是完整的代码:

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <vector>
#include<iostream>

int main()
{

    cv::Ptr<IplImage> img  = cvLoadImage("img.jpg",1);
    cv::Ptr<IplImage> img1 = cvLoadImage("img1.jpg",1);

    cv::Mat image(img,false);
    cv::Mat image1(img1,false);

    cv::resize(image,image,cv::Size(image.cols/5,image.rows/7));
    cv::resize(image1,image1,cv::Size(image1.cols/5,image1.rows/7));

    cv::waitKey(0);

    std::vector<cv::KeyPoint> keypoints;
    std::vector<cv::KeyPoint> keypoints1;

    cv::SurfFeatureDetector surf(20000);

    surf.detect(image,keypoints);
    surf.detect(image1,keypoints1);



    cv::drawKeypoints(image,keypoints,image,cv::Scalar(0,0,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
    cv::drawKeypoints(image1,keypoints1,image1,cv::Scalar(0,0,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

    cv::imshow("1",image);
    cv::imshow("2",image1);

    cv::waitKey(0);

    cv::SurfDescriptorExtractor surfDesc;

    cv::Mat descriptors;
    cv::Mat descriptors1;

    surfDesc.compute(image,keypoints,descriptors);
    surfDesc.compute(image1,keypoints1,descriptors1);

    std::cout<<sizeof(unsigned long);

    cv::BruteForceMatcher<cv::L2<float>>matcher;

    std::vector<cv::DMatch>matches;

     matcher.match( descriptors, descriptors1, matches );

     cv::Mat img_matches;
     drawMatches( image, keypoints, image1, keypoints1,matches, img_matches, cv::Scalar::all(-1), cv::Scalar::all(-1),std::vector<char>(), cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

     imshow( "Matches", img_matches );

    cv::waitKey(0);
    return 0;

}

程序在 matcher.match 处以“bad alloc”通信中断,但问题是关键点向量(可能)因为它显示的是0大小并且几百万的容量是ridicoulus(因此坏的分配问题)。函数 drawKeypoints 运行良好,它在图像上绘制了一组合理的关键点,尽管有关键点矢量参数,但这些关键点看起来很好。我应该在链接器 - >其他依赖项中添加我已经包含了没有“d”的库名称。例如,我有 opencv_highgui230 而不是 opencv_highgui230d 。这是一个我无法改变的必要性,因为如果库名称中添加了“d”,程序会在“应用程序无法成功运行...”通信后立即退出。我赢了7 64,VS 2010,我在调试模式下运行程序。我非常感谢matcher的一些帮助

1 个答案:

答案 0 :(得分:0)

我做了一些调查工作,并在Linux和Windows(使用VS2008)上使用 OpenCV 2.3.1 测试了您的代码。两个系统都是32位。

需要注意的一件重要事情是,您没有测试cvLoadImage()的成功与否。如果找不到图像,您将在以后遇到崩溃:

if (!img || !img1)
{
    // log error and quit
    return 1;
}

在Windows上,应用程序崩溃。重要的是要声明我使用完全相同的代码以及完全相同的输入图像:

enter image description here enter image description here

崩溃应用程序的指令就是这个调用:

surf.detect(image,keypoints);

错误说:

First-chance exception at 0x0038a473 in cv_test.exe: 0xC0000005: Access violation reading location 0x000000c8.
Unhandled exception at 0x0038a473 in cv_test.exe: 0xC0000005: Access violation reading location 0x000000c8.

我正在将应用程序链接到:

opencv_core231.lib opencv_highgui231.lib opencv_flann231.lib opencv_imgproc231.lib  opencv_features2d231.lib

适用于Linux ,生成的图片为:

enter image description here

我不知道还能告诉你什么。 = /

相关问题