# type wrong?

    int r = img.rows;
int c = img.cols;
int MaxRadius=0;
int radius_p = 0;
r > c ? MaxRadius=c/2 : MaxRadius = r/2;
int MaxRadius_p = MaxRadius*MaxRadius;
Mat M(MaxRadius, 3, CV_32F, Scalar::all(0));
Mat_<float> & average_p = (Mat_<float>&) M;
for (auto i=0; i<r; ++i)
for(auto j=0; j<c; ++j)
{
radius_p = std::pow((i-r/2),2)+std::pow((j-c/2),2);
if(img.at<Vec2f>(i,j) != 1 && radius_p <=MaxRadius_p)
{
average_p(radius_p, 0) += img.at<Vec2f>(i,j);
average_p(radius_p, 1) += 1;

}
else
continue;
}


I want to calculate the sum and count the number of pixels in an image. but it is very strange, I cannot count the number of pixels.

average_p(radius_p, 1) += 1;


this code did not work, I print them on the terminal, it shows a huge number, like this. 4.7146e+11 4.82613e+11 3.06337e+11 6.53814e+11 5.3117e+11 5.24466e+11 2.63879e+10 7.74249e+10

edit retag close merge delete

## Comments

you don't show, where img comes from, but yes, most probably 'type wrong'.

what are you trying to achieve ? get the mean of the img ? there's a function for that.

img was a power spectrum image, and its type is float. I tried to calculate radial average.

yes, I initialized the array Mat M(MaxRadius, 3, CV_32F, Scalar::all(0)); Mat_<float> & average_p = (Mat_<float>&) M;