Stack around the variable 'hull_header' was corrupted when using convexity defects [closed]
Hello ! I am trying to develop a hand recognition system. I am actually using convex hull but the problem is that i am getting the error Stack around the variable 'hull_header' was corrupted and the error come from this piece of code :
void findConvexityDefects(vector<Point>& contour, vector<int>& hull, vector<Point>& convexDefects){
if (hull.size() > 0 && contour.size() > 0){
CvSeq* contourPoints;
CvSeq* defects;
CvMemStorage* storage;
CvMemStorage* strDefects;
CvMemStorage* contourStr;
CvConvexityDefect *defectArray = 0;
strDefects = cvCreateMemStorage();
defects = cvCreateSeq(CV_SEQ_KIND_GENERIC | CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), strDefects);
// transform our vector<Point> into a CvSeq* object of CvPoint.
contourStr = cvCreateMemStorage();
contourPoints = cvCreateSeq(CV_SEQ_KIND_GENERIC | CV_32SC2, sizeof(CvSeq), sizeof(CvPoint),contourStr);
for (int i = 0; i<(int)contour.size(); i++) {
CvPoint cp = { contour[i].x, contour[i].y };
cvSeqPush(contourPoints, &cp);
}
// do the same thing with the hull index
int count = (int)hull.size();
int* hullK = (int*)malloc(count*sizeof(int));
for (int i = 0; i<count; i++){hullK[i] = hull.at(i); }
CvMat hullMat = cvMat(1, count, CV_32SC1, hullK);
// calculate convexity defects
storage = cvCreateMemStorage(0);
defects = cvConvexityDefects(contourPoints, &hullMat, storage);
defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*defects->total);
cvCvtSeqToArray(defects, defectArray, CV_WHOLE_SEQ);
// store defects points in the convexDefects parameter.
for (int i = 0; i<defects->total; i++){
CvPoint ptf;
ptf.x = defectArray[i].depth_point->x;
ptf.y = defectArray[i].depth_point->y;
convexDefects.push_back(ptf);
}
// release memory
cvReleaseMemStorage(&contourStr);
cvReleaseMemStorage(&strDefects);
cvReleaseMemStorage(&storage);
}
}
Here's the whole code :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <opencv2\opencv.hpp>
#include <vector>
#include <cmath>
#include <opencv2/features2d/features2d.hpp>
#include <opencv\highgui.h>
#include <opencv2\imgproc\imgproc_c.h>
#include <stdarg.h>
using namespace cv;
using namespace std;
/////////////////////////////////////////////////////////////
/// Global Variables
int filterKernelSize = 15; // size of median filter, need to be odd number
int resultDigit; // the outcome of the recognition algorithm
Mat src; Mat srcSm;
/// Function headers
int getContourAndHull(cv::Mat);
vector<int> elimNeighborHulls(vector<int>, vector<Point>); // to remove neighbor hulls
vector<int> filterHulls(vector<int>, vector<Point>, RotatedRect); // to remove hulls below a height
vector<int> filterHulls2(vector<int>, vector<Point>, vector<Point>, RotatedRect); // to further remove hulls around palm
vector<Point> filterDefects(vector<Point>, RotatedRect); // to remove defects below a height
void findConvexityDefects(vector<Point>&, vector<int>&, vector<Point>&);
void display(char*, cv::Mat);
/////////////////////////////////////////////////////////////
// void cvShowManyImages(char* title, int nArgs, ...);
int main(int argc, char** argv)
{
int i, j;
int n;
double handDepth = 24;
double maxVal1 = 0, minVal = 0;
unsigned char data = 90;
int lastNum = 0;
//Mat image;
Mat num1, num2, num3, num4, num5, num0;
const char* inFileSrc1 = "C:/Users/HDR/Documents/Visual Studio 2010/Projects/openCV_tutorial/1.png";
const char* inFileSrc2 = "C:/Users/HDR/Documents/Visual Studio 2010/Projects/openCV_tutorial/2.png";
const char* inFileSrc3 = "C:/Users/HDR/Documents/Visual Studio 2010/Projects/openCV_tutorial/3.png";
const char* inFileSrc4 = "C:/Users/HDR/Documents/Visual Studio 2010/Projects/openCV_tutorial/4.png";
const char* inFileSrc5 = "C:/Users/HDR/Documents/Visual Studio 2010/Projects/openCV_tutorial/5.png";
const char* inFileSrc0 = "C:/Users/HDR/Documents/Visual Studio 2010/Projects/openCV_tutorial ...
Throw away that old deprecated C code and start by using the (not so) new C++ interface
@LorenaGdL , lol, unfortunately, this is the opencv library code ;)
@berak ouch, big fail lol. Apologies to the OP
could you share your code and a sample image? or preferably could you give values of
vector<Point>& contour
that you called the function findConvexityDefects withI have edited the question and shared the code