【发布时间】: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<vector<point>> 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++ 运行时错误。您会收到一个大消息框,说明“重试调试”。是你做的吗?如果您这样做了,您将通过查看调用堆栈准确地看到错误发生的位置。