简体   繁体   English

图像像素的总和

[英]sum of image pixels

I tried to sum pixels values on black and white area, through dividing the image into a grid of blocks and getting the sum of pixels for each block. 我试图通过将图像划分为块网格并获得每个块的像素总和来对黑色和白色区域上的像素值求和。

When I printed the values of each, the values were all the same = 255. My question is: Why does this happen? 当我打印每个的值时,值都是相同的= 255.我的问题是:为什么会发生这种情况? Some blocks have only black pixels and some have black and white? 有些块只有黑色像素,有些块有黑色和白色?

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;

}    

I think this will do 我认为这样做

     for(int i=0;i<block.rows;i++)
     {
         for(int j=0;j<block.cols;j++)
             {

              sum = sum + block.at<uchar>(i,j); //sums[k-1] + block.at<uchar>(i,j);
         }
     }
     sums[a]=sum;

Debug it to be sure. 调试它是肯定的。 Without using sum it would be: 不使用总和,它将是:

     for(int i=0;i<block.rows;i++)
     {
         for(int j=0;j<block.cols;j++)
             {

               sums[a] =  sums[a] + block.at<uchar>(i,j); //sums[k-1] + block.at<uchar>(i,j);
         }
     }         

The row

sums[a] = sum + block.at(i,j); sums [a] = sum + block.at(i,j);

looks strange. 看起来很奇怪 sums[a] has always the value of the last element of block. sums [a]总是块的最后一个元素的值。 If this is by chance always 255, you have your explanation. 如果这是偶然的255,你有你的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM