【问题标题】:Error when debug: Debug Assertion Failed: Vector subscript out of range调试时出错:调试断言失败:向量下标超出范围
【发布时间】:2020-04-28 06:30:03
【问题描述】:

我正在编写使用 Hu Moment 提取功能识别手势的代码,但是当我开始调试时,会出现一条通知,提示 Debug Assertion Failed!向量下标超出范围。有人告诉我,我的代码中有错误,但我仍然无法弄清楚。请帮我。这是崩溃的代码

//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&lt;vector&lt;point&gt;&gt; MyContours 的内容是用作比较值的图像声明。

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

【问题讨论】:

  • 有人告诉我,我的代码中有错误,但我仍然无法弄清楚 -- 错误消息“向量下标超出范围”是Visual C++ 运行时错误。您会收到一个大消息框,说明“重试调试”。是你做的吗?如果您这样做了,您将通过查看调用堆栈准确地看到错误发生的位置。

标签: c++ vector opencv3.0


【解决方案1】:

唯一让我感到震惊的是,如果MyContours.size() 等于零,那么if (array[answer] &lt; 0.9) 将是下标超出范围错误。您可以通过将代码更改为

来测试它
if (!array.empty() && array[answer] < 0.9)

但这只是一个理论,这里有太多未知数无法确定。

【讨论】:

    猜你喜欢
    • 2017-06-18
    • 2021-12-22
    • 2016-02-14
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2021-03-13
    相关资源
    最近更新 更多