【发布时间】:2012-05-31 01:24:12
【问题描述】:
我尝试通过将图像划分为块网格并获取每个块的像素总和来对黑白区域的像素值求和。
当我打印每个值时,值都相同 = 255。 我的问题是:为什么会发生这种情况?有些块只有黑色像素,有些块有黑色和白色?
double* divide (Mat I)
{
//double* pointer;
static double* sums= new double [9];
//pointer= sums[0];
//double sums2[10];
Mat block;
//Mat block2;
int numberblocks=9;
int bh;
int bw;
//int bh2;
//int bw2;
bh=I.cols/numberblocks;
bw=I.rows/numberblocks;
//bh2=u.cols/numberblocks;
//bw2=u.rows/numberblocks;
//
double blockarea=bh*bw;
//double num=0;
//double blockarea2=bh2*bw2;
//int r=0;
//int c=0;
//int err=0;
for(int a=0;a<9;a++)
{
for (int r = 0; r < I.rows; r += bw)
{
for (int c = 0; c < I.cols; c += bh)
{
block = I(cv::Range(r, min(r + bw, I.rows)),cv::Range(c, min(c + bh, I.cols)));
}
}
double sum=0;
for(int i=0;i<block.rows;i++)
{
for(int j=0;j<block.cols;j++)
{
sums[a] = sum + block.at<uchar>(i,j); //sums[k-1] + block.at<uchar>(i,j);
}
}
cout<<"sum of"<<a<<"is"<<sums[a]<<endl;
}
return sums;
}
【问题讨论】:
-
如果您希望我们查看,请确保正确格式化您的代码。它看起来越好,就越容易提供帮助。
-
一步步调试代码,检查每个变量的值。你会意识到你没有正确存储这些总和。也许你需要定义另一个变量。
标签: c++ image-processing opencv