jane
jane

Reputation: 567

Calculate Y coordinates of an image from graph point of origin

In Matlab the image axes are shown as rows and columns (matrix style) which flip/cause the Y axis to start from the upper left corner. In the script below, I divide an outline to equally distance points using interparc (File Exchange link).

I wish to convert/adjust the calculated Y coordinates of the selected points so they will start from the “graph point of origin” (0,0; lower left corner) but without flipping the image. Any idea how to do this coordinates conversion?

Code:

clc;
clear;
close all;

readNumPoints = 8  
numPoints = readNumPoints+1
url='https://icons.iconarchive.com/icons/thesquid.ink/free-flat-sample/512/owl-icon.png';

I = imread(url);
I = rgb2gray(I);
imshow(I);
 
BW = imbinarize(I);
BW = imfill(BW,'holes');
hold on;
[B,L] = bwboundaries(BW,'noholes');

k=1;
stat = regionprops(I,'Centroid');
b = B{k};
c = stat(k).Centroid;
y = b(:,2);
x = b(:,1);

plot(y, x, 'r', 'linewidth', 2);

pt = interparc(numPoints,x,y,'spline');
py = pt(:,2);
px = pt(:,1);
 
sz = 150;
scatter(py,px,sz,'d')
 
str =1:(numPoints-1);
plot(py, px, 'g', 'linewidth', 2);
text(py(1:(numPoints-1))+10,px(1:(numPoints-1))+10,string(str), 'Color', 'b');
pointList = table(py,px)
pointList(end,:) = [] 

Upvotes: 4

Views: 229

Answers (1)

Miscellaneous
Miscellaneous

Reputation: 398

You will need to flip the display direction of y-axis (as @If_You_Say_So suggested in the comment).

set(gca,'YDir','normal')

Y-axis is now pointing upward, but the image is displayed upside down. So we flip the y-data of the image as well.

I=flipud(I);

The image is flipped twice and is thus upright.

The data should be flipped before you plot it or do any calculation based on it. The direction of y-axis can be flipped later when you show the image or plot the outline.

url='https://icons.iconarchive.com/icons/thesquid.ink/free-flat-sample/512/owl-icon.png';

I = imread(url);
I = rgb2gray(I);

% Flip the data before `imshow`
I=flipud(I);

imshow(I);

% Flip the y-axis display
set(gca,'YDir','normal')
pointList =

  py        px  
______    ______

     1       109
149.02    17.356
362.37     20.77
   512    113.26
413.99    270.84
368.89    505.99
 141.7       508
98.986    266.62

Upvotes: 3

Related Questions