# How to get onclick coordinate pixel value and location from an image?

283 views (last 30 days)
Xylo on 24 Feb 2014
Hey frndz, I am new in Matlab. Since 3months ago I am using matlab. Within these 3 months I just learned some matrix manipulation functionm addition, string handling etc. So I need all of your help. Please please help me............... How to get onclick coordinate pixel value and location from an image? Actually I want to get some location(x,y) and corresponding pixel value after clicking on image. how can I do this????????
Xylo on 24 Feb 2014

David Young on 24 Feb 2014
Edited: David Young on 24 Feb 2014
Use ginput
If you want to record multiple points, you can use something like the function that follows. You can easily modify it to display dots rather than lines between the points - check the documentation for the plot function.
% POINTS = READPOINTS(IMAGE) displays the image in the current figure,
% then records the position of each click of button 1 of the mouse in the
% figure, and stops when another button is clicked. The track of points
% is drawn as it goes along. The result is a 2 x NPOINTS matrix; each
% column is [X; Y] for one point.
%
if nargin < 2
n = Inf;
pts = zeros(2, 0);
else
pts = zeros(2, n);
end
imshow(image); % display image
xold = 0;
yold = 0;
k = 0;
hold on; % and keep it there while we plot
while 1
[xi, yi, but] = ginput(1); % get a point
if ~isequal(but, 1) % stop if not button 1
break
end
k = k + 1;
pts(1,k) = xi;
pts(2,k) = yi;
if xold
plot([xold xi], [yold yi], 'go-'); % draw as we go
else
plot(xi, yi, 'go'); % first point on its own
end
if isequal(k, n)
break
end
xold = xi;
yold = yi;
end
hold off;
if k < size(pts,2)
pts = pts(:, 1:k);
end
end
Mrutyunjaya Hiremath on 17 May 2020
pixels = impixel(image, pts(1,:), pts(2,:));

Image Analyst on 24 Feb 2014
If you don't need the value in your code but just want to interactive view the x,y and RGB as you mouse over the image, you can use impixelinfo().
Xylo on 25 Feb 2014
No, not at all. I need values as well as location.............