OpenCV Buffer Overflow Issue... [closed]

asked 2013-06-06 13:13:53 -0600

TonyRo gravatar image

updated 2013-06-06 13:18:04 -0600

I've got myself in a pickle on this project I'm working on. My main objective is to stitch two webcam feeds together and do object detection on them - bounding boxes, etc...the standard stuff.

I can't rid myself of buffer overflows though - the somewhat simplified code below (for readability) compiles x64 and soon after I get a buffer overflow error and this in the console:

OpenCV Error: Assertion Failed (contour.checkVector(2) >= 0 && (contour.depth() == CV_32F || contour.depth() == CV_32S)) in unknown function, file ..\..\..\src\opencv\modules\imgproc\src\contours.cpp, line 1904

If comment out all of the lines that have to do with contours (from findContours to drawBoundingBoxes in main) it compiles and runs fine until I hit the spacebar to stop the program, and then I get another buffer overflow error. I get the same errors when I compile x32 as well, for the record.

Any help? Relevant code/pseudo-code pasted below:

// **defines.h**
//Definitions for anything in all caps, like WIDTH, HEIGHT, ERODEIT, etc...

// **protos.h**
// All function prototypes, nothing else

// **detection.cpp**

/* This is the code that related to background subtraction operations*/

#include <opencv2/opencv.hpp>
#include <iostream>
#include "defines.h"

using namespace std;
using namespace cv;

void initBackgroundSubtractor(BackgroundSubtractorMOG2 &bSub)
{
    bSub.set("detectShadows", 1);
}

Mat doBackgroundSubtract(BackgroundSubtractorMOG2 &bSub, Mat panorama)
{
    Mat foreground;

    bSub.operator()(panorama, foreground);
    erode(foreground, foreground, Mat(), Point(-1, -1), ERODEIT, BORDER_DEFAULT);
    dilate(foreground, foreground, Mat(), Point(-1, -1), DILATEIT, BORDER_DEFAULT);

    return foreground;
}

// **contourOps.cpp**

/* Functions that operate on, filter, or relate to OpenCV contours vectors */

#include <opencv2/opencv.hpp>
#include <vector>
#include <fstream>
#include "defines.h"

using namespace std;
using namespace cv;

/* Returns the centroid of a contour */

Point getCentroid(vector<Point> contour)
{
    Point centroid;
    Moments m;

    m = moments(contour, false);
    centroid.x = int(m.m10/m.m00);
    centroid.y = int(m.m01/m.m00);

    return centroid;
}

/* Draws a rectangle around a contour */

void drawBoundingBoxes(vector<vector<Point>> contours, Mat &img)
{
    vector<Rect> boundRect(contours.size());

    for(unsigned int j = 0; j < contours.size(); j++)
    {
        boundRect[j] = boundingRect(contours[j]);
        rectangle(img, boundRect[j], Scalar(153,0,76), 2, 8, 0);
    }
}

/* Removes contours from a vector if they're smaller than the argument "area" */

void contourSizeTrim (vector<vector<Point>> &contours, int area)
{
    vector<vector<Point>>::iterator i = contours.begin();
    while(i != contours.end())
    {
        if(contourArea(*i, false) < area)
            i = contours.erase(i);
        else
            i++;
    }
}

/* Removes contours from a vector if they're X % smaller than largest contour in vector */

void contourRelSizeTrim(vector<vector<Point>> &contours, int percent)
{
    double maxArea = 0.0;

    for(unsigned int i=0; i<contours.size(); i++)
    {
        if (contourArea(contours[i], false) > maxArea)
                maxArea = contourArea(contours[i], false);
    }

    vector<vector<Point>>::iterator j = contours.begin();
    while(j != contours.end())
    {
        if (contourArea(*j, false) < (double)(percent/100.0)*maxArea)
            j = contours.erase(j);
        else
            j++;
    }
}

// **realtimestitch.cpp**

#include <opencv2/opencv.hpp>
#include <opencv2/stitching/stitcher.hpp>
#include <vector>
#include <iostream>
#include "defines.h"

using namespace std;
using namespace cv;

void initStitcher(VideoCapture &capture1, VideoCapture &capture2, Stitcher ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-11-13 03:50:58.074207

Comments

whoa WALL OF CODE, again

berak gravatar imageberak ( 2013-06-06 13:18:28 -0600 )edit

Sorry, was unsure of any other way to properly portray my problem with all of the relevant information.

TonyRo gravatar imageTonyRo ( 2013-06-06 13:31:26 -0600 )edit

i'll promise to look at it again, tomorrow, but, honestly, a 50 line testcase has more chances to get an answer ...

berak gravatar imageberak ( 2013-06-06 14:02:42 -0600 )edit

I've narrowed it down a bit if this helps:

The entire program runs fine (i.e. the stitching works, I can see "pano", and if I imshow the foreground mask "fGround" it looks fine as well) if I just comment out drawBoundingBoxes in main.cpp until I hit spacebar, or whatever, and then I get a runtime error (as mentioned in my original post) from triggering waitKey. So there's some problem with waitKey (I don't understand how this is possible at all) and another from drawBoundingBoxes().

TonyRo gravatar imageTonyRo ( 2013-06-07 09:35:01 -0600 )edit