File Exchange

image thumbnail

Draw a border around an image

version (2.16 KB) by Eric Johnson
Draws a colored border around an image.


Updated 31 Dec 2009

View Version History

View License

ADDBORDER draws a border around an image.

NEWIMG = ADDBORDER(IMG, T, C, S) adds a border to the image IMG with thickness T, in pixels. C specifies the color, and should match the color format of the image (e.g., RGB or indexed). STROKE is a string indicating the position of the border:

'inner' - border is added to the inside of the image. The dimensions of OUT will be the same as IMG.

'outer' - the border sits completely outside of the image, and does not obscure any portion of it.

'center' - the border straddles the edges of the image.


load mandrill
X2 = addborder(X, 20, 62, 'center');
axis off
axis image

Cite As

Eric Johnson (2021). Draw a border around an image (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (17)

Abhilash Molkeri

Parishad Bromandnia

Felipe Clemente

Xiaoyun Chen

Hey, not sure what went wrong in my code, but all the output color was black no matter what setting was.

I = imread('obj 21.png');
I2 = addborder(I, 20, [0 0.5 0], 'outer'); % isn't that [0,0.5,0] green???
imwrite(I2, 'obj 21 outer.png');


shadi AL amoudi

i am beginner in Matlab, could you please let me know how i can run the code.

Niccolò Baldanzini

Matt J

Matt Saviano

Arpit Bhatia

Mohamed Gaber

That's what i was searching for, Many thanks!

James Cusack

Fantastic implementation, nice and simple. Well documented. Thank you.

Jose Ramos

This is a great function. It made my day. However, it gives me an error when I want to use it with grayscale images. Is this only for color images? Do you know a workaround grayscale images? That would be great. Thanks for a good job.

M Sohrabinia

The function works perfectly, however, it could have a better help to explain where X comes from: X in the example is the input image (or IMG) for which you want to show (e.g., using imshow). In fact, this function turns the marginal pixels to the values given by color. My suggestion is to assign default values for T, C and Stroke, something like: T=1 pixel, C=0=black, S= 'center'. Its a great function anyway, thanks for sharing.


Awesome. Just what I needed.



Where doese the X from your example come from? It is not working at all. The 't' is limited to 1 and what I get is only img2 = 'c' depends on what is entered to 'c'.

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!