【问题标题】:sum of image pixels图像像素总和
【发布时间】: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


【解决方案1】:

我觉得这样就可以了

     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;

调试它以确保。如果不使用 sum 它将是:

     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);
         }
     }         

【讨论】:

  • 当我尝试这个时:-' double sum= block.at(0,0); for(int i=0;i(i,j); } 总和[a]+=总和; } cout
【解决方案2】:

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

看起来很奇怪。 sums[a] 始终具有块的最后一个元素的值。如果这偶然总是 255,你有你的解释。

【讨论】:

  • 换句话说,你从来没有设置总和,所以你实际上并没有对任何东西求和。
猜你喜欢
  • 2021-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
  • 2014-06-03
相关资源
最近更新 更多