I want to get some location(x,y) and corresponding pixel value after clicking on image (out of multiple images) and store it in an array . Any ideas how i can achieve this in Matlab ? For example I click on 231,23 (X,Y) coordinates and it has pixel value of 123 and image is 1.jpg. my first three elements of array would be 231 , 23 , 123 , 1
Asked
Active
Viewed 832 times
1 Answers
1
You could either use ginput
or specify a ButtonDownFcn
The following will record points until you hit enter.
fig = figure();
img = rand(50);
imshow(img)
[x,y] = ginput();
% Get the pixel values
data = img(sub2ind(size(img), round(y), round(x)));
Below is an example using the ButtonDownFcn callback
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

Suever
- 64,497
- 14
- 82
- 101