How can I find RGB values of a matrix of coordinates of an image?

3 ビュー (過去 30 日間)
Eric Martz
Eric Martz 2019 年 7 月 22 日
コメント済み: Walter Roberson 2019 年 7 月 23 日
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!

採用された回答

Image Analyst
Image Analyst 2019 年 7 月 22 日
(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
  3 件のコメント
Image Analyst
Image Analyst 2019 年 7 月 23 日
Did you see this line in the code:
rgbValues(counter, :) = P(y(row), x(col), :);
That gives you the RGB values. Red is column1, green is col2 and blue is col3.
Eric Martz
Eric Martz 2019 年 7 月 23 日
I figured it out. I edited the code to make this:
x = round(centers(:, 1));
y = round(centers(:, 2));
rgbValues = zeros(length(x), 3);
counter = 1;
for row = 1 : length(x)
rgbValues(counter, :) = P(round(centers(row, 2)), round(centers(row, 1)), :);
counter = counter + 1;
end
pts = [x, y];
rgb = horzcat(rgbValues, pts);
The issue with the code you provided was that it gave the rgb value of every possible combination of every x and y coordinate instead of just the x and y coordinates that went together.

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2019 年 7 月 23 日
rcenters = P( sub2ind(size(P), y, x, ones(size(y))) );

Akira Agata
Akira Agata 2019 年 7 月 23 日
Another possible solution:
row = round(centers(:, 1));
col = round(centers(:, 2));
R = arrayfun(@(x,y) P(x,y,1),row,col);
G = arrayfun(@(x,y) P(x,y,2),row,col);
B = arrayfun(@(x,y) P(x,y,3),row,col);
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 7 月 23 日
Using x for row and y for column is confusing.

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

カテゴリ

Help Center および File ExchangeRead, Write, and Modify Image についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by