问题
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;
}
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
回答1:
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.
来源:https://stackoverflow.com/questions/61474002/error-when-debug-debug-assertion-failed-vector-subscript-out-of-range