Question

I'm coding a function that does some image processing with OpenCV 2.4.3 and I'm using InputArray and OutputArray as parameter types. In a Debug build using Visual Studio 2008 Express, the code bellow (minimal test case) gives me this very weird error after the first iteration of the for(;;) loop:

"HEAP[Test.exe]: Heap block at 0032F598 modified at 0032F5D0 past requested size of 30 Windows has triggered a breakpoint in Test.exe. This may be due to a corruption of the heap, which indicates a bug in Test.exe or any of the DLLs it has loaded. This may also be due to the user pressing F12 while Test.exe has focus."

struct Corner
{
    float x;
    float y;
    float response;
};


void my_CornerDetector(InputArray _image, OutputArray _corners)
{
    vector<Corner> corners;
    Corner c;
    c.x = 150; c.y = 200; c.response = 0.1485;
    corners.push_back(c);
    corners.push_back(c);
    corners.push_back(c);
    Mat(corners).copyTo(_corners);
}

void main()
{
    Mat frame, frame_gray;    
    namedWindow("Output", CV_WINDOW_AUTOSIZE ); 

    VideoCapture capture;
    capture.open(0);
    for (;;)
    {
        capture >> frame;
        if (frame.empty())
            break;

        cvtColor( frame, frame_gray, CV_BGR2GRAY );

        vector<Corner> corners;
        my_CornerDetector( frame_gray, corners);

        for( int i = 0; i < corners.size(); i++ )
            circle( frame, Point2f(corners[i].x, corners[i].y), 4, CV_RGB(255,0,0), -1, 8, 0 ); 

        imshow("Output", frame);

        char key = (char)waitKey(2);
        switch (key)
        {
        case 27:
        case 'q':
          return;
          break;
        }
    }
}

Notice that corners had the right values (3 times the same corner) within the for loop during the first iteration and before the crash. If instead of using my Corner type for the std::vector I use a Point2f (and I forget about the response field) the example works as expected. And if instead of using a OutputArray I use std::vector &_corners it also works fine. What's going on?? I get the feeling maybe I need to register my type Corner somewhere so that it can work with OutputArray but I haven't seen such a thing in the OpenCV documentation.

BTW, I didn't press F12 ;-)

Was it helpful?

Solution

The problem lies with passing in the Vector corners. It doesn't get cast to OutputArray.

Replace the block:

vector<Corner> corners;
my_CornerDetector( frame_gray, corners);    
for( int i = 0; i < corners.size(); i++ )
circle(frame, Point2f(corners[i].x, corners[i].y), 4, CV_RGB(255,0,0), -1, 8, 0 );

with:

Mat corners;
my_CornerDetector( frame_gray, corners);        
for( int i = 0; i < corners.rows; i++ )
{
  Corner c = corners.at<Corner>(i, 0);
  circle(frame, Point2f(c.x, c.y), 4, CV_RGB(255,0,0), -1, 8, 0 );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top