Matlab 矩形 属性 - 'Position' 绘图时描述混乱

Matlab rectangle property - 'Position' description confusion while ploting

我正在 运行正在使用 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 页面,它说

The x and y elements define the coordinate for the lower-left corner of the rectangle. The width and height elements define the dimensions of the rectangle.

但是上面的描述在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');

矩形属性的完整描述是:

Size and location of the rectangle, specified as a four-element vector of the form [x y width height]. Specify the values in data units. The x and y elements define the coordinate for the lower-left corner of the rectangle. The width and height elements define the dimensions of the rectangle.

粗体的意思是要注意轴的方向。您首先使用 imshow 从上到下翻转轴的 y 轴方向。

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

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