如何在Matlab中从图像中获取onclick坐标像素值和位置?

时间:2016-03-01 17:56:42

标签: matlab

我希望在点击图像(多个图像中)之后得到一些位置(x,y)和相应的像素值,并将其存储在一个数组中。如何在Matlab中实现这一点? 例如,我点击 231,23 (X,Y)坐标,它的像素值为 123 ,图像为1.jpg。我的前三个数组元素是 231,23,123,1

1 个答案:

答案 0 :(得分:1)

您可以使用ginput或指定ButtonDownFcn

以下内容将记录点,直至您按Enter键。

fig = figure();
img = rand(50);

imshow(img)

[x,y] = ginput();

% Get the pixel values
data = img(sub2ind(size(img), round(y), round(x)));

以下是使用ButtonDownFcn回调

的示例
fig = figure();
img = rand(50);

hax = axes('Parent', fig);
him = imshow(img, 'Parent', hax);

% Specify the callback function
set(him, 'ButtonDownFcn', @(s,e)buttonDown(hax, img))

function buttonDown(hax, img)
    % Get the location of the current mouse click
    currentPoint = get(hax, 'CurrentPoint');
    currentPoint = round(currentPoint(1,1:2));

    % Retrieve the pixel value at this point
    data = img(sub2ind(size(img), currentPoint(2), currentPoint(1)));

    % Print the data to the command window.
    fprintf('x: %0.2f, y: %0.2f, pixel: %0.2f\n', ...
            currentPoint(1), currentPoint(2), data);
end