Hi,
I want to find mean value of a mammogram only by choosing the values greater than zero. I used for loops with condition but it gives 255 as answer constantly. I don't know what's the mistake i have done.
Code:
[x,y] = size(I);
s = 0;
for i = 1:x
for j = 1:y
if I(i,j) > 0
s = s + I(i,j);
end
end
end
disp('s = ');
disp(s);

 채택된 답변

Walter Roberson
Walter Roberson 2013년 1월 6일

0 개 추천

Change to
s = s + double(I(i,j));
Question: when you are calculating the mean, are you going to be dividing by the number of values in I, or by the number of non-negative values?
Also are you sure that I will be two-dimensional and not 3 dimensional?

댓글 수: 1

Thank you walter. It works. I am going to divide the sum by no. of non-negative values.
Corrected Code:
[x,y] = size(I);
s = 0;
cnt = 0;
for i = 1:x
for j = 1:y
if I(i,j) > 0
s = s + double(I(i,j));
cnt = cnt + 1;
end
end
end
m = s/cnt;

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2013년 1월 6일

0 개 추천

Why are you doing two loops, which will make it slow? Why not just do:
nonZeroPixels = yourImage > 0;
meanValue = mean(yourImage(nonZeroPixels));
The way above is vectorized, faster, and very MATLABish.
Jan
Jan 2013년 1월 6일

0 개 추천

Alternative method without loops:
s = mean(double(I(I > 0)));

댓글 수: 2

Walter Roberson
Walter Roberson 2013년 1월 6일
mean() works even without the double()
Jan
Jan 2013년 1월 6일
Yes, Walter, because the underlying SUM uses the DOUBLE format as default, when 'native' is not specified. I would prefer to let functions reply the same type as the input as default, therefore I tend to cast more often than needed.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by