I have a C++ program that crashes when I run it. I get the following error:
This may be due to a corruption of the heap, which indicates a bug in test1.exe or any of the DLLs it has loaded.
The program is used to identify the hand through the camera feed and detect the contours of the hand.
Code:
This is the function where the crash occurs:
void showimgcontours(Mat &threshedimg,Mat &original)
{
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
int largest_area = 0;
int largest_contour_index = 0;
**findContours(threshedimg, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);** //this will find largest contour
for (int i = 0; i< contours.size(); i++) // iterate through each contour.
{
double a = contourArea(contours[i], false); // Find the area of contour
if (a>largest_area)
{
largest_area = a;
largest_contour_index = i; //Store the index of largest contour
}
}
//search for largest contour has end
if (contours.size() > 0)
{
//drawContours(original, contours,largest_contour_index, CV_RGB(0, 255, 0), 2, 8, hierarchy);
//if want to show all contours use below one
drawContours(original,contours,-1, CV_RGB(0, 255, 0), 2, 8, hierarchy);
}
}
The bold line(findContours) is where the program breaks.
This is where the function is called:
while ((key = waitKey(30)) != 27)
{
toggle(key);
cap >> frame;
flip(frame, frame, 180);
cvtColor(frame, hsvframe, COLOR_BGR2HSV);
inRange(hsvframe, Scalar(H_MIN, S_MIN, V_MIN), Scalar(H_MAX, S_MAX, V_MAX), rangeframe);
if (domorph)
morphit(rangeframe);
if (doblurthresh)
blurthresh(rangeframe);
if (showcontours)
showimgcontours(rangeframe, frame);
if (showchangedframe)
imshow("Camera", frame);
else
imshow("Camera", rangeframe);
}
}
This function is called when the user presses the key 'c'. I googled for solutions but didn't find anything that helped. Someone please help me!