opencv代码在循环中出错 80C

我在做把原始图像用一个blocksize*blocksize的block逐个扫描以找到符合判断条件且平均值最大的那个block在原始图像上的坐标。
代码在循环之外可以运行,加进循环之后就一直报错,说是cvSetImageROI之中rect参数问题,但是我找不到问题,求大神指教
#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()
{
IplImage* image = cvLoadImage("C:\11.png"); //
IplImage* image1;
IplImage* gray = NULL;
IplImage* multi = NULL;
IplImage* himage = NULL;
IplImage* hbackground = NULL;
IplImage* shadow = NULL;
cvShowImage("0", image);

CvSize imagesize = cvGetSize(image);


double blocksize;
if (image->height > image->width)
    blocksize = 0.05*image->width;
else
    blocksize = 0.05*image->height;
cout << blocksize << endl;
blocksize = cvFloor(blocksize);
cout << blocksize << endl;
CvScalar meanvalue;
CvScalar sigma;
double meanmax = 0;
double meannum;
double sigmanum;
double blockx = 0;
double blocky = 0;
double threshold1 =6;
double threshold2 = 8;


for (int i = 0; i < (imagesize.height - blocksize-2); i++)
{
    for (int j = 0; j < (imagesize.width - blocksize-2); j++)
    {
        CvRect rect;
        rect.x = i, rect.y = j, rect.width = blocksize, rect.height = blocksize;
        cvSetImageROI(image, rect);//设置源图像ROI


        cvAvgSdv(image, &meanvalue, &sigma);//////error cant fix
        meannum = (meanvalue.val[0] + meanvalue.val[1] + meanvalue.val[2]) / 3;
        sigmanum = (sigma.val[0] + sigma.val[1] + sigma.val[2]) / 3;
        if ( meannum > threshold1 && sigmanum < threshold2 && meannum>meanmax)
        {
            meanmax = meannum;
            blockx = i;
            blocky = j;
        }
        cvResetImageROI(image);

    }

}
cvWaitKey(0);
return 0;

}

3个回答

试试这样

fro()
{
for()
{
cvSetImageROI(image, rect);//设置源图像ROI
//提取ROI
cvCopy(image,image1);

cvAvgSdv(image1, &meanvalue, &sigma);

cvReleaseImage(&image1);
}
}

或者在循环中加入

cvZero(image);

额,是这样的
rect.x = i, rect.y = j, rect.width = blocksize, rect.height = blocksize;
改成:
rect.x =j , rect.y = i, rect.width = blocksize, rect.height = blocksize;
就可以运行成功了

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