I am getting the value 255 all the time in the below loop. P is an lena image of size m*n*3 (m=256,n=256). Actually i need the sum of all pixel values in the entire image. sum=0;
for i=1:3
for j=1:256
for k=1:256
sum = sum+P(j,k,i)
end
end
end
Please help me to solve the problem.

5 件のコメント

KSSV
KSSV 2017 年 12 月 18 日
Hello....you need not to run a loop to get sum...read the documentation of sum carefully. You can specify the dimensions and get the sum at once.
Roger Stafford
Roger Stafford 2017 年 12 月 18 日
There is a Matlab function by the name of 'sum' and therefore you should not be using that name for your variable. It can confuse Matlab.
Sneha
Sneha 2017 年 12 月 18 日
I know about the function sum. But in my work there is an equation as given below. I was trying to implement the same. Thats why i tried that loop.
Stephen23
Stephen23 2017 年 12 月 18 日
Do NOT use the variable name sum, as sum is the name of a very important inbuilt function.
Sneha
Sneha 2017 年 12 月 18 日
ok got it sir

サインインしてコメントする。

 採用された回答

James Tursa
James Tursa 2017 年 12 月 18 日

0 投票

Variable P is likely class uint8. Assuming this is true, the summing is done as uint8 which clips at the highest value of 255 for this class. To get around this, do the sum as double instead. E.g.,
Psum = sum(double(P(:)));
Note that I am calling the MATLAB function sum, and NOT using sum as a variable name.

その他の回答 (0 件)

タグ

質問済み:

2017 年 12 月 18 日

コメント済み:

2017 年 12 月 18 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by