Add overlay image on top of other image

Viewed 580

Suppose that I have an RGB image RGB and a binary image binary that contains the result of segmentation of image RGB. How to draw the image binary on top of image 'RGB` and show the segmentation as a red transparent area? I tried the following but I got an error. Please help me to figure out the best way in MATLAB

I=imread('RGB.png');
[M,N,C] = size(I);
h=imshow(I);
alpha= imread('binary.png');
alpha = cat(3,alpha,zeros(M,N), zeros(M,N));
set(h, 'AlphaData', alpha);

Here are the input images:

RGB

binary

3 Answers

You simply use your binary alpha (one layer only) as AlphaData.

Here is a method to add red overlays in select regions of the mask. Modifications to these scripts can be made to fill in the remaining regions with the colours white or black. The regions of interest on the mask are selected using a logical array.

Overlay Red Over White Region of Mask

Overlay Image 1

Overlay_Opacity = 0.5;

Image =imread('RGB.png');
imshow(Image);

Red_Channel = imread('binary.png');
White_Mask_Region = Overlay_Opacity*(Red_Channel ~= 0);

Overlay_Image(:,:,1) = White_Mask_Region;
Overlay_Image(:,:,2) = 0;
Overlay_Image(:,:,3) = 0;

hold on
Overlay = image(Overlay_Image);
Overlay.AlphaData = White_Mask_Region;
saveas(gcf,'Overlay_1.png');

Overlay Red Over Black Region of Mask

Overlay Image 2

Overlay_Opacity = 0.5;

Image =imread('RGB.png');
imshow(Image);

Red_Channel = imread('binary.png');
Black_Mask_Region = Overlay_Opacity*(Red_Channel == 0);

Overlay_Image(:,:,1) = Black_Mask_Region;
Overlay_Image(:,:,2) = 0;
Overlay_Image(:,:,3) = 0;

hold on
Overlay = image(Overlay_Image);
Overlay.AlphaData = Black_Mask_Region;
saveas(gcf,'Overlay_2.png');
Related