How to I get the coordinates of a given pixel?

3 次查看(过去 30 天)
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 个评论
James Richards
James Richards 2016-3-21
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 2016-3-21
X coordinate 1003.87333157056 with Y coordinate 989.636902390572 would be found at index (989.636902390572, 1003.87333157056) in the array. Y then X.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2016-3-22
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 个评论
Image Analyst
Image Analyst 2020-2-3
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 2021-5-6
"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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by