从函数崩溃返回,只有在代码中的某一点之后 [英] Returning from function crashing, only after a certain point in the code
问题描述
我无法弄清楚为什么当我返回时,我的功能会崩溃我的程序。我做了一些调试,喜欢从函数返回导致崩溃的时刻。
void Find_contour $ b {
// VAR
double threshold_val = 128;
int n_erode_dilate = 1;
Mat m = img_complete.clone();
cvtColor(m,m,CV_RGB2GRAY);
blur(m,m,Size(5,5));
threshold(m,m,threshold_val,255,CV_THRESH_BINARY);
erode(m,m,Mat(),Point(-1,-1),n_erode_dilate);
dilate(m,m,Mat(),Point(-1,-1),n_erode_dilate);
矢量<矢量< Point> >轮廓;
vector< Point>点数;
//返回这里是安全的******
findContours(m,contour,CV_RETR_LIST,CV_CHAIN_APPROX_NONE);
//在这之后返回崩溃******
for(size_t i = 0; i for(size_t j = 0; j< contours [i] .size(); j ++){
Point p = contoururs [i] [j]
points.push_back(p);
}
}
vector< vector< Point>> contours_poly(contours.size());
vector< Rect> boundRect(contours.size());
vector< Point2f> center(contours.size());
for(int i = 0; i {
approximatePolyDP(Mat(contoururs [i]),contoururs_poly [i] 3,true);
boundRect [i] = boundingRect(Mat(contoururs_poly [i]));
}
// drawContours(img_contour,contour,idx,colors [idx%4]);
for(int i = 0; i {
Scalar color = Scalar(rng.uniform(0,255),rng。 uniform(0,255),rng.uniform(0,255));
drawContours(drawing,contour,i,color,2,8,CV_RETR_LIST,0,Point());
rectangle(drawing,boundRect [i] .tl(),boundRect [i] .br(),color,2,8,0)
}
}
标记在哪里它是安全的返回和崩溃的地方,所以我假设它与我的载体有关的东西。
我可能只是缺少一些基本的,
编辑:嘿,我已经缩小了 findContours
。通过代码,在安全的地方返回让我到 inline void Mat :: release(
函数,而之后的任何地方findContours
将我的注释为 // TEMPLATE FUNCTION _Destroy_range
,然后转到 void deallocate
图像检测中的0x51995042(ucrtbase.dll)未处理的异常2.exe:传递了无效的参数到一个考虑了无效参数致命的函数。
我还想补充一点,这个错误只发生在函数返回或结束时, findContour
期间的错误。
我对这个整个过程相当新,非常感谢大家的帮助。 / p>
结果是,OpenCV 3.0.0和Visual Studio 2015有一点兼容性问题。
我已经尝试过Visual Studio 2013,一切正常。
感谢您的帮助,谢谢@ Simon Kraemer建议这样做。
I am having trouble figuring out why my function is crashing my program when I return from it. I did a bit of debugging and fond the moment where returning from the function causes it to crash.
void Find_contour()
{
//VAR
double threshold_val = 128;
int n_erode_dilate = 1;
Mat m = img_complete.clone();
cvtColor(m, m, CV_RGB2GRAY);
blur(m, m, Size(5, 5));
threshold(m, m, threshold_val, 255, CV_THRESH_BINARY);
erode(m, m, Mat(), Point(-1, -1), n_erode_dilate);
dilate(m, m, Mat(), Point(-1, -1), n_erode_dilate);
vector<vector<Point> > contours;
vector<Point> points;
//returning here is safe******
findContours(m, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
//returning after here crashes******
for (size_t i = 0; i<contours.size(); i++) {
for (size_t j = 0; j < contours[i].size(); j++) {
Point p = contours[i][j];
points.push_back(p);
}
}
vector<vector<Point>> contours_poly(contours.size());
vector<Rect> boundRect(contours.size());
vector<Point2f> center(contours.size());
for (int i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
boundRect[i] = boundingRect(Mat(contours_poly[i]));
}
//drawContours(img_contour, contour, idx, colors[idx % 4]);
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, CV_RETR_LIST, 0, Point());
rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
}
}
Here is the function, I have labelled where the it is safe to return and where it crashes, so I am assuming it has something to do with my vectors there.
I may just be missing something basic, which is making it hard for me to look up, thanks in advance!
EDIT: Hey I have narrowed it down findContours
. stepping through the code, returning at a safe spot takes me to inline void Mat::release(
function, while anywhere after findContours
takes me something commented as // TEMPLATE FUNCTION _Destroy_range
. which then goes to void deallocate
which at this points causes a break in my program. and has a message.
Unhandled exception at 0x51995042 (ucrtbase.dll) in Image detection 2.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
I would also like to add that this error only occurs when the function returns or ends, and there are no errors during findContour
.
I am fairly new to this whole process, and am very appreciative of everyone's help.
Turns out, there is a bit of a compatibility issue with OpenCV 3.0.0 and Visual Studio 2015.
I have tried it with Visual Studio 2013 and everything works perfectly.
thank you to those who helped, and thank you to @Simon Kraemer for suggesting this.
这篇关于从函数崩溃返回,只有在代码中的某一点之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!