0
void trackFilteredObject(int &x, int &y, Mat threshold, Mat &cameraFeed) {
Mat temp;
threshold.copyTo(temp);
//these two vectors needed for output of findContours
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
//find contours of filtered image using openCV findContours function
findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//use moments method to find our filtered object
double refArea = 0;
int largestIndex = 0;
bool objectFound = false;
if (hierarchy.size() > 0) {
int numObjects = hierarchy.size();
//if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
if (numObjects<MAX_NUM_OBJECTS) {
for (int index = 0; index >= 0; index = hierarchy[index][0]) {
Moments moment = moments((cv::Mat)contours[index]);
double area = moment.m00;
//if the area is less than 20 px by 20px then it is probably just noise
//if the area is the same as the 3/2 of the image size, probably just a bad filter
//we only want the object with the largest area so we save a reference area each
//iteration and compare it to the area in the next iteration.
if (area>MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea) {
x = moment.m10 / area;
y = moment.m01 / area;
objectFound = true;
refArea = area;
//save index of largest contour to use with drawContours
largestIndex = index;
}
else objectFound = false;
}
//let user know you found an object
if (objectFound == true) {
putText(cameraFeed, "Tracking Object", Point(0, 50), 2, 1, Scalar(0, 255, 0), 2);
//draw object location on screen
drawObject(x, y, cameraFeed);
//draw largest contour
//drawContours(cameraFeed, contours, largestIndex, Scalar(0, 255, 255), 2);
}
}
else putText(cameraFeed, "TOO MUCH NOISE! ADJUST FILTER", Point(0, 50), 1, 2, Scalar(0, 0, 255), 2);
}
Whenever I compile my code with this function, this problem appears, heap corruption. i want to track an object and draw a geometric figure on it
Then, this error appears at runtime, only when I use this function. debugging the code, the error occurs when I leave the function. From what I’ve seen, something is happening with the use of vectors that I use as a parameter...
Did the error occur at runtime or build time? When you debug, on which line of code does the error appear? If it’s only when you use this function, is it connected to the Opencv dependency or the function itself? I mean, does your code already work with other things from Opencv? One more thing: are you using the Opencv libraries compiled for VS 2015 and for the correct platform (32 bit x 64 bit)? Please edit the question to inform these details.
– Luiz Vieira
Then, this error appears at runtime, only when I use this function. debugging the code, the error occurs when I leave the function. From what I’ve seen, something is happening with the use of vectors that I use as a parameter... I managed to run other things with the library without problems, only with this function findCountors I find this kind of problem. worse he is of great help to the application I’m wanting. and before I forget, thanks Luiz Vieira
– Thiago Rossato
Well, I did a test here in my environment (the way I could, since your code does not compile by the lack of a lot of things - constants, another function, definition of which Threshold is used, etc.), and did not generate the same error. I insist it may be some problem with your Opencv compilation.
– Luiz Vieira
If you prepare a minimal and complete example that is easily compiled into a separate project, I test here again. But I think it will work as well. Did you test the Opencv example in your environment? http://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html
– Luiz Vieira