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

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????????

 Respuesta aceptada

David Young
David Young el 24 de Feb. de 2014
Editada: David Young el 24 de Feb. de 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.
function pts = readPoints(image, n)
%readPoints Read manually-defined points from image
% 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.
%
% POINTS = READPOINTS(IMAGE, N) reads up to N points only.
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

7 comentarios

Xylo
Xylo el 24 de Feb. de 2014
Editada: Xylo el 24 de Feb. de 2014
thank u for response. I have already used this function. but this function use a two-axis cross line, which i dnt want to use. and another thing is that this function does not allow to put a sign/symbol(for understnding that i have clicked on this point) on that particular point.
Actually I want to get multiple location's cordinate value and their intensity also. just like this photo(for example):
in this uploaded photo black dots are the clicked point and after clicking i want to get corresponding location and pixel vale. thank u.
I'm not sure how to get rid of the cross-line cursor (what would you replace it with?) I'm editing a function I use for multiple points into the answer above.
Xylo
Xylo el 25 de Feb. de 2014
Editada: Xylo el 25 de Feb. de 2014
thank you David Young........ your code is working fine............... and about to get rid of the cross-line cursr I will handle it later. lot of thanks to u..
May I know where the values are getting stored?
In xi and yi in the line of code that says this:
[xi, yi, but] = ginput(1); % get a point
That's asking the user for a single points. Then he stores that point into a list of multiple points with these lines of code:
pts(1,k) = xi;
pts(2,k) = yi;
The question remained unanswered. xi and yi are only the coordinates. Where is the pixel value captured?
pixels = impixel(image, pts(1,:), pts(2,:));

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 24 de Feb. de 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().
According to ChatGPT is easier to do:
function pts = readPoints(image, n)
%readPoints Read manually-defined points from image
% POINTS = READPOINTS(IMAGE, N) reads up to N points only.
imshow(image); % display image
[x,y] = ginput(n);
pts = [x y];
end

Categorías

Más información sobre Read, Write, and Modify Image en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Feb. de 2014

Editada:

el 5 de Feb. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by