调试时出错:调试断言失败:矢量下标超出范围 [英] Error when debug: Debug Assertion Failed: Vector subscript out of range

查看:121
本文介绍了调试时出错:调试断言失败:矢量下标超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hu Moment提取功能来编写用于识别手势的代码,但是当我开始调试时,会出现一条通知,指出Debug Assertion Failed(调试断言失败)!向量下标超出范围.有人告诉我,我的代码中有错误,但我仍然无法解决.请帮我.这是崩溃的代码

I have work on code for recognition hand gesture using Hu Moment extraction feature, but when i start to debug, a notification appears that Debug Assertion Failed! Vector subscript out of range. someone told me that I you have a bug in my code, but I still can't figured it out. Please help me. Here's the crashed code

//function hand image declare and preprocessing image
//function contour detection
//function bounding box
int main()
{

VideoCapture cap(0); //capture the video from web cam

if (!cap.isOpened())  // if not success, exit program
{
    cout << "Cannot open the web cam" << endl;

}
vector<vector<Point> > MyContours = getTestCases();
string array[] = {"V","W","Y"};
while (1)
{
    Mat imgOriginal;

    bool bSuccess = cap.read(imgOriginal); // read a new frame from video
    if (!bSuccess) //if not success, break loop
    {
        cout << "Cannot read a frame from video stream" << endl;
        break;
    }
    Mat send;
    imgOriginal.copyTo(send);
    vector<Point> present_hand_state = detectHand(send);

    double area = contourArea(present_hand_state, false);
    cout << area << endl;
    int a = matchTheState(present_hand_state, MyContours);

    if (a != 100 & area > 500)
        imshow("ImgOriginal", imgOriginal);

    waitKey(23);
}
}

int matchTheState(vector<Point> present_hand_state, vector<vector<Point > > MyContours)
{
vector<double> array(MyContours.size());
int answer = 0;
for (int i = 0; i < MyContours.size(); i++)
{
    double match_value = cv::matchShapes(MyContours[i], present_hand_state, CV_CONTOURS_MATCH_I2, 0);
    array[i] = match_value;
    if (array[answer] > array[i])
        answer = i;
}
if (array[answer] < 0.9)
    return answer;
else
    return 100;
}

,而vector<vector<point>> MyContours的内容是用作比较值的图像声明.

while the contents of the vector<vector<point>> MyContours is an image declaration that is used as a comparison value.

vector<Mat> testCases;
vector<vector<Point> > MyContours;
Mat test = imread("D:\KULIAH\TA\database\F1\frame_v_thres.jpg", CV_LOAD_IMAGE_GRAYSCALE);
testCases.push_back(test);//1
test = imread("D:\KULIAH\TA\database\F1\frame_w_thres.jpg", CV_LOAD_IMAGE_GRAYSCALE);
testCases.push_back(test);//2
test = imread("D:\KULIAH\TA\database\F1\frame_y_thres", CV_LOAD_IMAGE_GRAYSCALE);
testCases.push_back(test);//3

推荐答案

唯一让我跳出来的是,如果MyContours.size()等于零,则if (array[answer] < 0.9)将是超出范围错误的下标.您可以通过将代码更改为

The only thing that's leaping out at me is that if MyContours.size() equals zero then if (array[answer] < 0.9) would be a subscript out of range error. You could test this by changing the code to

if (!array.empty() && array[answer] < 0.9)

但这只是一个理论,这里有太多未知数可以确定.

But it's only a theory, there's too many unknowns here to be sure.

这篇关于调试时出错:调试断言失败:矢量下标超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆