필터 지우기
필터 지우기

How to I get the coordinates of a given pixel?

조회 수: 3 (최근 30일)
James Richards
James Richards 2016년 3월 21일
댓글: Pablo Velez 2021년 5월 6일
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