我使用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设置:
build\include
\build\x64\vc12\lib
答案 0 :(得分:1)
您正在使用OpenCV build with vc12 compiler(Visual Studio 2013),但在您的项目中,您使用的是vc14(Visual Studio 2105)。
请务必使用使用vc14编译的预构建库。
我确信OpenCV 3.1已经为vc14预建了二进制文件。我不知道OpenCV 2.4.13是否也有它们(可能不是)。在这种情况下,您需要使用vc14重新编译OpenCV,或切换到OpenCV 3.1