How can I create a rectangle with an outlined border?

user1207668 picture user1207668 · Feb 13, 2012 · Viewed 13.7k times · Source

I want to draw a rectangle to outline an area of an image that I've plotted in one axes of a figure. I have multiple axes in this figure, so I am using the rectangle() function. What I want is to draw a white rectangle a thin black border just inside and just outside the rectangle. The part of the image inside the rectangle should be visible, so all 'facecolor' should be 'none'. I have tried drawing 3 rectangles, two black ones with thin linewidths and one thicker white one, but the problem is that 'Position' is defined in axes units and 'LineWidth' is defined in point units, so that the scaling doesn't work too well, especially when the figure is resized.

FYI, the outline is so that the white rectangle is more visible against a light background. The images plotted vary widely, so a single color won't be universally visible for my data.

Any suggestions on how I can do this?

Answer

yuk picture yuk · Feb 13, 2012

How about just using different line widths for black and white rectangle?

imshow('cameraman.tif')
rectangle('position',[80 30 100 100],'edgecolor','k','LineWidth',4)
rectangle('position',[80 30 100 100],'edgecolor','w','LineWidth',1)

cameraman with rectangle (Save As)

Hmm, the corners look much better on MATLAB figure than as PNG file.

Better with getframe:

cameraman with rectangle (getframe)