报错:Microsoft C++ 异常: 内存位置 0x0037ed40 处的 std::bad_alloc。急求高手帮忙解决问题。谢谢!
代码大致如下:
IplImage *imgl = cvLoadImage(“E:\2013-09-16testimage\2899.bmp”, 0);
IplImage *imgLl = cvLoadImage(“E:\2013-09-16testimage\2899.bmp”, 3);
IplImage *testImage,*result;
CRsLSM lsm; //定义一个类
char filename[100];
int t;
for (t=2899;t<=3099;t++)
{
sprintf(filename,”E:\2013-09-16testimage\%d.bmp”,t);
IplImage *imgr = cvLoadImage(filename, 0);
IplImage *imgRr = cvLoadImage(filename,3);
char *filenameL=”E:\2013-09-16testimage\2899.bmp”;
char *filenameR=filename;
int *imgL,*imgR;
imgL=new int [imgl->height*imgl->width];
imgR=new int [imgr->height*imgr->width];
imgL=GetGrayValue(filenameL);
imgR=GetGrayValue(filenameR); 当运行到第20次的时候,就报错了
//}catch(const bad_alloc & e){return -1;}
double **m=ReadPoint(“test.txt”);
//cout<<m[0][1]<<” “<<m[0][1]<<endl;
double xl;
double yl;
for (int i=0;i<8;i++)
{
yl=double(m[i][2]);
xl=double(m[i][1]);
//double xl=1017.5;
//double yl=756.;
double xr=0,yr=0;
CvRect rect1;
rect1.x=xl-10,rect1.y=yl-10,rect1.width=20,rect1.height=20;
//CvSize size=cvSize(20,20);
cvSetImageROI(imgl,rect1);
IplImage *templat=cvCreateImage(cvSize(20,20),imgl->depth,imgl->nChannels);
cvCopy(imgl,templat);
CvRect rect;
rect.x=xl-20,rect.y=yl-20,rect.width=40,rect.height=40;
testImage=cvCreateImage(cvSize(40,40),8,0);
cvSetImageROI(imgr,rect);
cvCopy(imgr,testImage);
int testImageW, testImageH, templatW, templatH, resultH, resultW;
testImageW = testImage->width;
testImageH = testImage->height;
templatW = templat->width;
templatH = templat->height;
if(testImageW < templatW || testImageH < templatH)
{
cout <<“原图像不能比模板小” << endl;
return 0;
}
resultW = testImageW – templatW + 1;
resultH = testImageH – templatH + 1;
result = cvCreateImage(cvSize(resultW, resultH), IPL_DEPTH_32F , 1);
cvMatchTemplate(testImage, templat, result, CV_TM_CCORR_NORMED);
double minValue, maxValue;
CvPoint minLoc, maxLoc;
cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc);
xr=maxLoc.x+10+xl-20;
yr=maxLoc.y+10+yl-20;
/*cvLine(imgLl,cvPoint(xl-5,yl),cvPoint(xl+5,yl),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgLl,cvPoint(xl,yl-5),cvPoint(xl,yl+5),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr-5,yr),cvPoint(xr+5,yr),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr,yr-5),cvPoint(xr,yr+5),CV_RGB(255,0,0),1,CV_AA,0);*/
int MatchWindowsHei=5,MatchWindowsWid=5;
double a1=1,a2=0,b1=0,b2=1;
double *cc;
cc=new double [MatchWindowsHei*MatchWindowsWid];
lsm.LSM( xl,yl,xr,yr,MatchWindowsHei,MatchWindowsWid,a1,a2,b1,b2,*cc);
cvLine(imgLl,cvPoint(xl-5,yl),cvPoint(xl+5,yl),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgLl,cvPoint(xl,yl-5),cvPoint(xl,yl+5),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr-5,yr),cvPoint(xr+5,yr),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr,yr-5),cvPoint(xr,yr+5),CV_RGB(255,0,0),1,CV_AA,0);
代码大致如下:
IplImage *imgl = cvLoadImage(“E:\2013-09-16testimage\2899.bmp”, 0);
IplImage *imgLl = cvLoadImage(“E:\2013-09-16testimage\2899.bmp”, 3);
IplImage *testImage,*result;
CRsLSM lsm; //定义一个类
char filename[100];
int t;
for (t=2899;t<=3099;t++)
{
sprintf(filename,”E:\2013-09-16testimage\%d.bmp”,t);
IplImage *imgr = cvLoadImage(filename, 0);
IplImage *imgRr = cvLoadImage(filename,3);
char *filenameL=”E:\2013-09-16testimage\2899.bmp”;
char *filenameR=filename;
int *imgL,*imgR;
imgL=new int [imgl->height*imgl->width];
imgR=new int [imgr->height*imgr->width];
imgL=GetGrayValue(filenameL);
imgR=GetGrayValue(filenameR); 当运行到第20次的时候,就报错了
//}catch(const bad_alloc & e){return -1;}
double **m=ReadPoint(“test.txt”);
//cout<<m[0][1]<<” “<<m[0][1]<<endl;
double xl;
double yl;
for (int i=0;i<8;i++)
{
yl=double(m[i][2]);
xl=double(m[i][1]);
//double xl=1017.5;
//double yl=756.;
double xr=0,yr=0;
CvRect rect1;
rect1.x=xl-10,rect1.y=yl-10,rect1.width=20,rect1.height=20;
//CvSize size=cvSize(20,20);
cvSetImageROI(imgl,rect1);
IplImage *templat=cvCreateImage(cvSize(20,20),imgl->depth,imgl->nChannels);
cvCopy(imgl,templat);
CvRect rect;
rect.x=xl-20,rect.y=yl-20,rect.width=40,rect.height=40;
testImage=cvCreateImage(cvSize(40,40),8,0);
cvSetImageROI(imgr,rect);
cvCopy(imgr,testImage);
int testImageW, testImageH, templatW, templatH, resultH, resultW;
testImageW = testImage->width;
testImageH = testImage->height;
templatW = templat->width;
templatH = templat->height;
if(testImageW < templatW || testImageH < templatH)
{
cout <<“原图像不能比模板小” << endl;
return 0;
}
resultW = testImageW – templatW + 1;
resultH = testImageH – templatH + 1;
result = cvCreateImage(cvSize(resultW, resultH), IPL_DEPTH_32F , 1);
cvMatchTemplate(testImage, templat, result, CV_TM_CCORR_NORMED);
double minValue, maxValue;
CvPoint minLoc, maxLoc;
cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc);
xr=maxLoc.x+10+xl-20;
yr=maxLoc.y+10+yl-20;
/*cvLine(imgLl,cvPoint(xl-5,yl),cvPoint(xl+5,yl),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgLl,cvPoint(xl,yl-5),cvPoint(xl,yl+5),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr-5,yr),cvPoint(xr+5,yr),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr,yr-5),cvPoint(xr,yr+5),CV_RGB(255,0,0),1,CV_AA,0);*/
int MatchWindowsHei=5,MatchWindowsWid=5;
double a1=1,a2=0,b1=0,b2=1;
double *cc;
cc=new double [MatchWindowsHei*MatchWindowsWid];
lsm.LSM( xl,yl,xr,yr,MatchWindowsHei,MatchWindowsWid,a1,a2,b1,b2,*cc);
cvLine(imgLl,cvPoint(xl-5,yl),cvPoint(xl+5,yl),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgLl,cvPoint(xl,yl-5),cvPoint(xl,yl+5),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr-5,yr),cvPoint(xr+5,yr),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr,yr-5),cvPoint(xr,yr+5),CV_RGB(255,0,0),1,CV_AA,0);
//cvRectangle(testImage, maxLoc, cvPoint(maxLoc.x + templatW, maxLoc.y+ templatH), cvScalar(0,0,255));
}
cvNamedWindow(“Right”,0);
cvShowImage(“Right”,imgRr);
//cvNamedWindow(“srcResult”, 0);
//cvShowImage(“srcResult”, testImage);
cvWaitKey(2000);
delete []imgL;
delete []imgR;
imgL=NULL;
imgR=NULL;
cvDestroyAllWindows();
}
cvNamedWindow(“Left”,0); //创建窗体
cvShowImage(“Left”,imgLl);//显示图片
cvReleaseImage(&testImage);
return 0;
解决方案
5
std::bad_alloc 一般的开辟内存失败。
你的 imgL imgR
imgL=new int [imgl->height*imgl->width];
imgR=new int [imgr->height*imgr->width];
new 出来了有根本没有用,不如去掉。
你的 imgL imgR
imgL=new int [imgl->height*imgl->width];
imgR=new int [imgr->height*imgr->width];
new 出来了有根本没有用,不如去掉。
5
本人觉得应该是你的内存不够或碎片太多。像你这样反复NEW再DELETE很容易产生那样的问题。或数组越界。
10
仅供参考,不一定对:
IplImage *imgl = cvLoadImage("E:\2013-09-16testimage\2899.bmp", 0);
IplImage *imgLl = cvLoadImage("E:\2013-09-16testimage\2899.bmp", 3);
IplImage *testImage,*result;
CRsLSM lsm; //定义一个类
char filename[100];
int t;
testImage=cvCreateImage(cvSize(40,40),8,0);
IplImage *templat=cvCreateImage(cvSize(20,20),imgl->depth,imgl->nChannels);
double *cc;
cc=new double [MatchWindowsHei*MatchWindowsWid];
for (t=2899;t<=3099;t++) {
sprintf(filename,"E:\2013-09-16testimage\%d.bmp",t);
IplImage *imgr = cvLoadImage(filename, 0);
IplImage *imgRr = cvLoadImage(filename,3);
char *filenameL="E:\2013-09-16testimage\2899.bmp";
char *filenameR=filename;
int *imgL,*imgR;
imgL=new int [imgl->height*imgl->width];
imgR=new int [imgr->height*imgr->width];
imgL=GetGrayValue(filenameL);
imgR=GetGrayValue(filenameR); //当运行到第20次的时候,就报错bad_alloc
double **m=ReadPoint("test.txt");
//cout<<m[0][1]<<" "<<m[0][1]<<endl;
double xl;
double yl;
for (int i=0;i<8;i++) {
yl=double(m[i][2]);
xl=double(m[i][1]);
//double xl=1017.5;
//double yl=756.;
double xr=0,yr=0;
CvRect rect1;
rect1.x=xl-10,rect1.y=yl-10,rect1.width=20,rect1.height=20;
//CvSize size=cvSize(20,20);
cvSetImageROI(imgl,rect1);
cvCopy(imgl,templat);
CvRect rect;
rect.x=xl-20,rect.y=yl-20,rect.width=40,rect.height=40;
cvSetImageROI(imgr,rect);
cvCopy(imgr,testImage);
cvReleaseImage(&imgr);
int testImageW, testImageH, templatW, templatH, resultH, resultW;
testImageW = testImage->width;
testImageH = testImage->height;
templatW = templat->width;
templatH = templat->height;
if(testImageW < templatW || testImageH < templatH) {
cout <<"原图像不能比模板小" << endl;
return 0;
}
resultW = testImageW - templatW + 1;
resultH = testImageH - templatH + 1;
result = cvCreateImage(cvSize(resultW, resultH), IPL_DEPTH_32F , 1);
cvMatchTemplate(testImage, templat, result, CV_TM_CCORR_NORMED);
double minValue, maxValue;
CvPoint minLoc, maxLoc;
cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc);
cvReleaseImage(&result);
xr=maxLoc.x+10+xl-20;
yr=maxLoc.y+10+yl-20;
/*cvLine(imgLl,cvPoint(xl-5,yl),cvPoint(xl+5,yl),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgLl,cvPoint(xl,yl-5),cvPoint(xl,yl+5),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr-5,yr),cvPoint(xr+5,yr),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr,yr-5),cvPoint(xr,yr+5),CV_RGB(255,0,0),1,CV_AA,0);*/
int MatchWindowsHei=5,MatchWindowsWid=5;
double a1=1,a2=0,b1=0,b2=1;
lsm.LSM( xl,yl,xr,yr,MatchWindowsHei,MatchWindowsWid,a1,a2,b1,b2,*cc);
cvLine(imgLl,cvPoint(xl-5,yl),cvPoint(xl+5,yl),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgLl,cvPoint(xl,yl-5),cvPoint(xl,yl+5),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr-5,yr),cvPoint(xr+5,yr),CV_RGB(255,0,0),1,CV_AA,0);
cvLine(imgRr,cvPoint(xr,yr-5),cvPoint(xr,yr+5),CV_RGB(255,0,0),1,CV_AA,0);
//cvRectangle(testImage, maxLoc, cvPoint(maxLoc.x + templatW, maxLoc.y+ templatH), cvScalar(0,0,255));
}
cvNamedWindow("Right",0);
cvShowImage("Right",imgRr);
//cvNamedWindow("srcResult", 0);
//cvShowImage("srcResult", testImage);
cvWaitKey(2000);
delete []imgL;
delete []imgR;
imgL=NULL;
imgR=NULL;
cvDestroyAllWindows();
cvReleaseImage(imgRr);
}
cvNamedWindow("Left",0); //创建窗体
cvShowImage("Left",imgLl);//显示图片
cvReleaseImage(&testImage);
return 0;