【问题标题】:Calculate Y coordinates of an image from graph point of origin从图形原点计算图像的 Y 坐标
【发布时间】:2022-01-15 21:01:10
【问题描述】:

在 Matlab 中,图像轴显示为行和列(矩阵样式),它们翻转/导致 Y 轴从左上角开始。在下面的脚本中,我使用interparc (File Exchange link) 将轮廓划分为等距离的点。

我希望转换/调整所选点的计算 Y 坐标,使它们从“图形原点”(0,0;左下角)开始,但不翻转图像。知道如何进行坐标转换吗?

代码:

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,:) = [] 

【问题讨论】:

  • 您可以使用 imfinfo(url) 获取高度,然后从中减去 y 以获得您所要求的内容。然而,这比你在问题中所做的其他事情要容易得多,所以我认为这不是你要问的。另外,你为什么用 x 作为垂直坐标,用 Y 作为水平坐标?
  • 我把坐标翻了个跟原图匹配,不然不行。
  • 我根本没有审查你的代码,只是看看这个问题,我想知道像set(gca,'YDir','normal') 这样简单的东西是你需要的吗?
  • set(gca, 'YDir', 'normal') 翻转图像方向

标签: matlab image-processing coordinates


【解决方案1】:

您需要翻转 y 轴的显示方向(如评论中的@If_You_Say_So 建议)。

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

Y 轴现在指向上方,但图像显示颠倒。所以我们也翻转图像的 y 数据。

I=flipud(I);

图像被翻转两次,因此是直立的。

在绘制数据或根据它进行任何计算之前,应翻转数据。以后显示图像或绘制轮廓时,可以翻转 y 轴的方向。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2016-11-14
    • 2014-11-05
    • 2012-11-02
    相关资源
    最近更新 更多