Summing specific pixel values

1 回表示 (過去 30 日間)
Jason
Jason 2014 年 10 月 22 日
コメント済み: Bjorn Gustavsson 2014 年 10 月 22 日
Hi. I am wanting to find the sum of all the positons xi, yi of an image IM. I thought the following would do it - but it doesn't.
sum(im (xi,yi));
Also, If I want to take this a step further and find the sum of all pixels at locations xi, yi but this time include the surrounding 8 pixels (so its a 3x3 centred on xi,yi), how do I do that?
thanks

採用された回答

Image Analyst
Image Analyst 2014 年 10 月 22 日
You can use a for loop:
m=magic(5) % Whatever...
xi=[2,3,4]
yi = [2,3,4]
theSum = 0;
the8Sum = 0;
for k = 1 : length(xi)
row = yi(k);
col = xi(k);
theSum = theSum + m(row, col);
the8Sum =the8Sum + ...
m(row-1, col-1) + ...
m(row-1, col) + ...
m(row-1, col+1) + ...
m(row, col-1) + ...
m(row, col) + ...
m(row, col+1) + ...
m(row+1, col-1) + ...
m(row+1, col) + ...
m(row+1, col+1);
end
theSum
the8Sum
  2 件のコメント
Jason
Jason 2014 年 10 月 22 日
Thanks, I will need to work out boundary issues.
Bjorn Gustavsson
Bjorn Gustavsson 2014 年 10 月 22 日
I suggest you do something like this instead:
ind1D = sub2ind(size(im),xi,yi);
sumPixxiyi = sum(im(ind1D));
Then for the nearest Neighbour you'd have to make arrays for them too. In my opinion such a solution is so much easier to maintain since it will be so many fewer lines of code that potential speed losses might be preferable.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by