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[xrand] = inp[x];
}
}
xrand++; // increment image column
}
yrand++; // increment image row
xrand = ximg; // reset image column to beginning column of next row
}
// reset xrand and yrand to original values
xrand = rand()%480 - 80; // random col position for figure
yrand = rand()%640 - 100; // random row position for figure
ximg = xrand;
yimg = yrand;
// loop through white rectangle where y is the row and x is the column
// rows 0-199 and cols 0-99
for( int y=0; y < 200; y++ )
{
inp= rct.ptr<uchar>(y); // pointer to current pixel in square
for( int x=0; x < 160; x++ )
{
xdiff = float(x - 80); // column difference from current pixel to center
ydiff = float(y - 100); // 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 (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[xrand] = inp[x];
}
}
xrand++; // increment image column
}
yrand++; // increment image row
xrand = ximg; //reset image column to beginning column of next row
}
int height = img.rows; // find height of image
int width = img.cols; // find width of image
chanl = img.channels(); // find number of channels in image
imgdepth = img.depth(); // find depth of image
// display calculated values for image in console window
cout << "The image channel is: " << chanl << endl;
cout << "The image depth is: " << imgdepth << endl;
cout << "The image height is: " << height << endl;
cout << "The image width is: " << width << endl;
cout << "The minimum intensity of the square is: " << minint << endl;
cout << "The maximum intensity of the square is: " << maxint << endl;
namedWindow("Example1", CV_WINDOW_NORMAL);
imshow("Example1", img);
waitKey(0);
return 0;
}