Borrar filtros
Borrar filtros

How to I get the coordinates of a given pixel?

3 visualizaciones (últimos 30 días)
James Richards
James Richards el 21 de Mzo. de 2016
Comentada: Pablo Velez el 6 de Mayo de 2021
I was wondering if there was a method to get the X,Y coordinate of a pixel in an image? I have the pixels that I'd like to get the coordinates of, but as they aren't regions, a regionprops centroid doesn't help, and I need it to be done without user input, so I can't click on them with ginput.
  3 comentarios
James Richards
James Richards el 21 de Mzo. de 2016
Sorry for the late reply. I'm not entirely certain myself (still quite new to matlab). I'm trying to get the X,Y coordinates of pixels found at the end points of ellipses. The following code is used to generate the ellipse from my regionprops-
phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);
for k = 1:length(refinedStats)
xbar = refinedStats(k).Centroid(1);
ybar = refinedStats(k).Centroid(2);
a = refinedStats(k).MajorAxisLength/2;
b = refinedStats(k).MinorAxisLength/2;
theta = pi*refinedStats(k).Orientation/180;
R = [ cos(theta) sin(theta)
-sin(theta) cos(theta)];
xy = [a*cosphi; b*sinphi];
xy = R*xy;
x = xy(1,:) + xbar;
y = xy(2,:) + ybar;
plot(x,y,'r','LineWidth',2);
end
And this code to get the endpoints of the ellipse-
l = length(refinedStats);
for n = 1:l
theta = pi*refinedStats(k).Orientation/180;
%for xEnd
xbar = refinedStats(n).Centroid(1);
a = refinedStats(n).MajorAxisLength/2;
xEnd = xbar + a * cos(theta);
refinedStats(n).endPointX = xEnd;
%for yEnd
ybar = refinedStats(n).Centroid(1);
yEnd = ybar + a * sin(theta);
refinedStats(n).endPointY = yEnd;
end
This gives me data in my 'refinedstats' structure like endPointX = 1003.87333157056 and endPointY= 989.636902390572. It's these numbers I'd like to get the coordinates of.
Walter Roberson
Walter Roberson el 21 de Mzo. de 2016
X coordinate 1003.87333157056 with Y coordinate 989.636902390572 would be found at index (989.636902390572, 1003.87333157056) in the array. Y then X.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 22 de Mzo. de 2016
Use bilinear interpolation, like interp2() if you want the exact value of fractional coordinates. However, it may be good enough for you to do a much simpler operation of just rounding the coordinates to the nearest integer and just getting the array element
grayLevel = grayImage(round(y), round(x));
Remember, like Walter said above, it's (y,x) not (x,y) because the first index is rows and y is the row.
You might also like the impixel() function, but I don't find that any more convenient than simple indexing.
  3 comentarios
Image Analyst
Image Analyst el 3 de Feb. de 2020
No, it's (x, y) not (row, column), which would be (y, x). That's one thing you always have to keep mindful of and a common cause of beginners' problems. It's described in the help documentation.
props = regionprops(mask, 'Centroid');
xy = vertcat(props.Centroid);
x = xy(:, 1); % Columns
y = xy(:, 2); % Rows
I can't help you with the conversion to geographical coordinates. Look into functions of the Mapping Toolbox.
Pablo Velez
Pablo Velez el 6 de Mayo de 2021
"a common cause of beginners' problems" happend to me but I didn't find it in the documentation https://www.mathworks.com/help/matlab/ref/imread.html I could only infer it from the example, is that what you mean with "described in the help documentation" or am I missing something.
Also I don't undertand why they switch x y places, python matplotlib does the same, why?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by