c++ - opencv 匹配器 bad_alloc + 不正确的库依赖项?

标签 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;

}

程序在 ma​​tcher.match 中断并出现“错误的分配”通信,但问题出在 keypoints vector (可能),因为它显示它的大小为 0 并且几百亿的容量是荒谬的(因此出现了糟糕的分配问题)。 drawKeypoints 函数运行良好,它在图像上绘制了一组合理的关键点,尽管有关键点 vector 参数,但这些关键点看起来不错。我应该补充一点,在 Linker->additional dependancies 中,我已经包含了没有“d”的库名称。例如,我有 opencv_highgui230 而不是 opencv_highgui230d。这是我无法更改的必需品,因为如果库名称添加了“d”,程序会在启动后立即退出,并显示“应用程序无法成功运行...”通信。我有 win 7 64,VS 2010,我在 Debug模式下运行程序。我真的很感激匹配器的一些帮助

最佳答案

我做了一些调查工作,并在 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

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

关于c++ - opencv 匹配器 bad_alloc + 不正确的库依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10332675/

相关文章:

python - 如何使用 open cv 将图像文件从一个目录写入和转换到另一个目录?

c# - 无法访问 C# emgucv 中的 Image<Bgr, byte>.Data 属性

c++ - std::sort 中用 lambda 函数指定的比较函数是否返回 bool 类型?

c++ - Visual Studio 2010 C++ 不显示控制台输出,尽管控制台子系统设置

vb.net - 确定是否在 ListView 框中选择了一个项目

c# - 为什么我需要 Default.aspx.designer.cs 文件?

c++ - Qt Creator 错误 : cannot find -lopencv_imgcodecs

c++ - Win API 绝对起点 - 从哪里开始?

C++ 之类的转换与 C 之类的转换?

c++ - 使用连接的套接字终止 QWebSocketServer