Question about coordinate system in matlab

8 views (last 30 days)
MR. W
MR. W on 6 Nov 2014
Commented: DGM on 20 Feb 2023
Hi, i'm a beginner in Matlab and currently is doing a project related to image processing. For some reason, i have encountered many problems from the coodinating system in Matlab due to not very familiar with it. Is there anyone is kind enough to explain about it?
I would like to know more about few topic as listed below: pixel coordinate in image, [x,y] coordinate in image, row colunmn in image,
And how to do conversion between them
Thank you in advance.

Answers (4)

Guillaume
Guillaume on 6 Nov 2014
This page out of matlab's doc explains most of it.
Personally, I think that Mathwork royally screwed up on this. You basically have two different coordinate systems and some functions work in one and some in the other. There may even be some functions that take inputs in one and return output in the other.
You have a set of function that use the [row, column] system (e.g. find). Origin is top-left corner. row increases as you go down, column as you go right. The other set of functions (e.g. mesghrid) use [x, y] with origin in the top left corner, x goes right and y goes down. You convert from one to the other by swapping the order of the two.
Then, you have the absurdity that is regionprops bounding box, whose coordinates of the corners are fractional (always integer + 0.5) because mathworks differentiate between the centre of a pixel (integer coordinates) and its corner.
Regardless of the coordinate system returned in the output of a function, you always access pixels with the [row, column] system.

Image Analyst
Image Analyst on 6 Nov 2014
Since youi're just starting, please see my tutorials in my File Exchange, particularly the one on image segmentation.
You always need to be careful about row,column and x,y. It's a very common mistake because MATLAB uses both everywhere, not just in image processing. People often get some coordinate in the form (x,y) and then try to access the pixel in their array using grayImage(x,y). As the others pointed out, that won't work because the first index is the row, and x is not the row. So if you want that (x,y) pixel you'll have to do grayImage(y, x) NOT grayImage(x,y).
To get pixel coordinates in real world coordinates (like cm or meters) you'll need to do a spatial calibration. See my attached demo.

Youssef  Khmou
Youssef Khmou on 6 Nov 2014
You can start inspecting the x,y values in sample, to get an idea try :
>>imshow('circuit.tif')
Then go to Tools in menu bar of the figure, then click on Data Cursor, when you click on point in the image the (x,y,intensity) is shown .

MR. W
MR. W on 7 Nov 2014
thanks for the answers, but i still not really understand, For example, i want to get the center line equation of an image y = mx +c, can i used size(image,1)/2 and size(image,2)/2 to get the coordinate information for the equation? or there is another way?
  3 Comments
MR. W
MR. W on 7 Nov 2014
Thanks for the demo and also the answer, however, i still face some problem when testing my own code.
This is my code, and the purpose of the code is to draw a center line in an image
first code:
I = imread('a.jpg');
y = 0.5:(size(I,1)-0.5);
x = (size(I,2)-0.5)/2;
figure; imshow(I); hold on; plot(x,y);
and the result i get:
However, when i doing using this code
I = imread('a.jpg');
center_top= [ (size(I,2))/2, 0.5];
center_bottom = [ (size(I,2))/2, (size(I,1))-0.5 ];
figure; imshow(I); hold on; plot(center_top,center_bottom);
i'm getting this
actually for the second code i just want to draw a center line from first top xy coordinate to bottom xy coordinate (because i want the y = mx+c center line equation), and the result seem not very accurate, can you provide me a better solution or pinpoint out the problem here?
Thank you in advance.
DGM
DGM on 20 Feb 2023
The problem here is the misuse of plot(). The first two arguments to plot should be x and y, not point1 and point2.
FWIW, this is one way to approach this sort of thing.
% an image
inpict = imread('peppers.png');
% it's often convenient to specify points in normalized coordinates
% by having all the points in the same array, the code stays neat ...
points = [0 0.5; % [y x]
1 0.5];
% denormalize to get image coordinates
sz = size(inpict);
points = points.*(sz(1:2)-1) + 1;
% plot things
imshow(inpict); hold on
plot(points(:,2),points(:,1),'c'); % ... and it's easy to split x and y

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!