2016-03-01 152 views
-1

我想獲得一些位置(x,y)和點擊圖像(多個圖像之外)後的相應像素值並將其存儲在數組中。任何想法如何我可以在Matlab中實現這一點? 例如我點擊231,23(X,Y)座標,它的像素值爲,圖像爲1.jpg。我的數組的前三個元素是231,23,123,1如何從Matlab中的圖像獲取onclick座標像素值和位置?

+0

參見:[按鈕向下回調](http://www.mathworks.com/help/matlab/creating_plots/button-down-callback -function.html) – excaza

回答

1

您既可以使用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