(opencv) 调试断言失败,向量下标超出范围
Posted
技术标签:
【中文标题】(opencv) 调试断言失败,向量下标超出范围【英文标题】:(opencv) debug assertion failed,vector subscript out of range 【发布时间】:2013-08-11 01:51:35 【问题描述】:我编写程序来捕捉人像,但是当我运行这个程序时,它在程序运行大约 10 秒时显示错误。有人可以帮我看看这些程序有什么问题吗?
对不起,如果我的代码看起来很复杂,因为这是我的第一个 OpenCV 程序,那么我尝试在这段代码中编写各种东西。
#include <iostream>
#include <Windows.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
boolean thread_running = false;
LPDWORD threadId;
Mat img,check_end,cimage;
HOGDescriptor hog;
HANDLE setThread = NULL;
Rect r,check;
vector<Rect> found,found_filtered,temp;
int picNo = 1;
int timeCount = 0;
IplImage crop;
boolean dup;
DWORD WINAPI thread(LPVOID lpParam)
thread_running=true;
found_filtered.clear();
crop = img;
hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);
thread_running=false;
return 0;
void draw_bounding_box(Mat img)
size_t i, j;
for (i=0; i<found.size(); i++)
r = found[i];
for (j=0; j<found.size(); j++)
if (j!=i && (r & found[j])==r)
break;
if (j==found.size())
found_filtered.push_back(r);
for (i=0; i<found_filtered.size(); i++)
r = found_filtered[i];
//cvRound = Converts floating-point number to integer
r.x += cvRound(r.width*0.1);
r.width = cvRound(r.width*0.8);
r.y += cvRound(r.height*0.06);
r.height = cvRound(r.height*0.9);
//rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 0);
int main (int argc, const char * argv[])
CvCapture *pCapture =cvCreateFileCapture("MOV00109.avi");
if ( pCapture == NULL )
cout << "ERROR: Failed to open camera" << endl;
return EXIT_FAILURE;
cvSetCaptureProperty( pCapture, CV_CAP_PROP_FRAME_WIDTH, 640 );
cvSetCaptureProperty( pCapture, CV_CAP_PROP_FRAME_HEIGHT, 480 );
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
namedWindow("video capture", CV_WINDOW_AUTOSIZE);
check_end = cvQueryFrame( pCapture );
while (true)
img = cvQueryFrame( pCapture );
if(!thread_running)
CreateThread(NULL,0,thread,&img,0,threadId);
draw_bounding_box(img);
imshow("video capture",img);
if(timeCount >= 3 && 0 < found_filtered.size())
for (int i=0; i<found_filtered.size(); i++)
r = found_filtered[i];
dup = false;
for (int j=0; j<temp.size(); j++)
check = temp[j];
if( check.x == r.x && check.y == r.y)
dup = true;
break;
if(!dup)
cvSetImageROI(&crop,r);
char filename[100];
sprintf(filename,"C%d.jpg",picNo++);
cvSaveImage( filename,&crop);
cvResetImageROI(&crop);
timeCount = 0;
else
timeCount++;
temp = found_filtered;
if(waitKey(100)>0 ) break;
return 0;
那就错了
debug assertion failed
....
...
file:c:\........\vc\include\vector
Line 932
Expression: vector subscript out of range
并且程序在这些地方显示错误
void __cdecl _CRT_DEBUGGER_HOOK(int _Reserved)
/* assign 0 to _debugger_hook_dummy so that the function is not folded in retail */
(_Reserved);
_debugger_hook_dummy = 0;
【问题讨论】:
请避免混合使用 c++ api 和 c api 调用。灾难收据。坚持使用 c++ api (cv::Mat) 并丢弃所有 cv* 函数 【参考方案1】:您从相机捕获的图像是pointing to driver memory。
在将其传递给另一个线程之前,您必须对其进行 clone()。
【讨论】:
谢谢。但是,这些解决方案是否会解决“向量下标超出范围”以上是关于(opencv) 调试断言失败,向量下标超出范围的主要内容,如果未能解决你的问题,请参考以下文章