one of arguments value out of range in cvPtr2D 40C

我在做阴影检测,想找到原始图像的一个block作为参考项,比较每个像素和参考项的HSV数值来判断此像素是否是阴影。
在debug的时候出现这个错误:One of arguments' values is out of range(index out of range) in cvPtr2D
这个只在我加了cvSet2D之后出现错误,但是感觉cvSet2D的使用好像没有错误,求大神帮忙看看是什么问题
#include "highgui.h"

#include "cv.h"

#include "stdio.h"
///*#include "cxcore.h"
//#include "cvaux.h" /
#include

///
#include
//#include
//#include
//#include
//#include
//#include
//#include */
#include
using namespace std;
using namespace cv;

int main()
{
//multi原始图像
//himage 原始图像的hsv
//background 原始图像的17*17部分
//hbackground background的hsv

IplImage* multi = cvLoadImage("C:\\11.png"); 
IplImage* himage= cvCreateImage(cvGetSize(multi), IPL_DEPTH_8U, 3);
cvCvtColor(multi, himage, CV_BGR2HSV);
cvShowImage("himage", himage);
IplImage* background = multi;

cvSetImageROI(background, cvRect(300, 300, 17, 17));//设置源图像ROI
cvShowImage("background", background);

IplImage* hbackground = cvCreateImage(cvGetSize(background), IPL_DEPTH_8U, 3);
cvCvtColor(background, hbackground, CV_BGR2HSV);
cvShowImage("hbackground",hbackground);
CvScalar cs;
cs = cvAvg(hbackground);
int csmean = (cs.val[0] + cs.val[1] + cs.val[2]) / 3;
cout << csmean << endl;

double alpha = 0.7;
double beta = 2;
double th1 = 0.04;
double th2 = 0.2;
IplImage* shadow = cvCreateImage(cvGetSize(multi), IPL_DEPTH_8U, 1);
cvShowImage("shadow", shadow);
CvScalar white = 255;
CvScalar zero = 0;
int i = 20;
int j = 20;
double himageh, himages, himagev;
himageh = cvGet2D(himage, i, j).val[0];
himages = cvGet2D(himage, i, j).val[1];
himagev = cvGet2D(himage, i, j).val[2];
cout << himageh << endl;
cout << himages << endl;
cout << himagev << endl;
cout << cs.val[2] << endl;

if ((himagev / (cs.val[2])) > alpha && (himagev / (cs.val[2])) < beta && himageh > th1 && himages > th2)
{
cout << 1 << endl;
//cvSet2D(shadow, i, j, 0);
}
else
{
cout << 0 << endl;
cvSet2D(shadow, i, j, 255);
}
//for (int i = 0; i < multi->height; i++)
//{
// for (int j = 0; j < multi->width; j++)
// {
// if ((cvGet2D(himage, i, j).val[2]) / (cs.val[2]) > alpha && (cvGet2D(himage, i, j).val[2]) / (cs.val[2]) < beta && cvGet2D(himage, i, j).val[0] > th1 && cvGet2D(himage, i, j).val[1] > th2)
// {
// cvSet2D(shadow, i, j, zero);
// }
// else
// {
// cvSet2D(shadow, i, j, white);
// }
// }
//}

cvWaitKey(0);
return 0;
}

1个回答

cvSetImageROI(background, cvRect(300, 300, 17, 17));//设置源图像ROI
cout << "cvGetSize(background): " << cvGetSize(background).width << " " << cvGetSize(background).height << endl;
//在设置了ROI过后,我获取了下宽高
//- 打印: cvGetSize(background): 17 17
//所以下面的是创建了一个hbackground是17*17的小块,后面的shadow也是一样的17*17大小,你的i和j等于20,所以后面cvSet2D就越界了
IplImage* hbackground = cvCreateImage(cvGetSize(background), IPL_DEPTH_8U, 3);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐