Ask Your Question

Heap error OpenCV 2.4.9

asked 2014-09-14 08:20:19 -0500

rogeralms gravatar image

updated 2014-09-14 09:16:53 -0500

Hi, I am a little rusty in C++. I am trying to display a black background gray-scale image with a square and rectangle placed randomly within the image. The square and rectangle have a white center and progressively get darker towards the edges. Displaying the square worked fine but when I added the rectangle, I am now getting a heap error when I press enter to close the display window.

Here is what is diplayed when the system breakpoint occurs:

extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer( const void * pUserData ) { if (!pUserData) return FALSE;

    if (!_CrtIsValidPointer(pHdr(pUserData),

sizeof(_CrtMemBlockHeader), FALSE)) return FALSE;

    return HeapValidate( _crtheap, 0, pHdr(pUserData) ); }

My code is below. Can someone point me in the right direction to correct this error?

Thank you.

include opencv\cv.h
include opencv\highgui.h
include cmath
include ctime

using namespace cv;
using namespace std;

int main()
    int chanl;                              // number of channels
    int imgdepth;                               // image depth
    float xdiff;            // difference between current pixel and center of figure cols
    float ydiff;            // difference between current pixel and center of figure rows
    float totdiff;              // use 2-d distance formula sqrt of sum of squares
    float two = 2.0;
    float xsqr;                         // square of col difference
    float ysqr;                         // square of row difference
    srand( time(0));                // needed to ensure true randomness for rand
    int xrand = rand()%480 - 50;                // random col position for figure
    int yrand = rand()%640 - 50;                // random row position for figure
    int ximg = xrand;
    int yimg = yrand;
    int minint = 255;                       // check for minimum intensity
    int maxint = 0;                         // check for max intensity  
    uchar* inp;                         // uchar* pointer for figure
    uchar* otp;                         // uchar* pointer for image

    Mat img = Mat::zeros(640,480, CV_8UC1);     // black background image
    Mat sqr = Mat::ones(100,100, CV_8UC1);      // white square 100x100 pixels
    Mat rct = Mat::ones(200,100, CV_8UC1);      // white rectangle 200x100 pixels
    Mat cir = Mat::zeros(70,70, CV_8UC1);   // black square to hold circle of radius 70 pixels

    // loop through white square where y is the row and x is the column
    // rows 0-99 and cols 0-99
    for( int y=0; y < 100; y++ ) 
        inp= sqr.ptr<uchar>(y);             // pointer to current pixel in square
        for( int x=0; x < 100; x++ ) 
            xdiff = float(x - 50);      // column difference from current pixel to center
            ydiff = float(y - 50);      // row difference from current pixel to center
            xsqr = pow(xdiff,two);              // square of column difference
            ysqr = pow(ydiff,two);              // square of row difference
            totdiff = sqrt( xsqr + ysqr);       // 2-d planar distance
            inp[x] = uchar(255 - int(totdiff)); 
                                                 // set center to white (ie totdiff = 0) and
                        // progressively get darker as you move from center
            if (inp[x] < minint)        // capture the minimum intensity of the square
                minint = inp[x];
            if (inp[x] > maxint)        // capture the maximum intensity of the square
                maxint = inp[x];
            if (yrand >= 0 && yrand < 640)      
// test row boundary in case square goes beyond edge of image
                if (xrand >= 0 && xrand < 480)  
// test col boundary in case square goes beyond edge of image
                    otp= img.ptr<uchar>(yrand); 
// if inside image, overlay intensity with value from square
                    otp ...
edit retag flag offensive close merge delete



Mat rct = Mat::ones(200,100, CV_8UC1); // yet you run the x-loop from 0..160 -> boom.

please, try to run a debug build. most asserts are ignored in release.

berak gravatar imageberak ( 2014-09-14 08:28:46 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-09-14 10:03:09 -0500

rogeralms gravatar image

Thank you so much! I copied that statement from the previous one and forgot to change the array boundaries.

edit flag offensive delete link more

Question Tools


Asked: 2014-09-14 08:20:19 -0500

Seen: 538 times

Last updated: Sep 14 '14