Steve on Image Processing and MATLAB

Concepts, algorithms & MATLAB

How to Overlay a Color on an Image Using a Mask

In my first year of writing this blog (2006!), I showed how to overlay a color onto an image based on a mask. This was the example I gave back then:

I wrote a function called imoverlay to handle all the details, and I submitted it to the File Exchange.

About three and a half years ago, in R2016a, the Image Processing Toolbox development team shipped a similar function, with the same name. I want to encourage everyone to use that new function.

Since my File Exchange version is still being frequently downloaded, I decided not to remove it (yet). Instead, I updated the submission to change the function name to imoverlay_old and to include a note about the new function in the Image Processing Toolbox.

Here's a new example. I'm going to use imoverlay (the new one) to overlay the output of the Canny edge detector on the peppers.png image. So that the overlay is visible everywhere, I will use imoverlay twice, with contrasting colors.

A = imread('peppers.png');
imshow(A,'InitialMagnification',200)
E = edge(rgb2gray(A),'canny');
imshow(E,'InitialMagnification',200);

Make a dilated (thickened) version of the edge mask and use it for the first overlay (in yellow).

Ed = imdilate(E,ones(3,3));
B = imoverlay(A,Ed,'y');

Now use the original edge detection output for the second overlay (in blue).

C = imoverlay(B,E,'b');
imshow(C,'InitialMagnification',200);

If you've been using my version of imoverlay and you have R2016a or later, consider switching to the one that is now included with the Image Processing Toolbox.




Published with MATLAB® R2019b

|
  • print
  • send email

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.