lopezbertoni
lopezbertoni

Reputation: 3641

Plot mask on top of grayscale image MATLAB

I'm running an algorithm to segment part of an image using morphological operations. I end up with a 2D binary image that represents the segmentation results. Namely, the mask. My question is how to plot the original image and the mask overlay in color on top of it.

Thank you.

Upvotes: 2

Views: 7426

Answers (2)

reve_etrange
reve_etrange

Reputation: 2571

Using imoverlay is fine, I do so myself, but often I want a transparent composite. It's convenient to have this in a single array as it makes saving the image a bit easier.

alpha = repmat(0.35 * mask,[1 1 3]);
labels = label2rgb(bwlabel(mask));
im3 = repmat(im,[1 1 3]); %# Assuming image is grayscale
overlay = ( (1-alpha) .* im3 ) + ( alpha .* labels );
imshow(overlay); %# Or imwrite, etc.

Upvotes: 2

petrichor
petrichor

Reputation: 6569

You can use imoverlay written by Steve Eddins.

In addition, you can check these blog posts by him:

Upvotes: 5

Related Questions