我试图通过将图像划分为块网格并获取每个块的像素总和来对黑白区域的像素值求和。
当我打印每个值时,值都相同 = 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;
}