c++ - OpenCV 奇怪的内存损坏

标签 c++ visual-studio opencv memory-leaks runtime-error

我使用 Visual Studio 为一个项目设置了 OpenCV,但我遇到了这些非常奇怪的内存错误。我一直在广泛寻找解决此问题的方法,虽然有许多类似的问题,但它们要么没有答案,要么对我不起作用。

这是我遇到问题的少数 OpenCV 函数之一(从 docs 获得),它复制了我得到的错误:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);


/** @function main */
int main(int argc, char** argv)
{
    /// Load source image and convert it to gray
    std::string img = "<path-to-picture>";
    src = imread(img, CV_LOAD_IMAGE_COLOR);

    /// Convert image to gray and blur it
    cvtColor(src, src_gray, CV_BGR2GRAY);
    blur(src_gray, src_gray, Size(3, 3));

    /// Create Window
    char* source_window = "Source";
    namedWindow(source_window, CV_WINDOW_AUTOSIZE);
    imshow(source_window, src);

    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Detect edges using canny
    Canny(src_gray, canny_output, thresh, thresh * 2, 3);
    /// Find contours
    findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    /// Draw contours
    Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
    for (int i = 0; i< contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
    }

    /// Show in a window
    namedWindow("Contours", CV_WINDOW_AUTOSIZE);
    imshow("Contours", drawing);


    waitKey(0);
    return(0);
}

奇怪的是 findContours() 工作完美,但之后程序崩溃并出现此错误:

Expression: "(_Ptr_user & (_BIG_ALLOCATION_ALIGNMENT - 1)) == 0" && 0

关于如何解决这个问题的任何想法?这是我的 OpenCV 设置:

  • Visual Studio 2015,调试/发布 x64
  • OpenCV 2.4.13(预建)
  • C++ includes 指向 build\include
  • C++ 链接器 指向 \build\x64\vc12\lib
  • 依赖项包括上述文件夹中的库。

最佳答案

您正在使用带有 vc12 编译器 (Visual Studio 2013) 的 OpenCV 构建,但在您的项目中您使用的是 vc14 (Visual Studio 2105)。

一定要使用用 vc14 编译的预构建库。

我确定 OpenCV 3.1 具有 vc14 的预构建二进制文件。我不知道 OpenCV 2.4.13 是否也有它们(可能没有)。这种情况下需要用vc14重新编译OpenCV,或者切换到OpenCV 3.1

关于c++ - OpenCV 奇怪的内存损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39106789/

相关文章:

c# - 有没有一种简单的方法可以在我的 .NET 项目中找到未使用的程序集引用?

python - 带有 Python 的 OpenCV 中的 imageData 函数

python - 如何知道 cv2 VideoCapture 对象中有多少个颜色 channel ?

c++ - 用于生成行人检测 ROI 的图像调整大小与图像金字塔 - OpenCV

c++ - 如果我想找到一些东西但不希望它在找不到时最后返回,我应该使用哪种 STL 算法?

visual-studio - Visual Studio 2010 版本控制和错误跟踪功能

c++ - 动态转换的一些 'good use' 示例是什么?

c++ - 更改项目以运行 VS2012 C++

c++ - 短版 : Error 14 error C2660: 'Player::addSpell' : function does not take 1 arguments

c++ - Qt QDesktopServices::openUrl - 使用发布值启动浏览器