如何从Matlab中的图像获取onclick坐标像素值和位置?
问题描述:
我想获得一些位置(x,y)和点击图像(多个图像之外)后的相应像素值并将其存储在数组中。任何想法如何我可以在Matlab中实现这一点? 例如我点击231,23(X,Y)坐标,它的像素值为,图像为1.jpg。我的数组的前三个元素是231,23,123,1如何从Matlab中的图像获取onclick坐标像素值和位置?
答
您既可以使用ginput
或指定ButtonDownFcn
下,直到你按下回车键将记录点。
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
参见:[按钮向下回调](http://www.mathworks.com/help/matlab/creating_plots/button-down-callback -function.html) – excaza