在 MATLAB 中检测图像特定 x 和 y 坐标的颜色

Detecting colour of a specific x and y coordinate of an image in MATLAB

我正在尝试获取图像上特定坐标的颜色。例如:X 是大小为 width:300 和 height:300 的图像。我想知道 x:10 和 y:10 坐标处的颜色。我无法编写任何代码,因为我不知道这在 matlab 中是否可行?

谢谢。

你当然可以在MATLAB上做这样的操作:

A = imread('yourimage.png');

X = 10;
Y = 10;

color = A(Y, X, :);
color = squeeze(color);

color 将是包含该像素的 RGB 值(从 0 到 255)的行向量。当然 Y 图像轴指向与笛卡尔轴相反的经文。

如果您想在 Matlab 中通过手动选择像素来获取颜色,您可以这样做:

A = imread('Lena.png');
imshow(A);
[x,y] = ginput(1); % Select a point on the plot
x = fix(x); y = fix(y); % Fix to nearest pixel
hold on;
plot(x,y,'bo'); % Plot the point
color = squeeze(A(x,y,:))'; % Get the color

然后甚至使用 that 之类的东西将其转换为它的名称。在这个特定的例子中,代码可能是这样的:

A = imread('Lena.png');
h = figure;
imshow(A);
while true
    [x,y,key] = ginput(1);
    x = fix(x); y = fix(y);
    hold on;
    plot(x,y,'bo');
    color = squeeze(A(x,y,:))';
    name = rgb2name(double(color)/256);
    disp(name);
    if key == 27; break; end; % key == escape
end
close(h);

玩得开心! ;)