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.

 Akzeptierte Antwort

sixwwwwww
sixwwwwww am 1 Nov. 2013
Bearbeitet: sixwwwwww am 1 Nov. 2013

1 Stimme

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 Kommentare

Andy
Andy am 1 Nov. 2013
Bearbeitet: Andy am 1 Nov. 2013
Thank you, this seems to give me the correct answer now. Azzi, i haven't tried yours yet but i will also take a look at your solution.
As a side question, i want to plot the RGB values that i now have. These are obviously in the range 0-255 currently. Is there a way to plot them like this, as looking at the matlab colours the rgb values should be represented between 0-1
I have divided my RGB values by 255 to get a value between 0 and 1. Is this the correct way?
I don't think this answer is correct, check
im(xx,yy,1)
and
n=size(im);
m=ones(size(xx));
im(sub2ind(n,xx,yy,m))
Azzi is absolutely correct. sixwwwwwwwww made a common beginner mistake, and Andy accepted the wrong answer. See this code to illustrate:
ma=[...
4 2 9 4
1 3 9 9
3 4 5 4
2 1 5 2];
im=cat(3,ma,ma,ma) % Make it 3D, each plane the same
% Specify row 2, col1, and row 3, col 4.
xx = [2,3]; % rows, not horizontal values!!!!!
yy = [1,4];
% Note im(xx(1), yy(1), 1) = im(2, 1, 1) = 1
% Note im(xx(2), yy(2), 1) = im(3, 4, 1) = 4
% The sum would be 5, and average would be 2.5
% WARNING: xx IS ROWS, NOT COLUMNS, like you'd think!
% AND YY IS NOT ROWS (THE VERTICAL DIRECTION)-
% IT'S THE COLUMN (HORIZONTAL DIRECTION).
values = im(xx,yy,1)
el = numel(xx)
% Azzi's code:
n=size(im);
m=ones(size(xx));
avRed = sum(im(sub2ind(n,xx,yy,m)))/el
% sixwwwwwwwwww code (doesn't work)
avRed = sum(sum(im(xx,yy,1)))/el
Andy also made a very common beginner mistake (though Azzi's code works assuming xx is rows and yy is columns). The mistake is thinking x,y = row, column. IT DOES NOT! You normally think x is the horizontal coordinate and y is the vertical coordinate. But matrices don't work that way. The first dimension is the rows - the vertical coordinate, which is y. Precisely the opposite of what one might think! So when you say im(xx,yy), xx is the row/vertical/ y coordinate, and yy is the column/horizontal/ x coordinate. So column,row = y, x, so if you have an array of x's (columns) and an array of y's (rows), you'd do this: im(y, x). You would NOT do i(x,y). Andy, does that make sense, because it's very important, and a common beginner mistake, and sometimes even trips up experienced people.
Andy
Andy am 4 Nov. 2013
Yes that makes sense, i used flipud on the image which meant that the answer i selected worked for me.
Image Analyst
Image Analyst am 4 Nov. 2013
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 1 Nov. 2013
Bearbeitet: Azzi Abdelmalek am 1 Nov. 2013

1 Stimme

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;

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by