always get 255 in sum
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sneha
am 18 Dez. 2017
Kommentiert: Sneha
am 18 Dez. 2017
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 Kommentare
Stephen23
am 18 Dez. 2017
Do NOT use the variable name sum, as sum is the name of a very important inbuilt function.
Akzeptierte Antwort
James Tursa
am 18 Dez. 2017
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!