2
votes

I started writing a program in MATLAB to overlay two grayscale images of same size with different alpha dynamically. Therefore both images are plotted in one figure and beneath them is a slider with which the alpha of the second image can be increased from zero to one. So when moving the slider one can actually sees the two images blending. Suppose the slider is at 0.3 then the 'AlphaData' of the second image is set to 0.3, while the 'AlphaData' of the first image is always 1. On the screen I now see an image, which is a combination of these two images.

Now I want to get exactly this image from the figure (with the same size, as the images before) and work with it. But I have no idea how to do it.

1

1 Answers

6
votes

Grayscale image is just array of numbers. Depending on how you get the data it could be 0~1 or 1~255. Overlaying two images is merely adding the numbers. Blending two images is merely computing their weighted sum.

clear;clc;close all
I1_rgb = imread('peppers.png');
I1_gray = rgb2gray(I1_rgb);
figure(1)
imshow(I1_gray)

I2_gray = imread('coins.png');
I2_gray = padarray(I2_gray, size(I1_gray)-size(I2_gray), ...
    'circular', 'post');
figure(2)
imshow(I2_gray)

alpha = .3; % this can be dynamically adjusted by a slider
O1 = I1_gray + I2_gray*alpha; % overlay
figure(3)
imshow(O1)
O2 = I1_gray*(1-alpha) + I2_gray*alpha; % blend
figure(4)
imshow(O2)

For mixing colored images, see my answer at MATLAB: Applying transparent mask over an RGB image and blending with another