Correctly sum image values

29 ビュー (過去 30 日間)
Andy
Andy 2013 年 11 月 1 日
コメント済み: Image Analyst 2013 年 11 月 4 日
I have a standard rob image
im = imread('myIm.jpg');
I want to access values within this image so that i can calculate the average for different regions. The coordinates for these rejoins are held in variables xx and yy. I have tried averaging the red, green, blue values from the image as below but instead of the sum returning a single value it returns an array of doubles. xx and yy are of size 1x1028 which is the size of the average i get back.
avRed = sum(im(xx,yy,1))/el;
avGre = sum(im(xx,yy,2))/el;
avBlu = sum(im(xx,yy,3))/el;
I want to sum up all the image values at points xx and yy, and then divide them by the number of elements el, and have the average colour value returned as a single value between 0-255 and not an array. Could anyone advise me on where i have gone wrong.

採用された回答

sixwwwwww
sixwwwwww 2013 年 11 月 1 日
編集済み: sixwwwwww 2013 年 11 月 1 日
Dear Andy, try this:
avRed = sum(sum(im(xx,yy,1)))/el;
avGre = sum(sum(im(xx,yy,2)))/el;
avBlu = sum(sum(im(xx,yy,3)))/el;
Since in your case you have a matrix so you have to sum twice because in first summation MATLAB calculates the sum of each column individually and stores them in a row vector and in the second summation it sum this row vector which is your desired summation.
I hope it helps. Good luck!
  5 件のコメント
Andy
Andy 2013 年 11 月 4 日
Yes that makes sense, i used flipud on the image which meant that the answer i selected worked for me.
Image Analyst
Image Analyst 2013 年 11 月 4 日
Flipping up/down is not going to make it magically work. It's a completely different concept. Evidently I didn't explain it well enough because you don't understand how im(xx,yy,1) gives you 4 pixel values instead of 2 pixel values.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 1 日
編集済み: Azzi Abdelmalek 2013 年 11 月 1 日
n=size(im);
m=ones(size(xx));
avRed = sum(im(sub2ind(n,xx,yy,m)))/el;
avGre = sum(im(sub2ind(n,xx,yy,2*m)))/el;
avBlu = sum(im(sub2ind(n,xx,yy,3*m)))/el;

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by