【问题标题】:Matlab rectangle property - 'Position' description confusion while plotingMatlab矩形属性 - 绘图时“位置”描述混淆
【发布时间】:2017-06-08 14:49:45
【问题描述】:

我正在运行 Matlab R2014b 并在给定特定坐标的图像上绘制一个矩形。

要清楚,说绘制一个矩形[100, 100, 200, 50] 是绘制一个矩形,其左角在水平和垂直方向上与图像的左上角偏移 100 像素,宽度为200,身高50。 以下代码有效:

figure;
imshow(im);
rectangle('Position',[100, 100, 200, 50],'edgeColor','r');

但是在我得到上述正确方法之前,我运行doc rectangle 并且命令给出了类似rectangle('Position',[x,y,w,h]) 的用法,然后我检查了Rectangle Properties 页面,它说

x 和 y 元素定义矩形左下角的坐标。 width 和 height 元素定义了矩形的尺寸。

但是,上面的描述在 y 方向上与上面的正确代码不匹配,即左下角或左上角是否为 (0,0) 点。我想它们适用于不同的场景。需要解释。

--** 编辑:添加一段代码供遇到这个上下翻转问题的人测试**--

im = imread('e:/_Photos/2.jpg'); %load your local image file

%Original version, correct to draw rectangles
figure;
subplot(1,3,1);
imshow(im);
rectangle('Position',[100, 100, 200, 50],'edgeColor','g');

%flip once using `axis xy`, need to set y_new = height-y
subplot(1,3,2);
imshow(im);
axis xy;
rectangle('Position',[100, size(im,1)-100, 200, 50],'edgeColor','r');
rectangle('Position',[100, 100, 200, 50],'edgeColor','g');

%flip twice using `flipud` and `axis xy`,
%note we still need to recalculate the new y
subplot(1,3,3);
im = flipud(im);
imshow(im);
axis xy;
rectangle('Position',[100, size(im,1)-100, 200, 50],'edgeColor','g');
rectangle('Position',[100, 100, 200, 50],'edgeColor','r');

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案1】:

    矩形属性的完整描述是:

    矩形的大小和位置,指定为 [x y width height] 形式的四元素向量。 以数据单位指定值。 x 和 y 元素定义矩形左下角的坐标。 width 和 height 元素定义了矩形的尺寸。

    粗体句表示要注意轴方向。您首先使用imshow,它将轴的y轴方向从上到下翻转。

    要查看与您预期相同的行为,您可以在调用 imshow 后键入 axis xy 将轴翻转回来(因此 (0,0) 点将位于左下角)。但是,这也会翻转图像,因此您可能只需要从顶部计算位置:

    rectangle('Position',[100 size(im,1)-100 200 50]);
    

    【讨论】:

    • 所以Matlab基本使用的是图形坐标系,其中(0,0)位于左下角,在做图像相关的操作时,坐标系,即Image Coordinate Systems是翻转的-上下。我认为这样做的原因是同意我们访问图像数据的方式。我说的对吗?
    • 是的。确切地。 axis xy 的对立面是 axis ij,这就像我们想到的是一张桌子,而不是笛卡尔系统。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 2014-02-17
    • 2023-03-06
    • 1970-01-01
    • 2021-07-21
    • 2016-01-31
    相关资源
    最近更新 更多