MATLAB: How to find RGB values of a matrix of coordinates of an image

image analysisimage processing

Hi,
I'm trying to pull the RGB values from a list of (x,y) coordinates that are centers of circles. My plan was to get the matrix of each component (r, g, and b) this way:
P = imread('image'); ...
x = centers(:, 1);
y = centers(:, 2);
rcenters = P(round(x), round(y), 1);
x is a 668 x 1 matrix, and so is y, so I was hoping rcenters would be 668 x 2, but instead it's 668 x 668, which does not make sense to me. I had to round the x and y coordinates to the nearest integer so that it would have integer indices.
How could I get it to return a 668 x 2 matrix with the R value of the x coordinate in column 1 and the R value of the y coordinate in column 2?
Thanks in advance!

Best Answer

(x,y) is NOT (row, column) -- it's (column, row). You've made a very common beginner mistake. Put y first -- P(y, x)
x = round(centers(:, 1));
y = round(centers(:, 2));
rgbValues = zeros(length(x), 3)
counter = 1;
for col = 1 : length(y)
for row = 1 : length(x)
rgbValues(counter, :) = P(y(row), x(col), :);
counter = counter + 1;
end
end