lincr
lincr

Reputation: 1653

Matlab rectangle property - 'Position' description confusion while ploting

I'm running Matlab R2014b and working at drawing a rectangle on an image given specific coordinates.

To be clear, say draw a rectangle[100, 100, 200, 50] is drawing a rectangle whose left corner is 100 pixels offset from the top left corner of the image in both horizontal and vertical directions, and the width is 200, height is 50. The following code works:

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

But before I get the above correct method, I run doc rectangle and the command gives usage like rectangle('Position',[x,y,w,h]), then I checked the Rectangle Properties page, it says

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.

However, the above description is mismatch with the above correct code in y-directions, i.e. whether lower-left or upper-left corner to be (0,0) point. I guess they are applyed in different scenarios. Explanation needed.

--** Edit: add a piece of code for anyone encountering this flip-up-down question to test**--

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');

Upvotes: 0

Views: 377

Answers (1)

EBH
EBH

Reputation: 10440

The full description of Rectangle Properties is:

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.

The bold sentence means that you should pay attention to the axis direction. You first use imshow which flips the y-axis direction of the axes from top to bottom.

To see the same behavior as you expected, you can type axis xy to flip the axes back after you call imshow (so (0,0) point will be at the lower left corner). However, this will also flip the image, so you probably just need to calculate the position from the top:

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

Upvotes: 1

Related Questions