Clear Filters
Clear Filters

How to pass from pixel coordinates to real world coordinates?

6 views (last 30 days)
Hi everyone! I have the following problem. I have obtained a binary image M that has value 1 when there is the obstacle, otherwise value 0 and then I have calculated the centroids of each cluster of pixels in this way.
s = regionprops(M,'centroid');
centroids = cat(1,s.Centroid);
figure
plot(centroids(:,1), centroids(:,2),'b*')
Now i would like to know how do i get these centroid values from pixel coordinates back to real world coordinates. How can i do? Thanks in advance.

Accepted Answer

Voss
Voss on 1 Jul 2022
Edited: Voss on 1 Jul 2022
unzip reticolato_centroids.zip
load reticolato_centroids.mat
x = -1600:3:1600; %real world coordinate
y = -1200:3:1200; %real world coordinate
s = regionprops(M,'centroid');
centroids = cat(1,s.Centroid);
% linear transform from
% pixel indices to [x y ]
% [1 1] -> [x(1) y(1) ]
% size(M,[2 1]) -> [x(end) y(end)]
centroids_real_world = (centroids-1)./(size(M,[2 1])-1).*[x(end)-x(1) y(end)-y(1)]+[x(1) y(1)];
figure
plot(xv,yv,'.r')
hold on
plot(centroids_real_world(:,1), centroids_real_world(:,2),'b.')
figure
set(pcolor(M),'EdgeColor','none')
hold on
plot(centroids(:,1), centroids(:,2),'m.')
% linear transform from
% [x y ] to pixel indices
% [x(1) y(1) ] -> [1 1]
% [x(end) y(end)] -> size(M,[2 1])
limits = ([-400 -100; 300 600]-[x(1) y(1)])./[x(end)-x(1) y(end)-y(1)].*(size(M,[2 1])-1)+1;
xlim(limits(:,1))
ylim(limits(:,2))

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!