【发布时间】: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