View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Image overlay

4.5 | 34 ratings Rate this file 112 Downloads (last 30 days) File Size: 2.09 KB File ID: #10502 Version:
image thumbnail

Image overlay


Steve Eddins (view profile)


23 Mar 2006 (Updated )

Overlay a binary mask onto an image using a specified color

| Watch this File

File Information

The function IMOVERLAY creates a mask-based image overlay. It takes input image and a binary mask, and it produces an output image whose masked pixels have been replaced by a specified color.


This file inspired Neuron Counting, Telo Tool Terminal Restriction Fragment Analysis, Visualization Of Multi Modal Volume Segmentation, Multi Wall (Cost231) & Free Space Signal Propagation Models, Imagescnan.M V2.1 (Aug 2009), Alphamask: Semi Transparent Image Overlay, Jalali Lab Ucla/Image Feature Detection Using Phase Stretch Transform, and Flatten Mask Overlay.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.2 (R2006a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (47)
25 Oct 2016 bourr houssam

16 Oct 2016 Abdollah Rahman

16 Oct 2016 Abdollah Rahman

20 Sep 2016 ujjwal baid

How to make it work for 16 bit image. I am working on 16 bit medical image.

15 Apr 2016 afshin shoeibi


01 Feb 2016 Matthew Eicholtz

Easy to understand and use. Works as expected. Thanks Steve!

18 Dec 2015 Shajahan Sunoj

Awesome... Thanks Eddins.. It helped me a lot... It Works so well now...

Comment only
14 Dec 2015 Steve Eddins

Steve Eddins (view profile)

Shajahan - this imoverlay.m has fewer than 134 lines in it, and it does not call imagesc. You must be using somebody else's imoverlay.m.

Comment only
12 Dec 2015 Shajahan Sunoj

Sorry. It does not work for me. Can anyone help me how to make it work?

I am trying to use your same image and same code as you showed in the cell segmentation demo. I am getting following errors.

Error using image
Incorrect number of arguments.

Error in imagesc (line 39)
hh = image(varargin{:},'CDataMapping','scaled');

Error in imoverlay (line 134)
hF = imagesc(F,climF);

Error in celltry (line 12)
overlay1 = imoverlay(I_eq, bw4_perim, [.3 1 .3]);

Comment only
24 Sep 2015 Matthew Schneck


19 Aug 2015 emrah

emrah (view profile)

05 Aug 2014 Kirtan

Kirtan (view profile)


25 May 2014 Mohammad Ahmed

It was great. Keep up the good work. Thumbs up.

12 Feb 2014 Massimo Ciacci

Massimo Ciacci (view profile)

16 Oct 2013 Steve Eddins

Steve Eddins (view profile)

Image Analyst, thanks for your comments. I like your suggestion about accepting the output of bwboundaries as the 2nd input argument. I don't really understand your comment about bwperim, though.

Comment only
13 Oct 2013 Image Analyst

Image Analyst (view profile)

bwperim is not the only way to create outlines. It would be nice if imoverlay were smart enough to see if the input was a binary image, or a cell array that is the output of bwboundaries. A lot of times we threshold and then call bwboundaries. It would be a nice enhancement if it could take the output of bwboundaries as the location of the outlines to draw.

Comment only
22 Jul 2013 Steve Eddins

Steve Eddins (view profile)

Andrew, you'll have to either pad or resize one of the images to match the other size to overlay them using this function. You could also consider using MATLAB graphics transparency features to overlay one image on another.

Comment only
19 Jul 2013 Andrew

Andrew (view profile)

Very good tool. Is there any way you can overlay two images that are different sized without one of them becoming dis-formed?

09 Dec 2012 Felix

Felix (view profile)

I'd like to use this function visualize the clipped pixels (e.g. with value 256 in 8bit grayscale) in red, in the preview of a camera recording.

its possible to apply an arbitrary function to each video frame before its displayed in the preview with this:

however, I havent been able to combine this with imoverlay. Any suggestions??

Comment only
24 Aug 2012 Margarita

18 Aug 2012 Arash

Arash (view profile)

14 Aug 2012 Sid

Sid (view profile)

21 Apr 2012 supriya

17 Apr 2012 Steve Eddins

Steve Eddins (view profile)

Lasse - thanks, I have uploaded a new version based on your suggestion.

Comment only
10 Apr 2012 Lasse

Lasse (view profile)

This might help to some people

out_red(logical(mask)) = color_uint8(1);
out_green(logical(mask)) = color_uint8(2);
out_blue(logical(mask)) = color_uint8(3);

Hope it helps..

05 Mar 2012 Jamuna Jayashri Mahalingam

I am not able to implement it. How should I use this function in my program?

Comment only
10 Dec 2011 Mahmut

Mahmut (view profile)

10 Dec 2011 Mich

Mich (view profile)

09 Mar 2011 Mangesh

09 Mar 2011 Mangesh

is it i can find the upper and lower pixel value of my region..............

Comment only
27 Feb 2010 TIAN ZHOU

thanks. used in my code. will upload soon

06 Nov 2009 James

James (view profile)

03 Aug 2009 hima

hima (view profile)

This code was great.heyy i want the canny edge borders to be they are in green to get that???

Comment only
17 Jun 2009 Rotem

Rotem (view profile)

Great work! saved me some precious time...

Comment only
04 Mar 2009 paddy84

This is an excellent bit of code. It worked perfectly fine in my case.

03 Aug 2008 Waleed El-Badry

that's an excellent piece of code.

11 Mar 2008 Carlos Adrián Vargas Aguilera

Great tool. Now I can paint NaN's in black on imagesc.


01 Dec 2007 Robert Misevski

Great tool work very well for what a needed to do.

03 Sep 2007 Steve Eddins

Matt - I added a note and an example to the documentation based on your suggestion. Thanks!

Comment only
31 Aug 2007 Phillip SUn

great and loved it

14 Aug 2007 B r

18 Jul 2007 Matt Whitaker

Simple, useful and well styled and coded. Did something in my existing code similar but this is a bit faster and less memory. Steve, you might want to note in the documentation that if you have a double input outside the range of 0-1 (soemthing you typically use imagesc to display) that you would want to run mat2gray on it before passing to this function. A useful further enhancement would be either to auto detect and apply this or have an optional switch.

14 Jun 2007 Chai Tan

Works great. Saves me a few lines of code! :) Thanks.

28 Apr 2007 Lennart Burg

I think it works great. Thnx

11 Oct 2006 Gil Hornung

I would like to overlay a picture which is not bw.

10 Oct 2006 James Jones

??? Subscript indices must either be real positive integers or logicals.

Error in ==> imoverlay at 54
out_red(mask) = color_uint8(1);

17 Apr 2012 1.2

Based on a suggestion from Lasse, I added code to automatically convert the mask input to be logical.

01 Sep 2016

Updated license

Contact us