This is face morphing. Now I want to save and convert to a gif image format.

15 views (last 30 days)
close all
iter=10;
A=im2double(imread('1.JPG'));
B=im2double(imread('2.JPG'));
A=imresize(A, [512 512]);
B=imresize(B, [512 512]);
D=B-A;
D=D./iter;
for i=1:iter
A=A+D;
imshow(A);
pause (0.1);
end

Answers (2)

DGM
DGM on 13 Sep 2024
Edited: DGM on 13 Sep 2024
Here:
% inputs
A = imread('tape.png');
B = imread('strawberries.jpg');
% parameters
nframes = 20;
framedelay = 0.1;
ditherstr = 'dither'; % 'dither' or 'nodither'
ncolors = 256; % 2-256
outsize = size(A,1:2); % [y x]
outfile = 'junk.gif';
% cast and resize
A = imresize(im2double(A),outsize);
B = imresize(im2double(B),outsize);
% calculate frame delta
% first frame is unaltered, so the denominator is N-1, not N
D = (B-A)/(nframes-1);
% while gray2ind() exists, it only takes grayscale inputs
% and it can only do uniform quantization. just use rgb2ind().
% expand here so we don't have to do it in the loop
if size(A,3) == 1
A = repmat(A,[1 1 3]);
end
% process and write
for f = 1:nframes
% the first frame is unaltered
if f ~= 1
A = A + D;
end
% convert to indexed
[thisframe thisCT] = rgb2ind(A,min(max(ncolors,2),256),ditherstr);
% write the frame
if f == 1
imwrite(thisframe,thisCT,outfile,'gif','DelayTime',framedelay,'Loopcount',Inf);
else
imwrite(thisframe,thisCT,outfile,'gif','DelayTime',framedelay,'WriteMode','append');
end
end
Or if you're using MIMT and can spare the memory, this is way easier to write.
% inputs
A = imread('tape.png');
B = imread('strawberries.jpg');
% parameters
nframes = 20;
framedelay = 0.1;
ditherstr = 'dither'; % 'dither' or 'nodither'
ncolors = 256; % 2-256
outsize = size(A,1:2); % [y x]
outfile = 'junk.gif';
% cast and resize
A = imresize(im2double(A),outsize);
B = imresize(im2double(B),outsize);
% calculate alpha vector
alpha = permute(linspace(1,0,nframes),[1 3 4 2]);
% construct 4D image
outstack = A.*alpha + B.*(1-alpha);
% write to a GIF
gifwrite(outstack,outfile,framedelay,ditherstr,'ncolors',ncolors) % MIMT-only

Image Analyst
Image Analyst on 13 Sep 2024
That is not face morphing. Morphing is not just a weighted sum of images. The image needs to be warped before adding. So you need to define which points from the first image will be mapped (moved) to which points in the final image. Then intermediate images can have those points moved an interpolated distance. For more info see
There are probably File Exhange entries on the topic so you should check those out.
To create a GIF animated file, see the official Mathworks Answer:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!