Creating sill image animation

3 views (last 30 days)
David Levi
David Levi on 13 Jan 2022
Answered: VINAYAK LUHA on 28 Sep 2023
Hello everyone!
I have a project in matlab : to make an animation video from one source image.
My idea for solving this problem includes those steps :
1) Seperate foreground and background from each other (using image segmentation methods).
2) Fill the background with color - in the area of the segmented foreground.
3) Blend the foreground and backround images in a for loop. Which in every iteration the foreground moves a little from his last location (for now is is not important what is the direction - I assume that the user will decide ).
I am new in matlab and threfore I am having trouble doing this -the idea is clear but writing the code is not simple for me.
I need some examples if you have and want to share.
Thanks you all!

Answers (1)

VINAYAK LUHA
VINAYAK LUHA on 28 Sep 2023
Hi David,
I understanding that you want to create a still image animation in MATLAB.
Here are few pointers to help you implement the steps you mentioned:
  1. For separating the background, you may use MATLAB’s Image Segmenter or “Color Thresholder” app or programmatically make use of a variety of other custom image segmentation methods.
  2. For changing the background color, you can modify the individual (R, G, B) values of the image or add another image of same dimension to the background.
  3. Similarly for blending the background image with the foreground image you can follow step 2 and for translating the foreground on the colored background you may shift the foreground image circularly using circshift” function.
Examples of how to implement the above 3 steps can be seen in the code snippet below:
% Load the foreground and the background images
foreground = imread('foreground.png');
background = imread("background.png");
%Filling the background image with some color
rgb =[125,125,125];
background(:,:,1) = rgb(1);
background(:,:,2) = rgb(2);
background(:,:,3) = rgb(3);
%Blend the foreground and the background images in loop
numFrames = 180;
foregroundOffset = 15;
videoFile = VideoWriter('wave.mp4', 'MPEG-4');
videoFile.FrameRate = 60;
open(videoFile);
for frame = 1:numFrames
offset = frame * foregroundOffset;
shiftedForeground = circshift(foreground, [0,offset]);
blendedImage = (double(shiftedForeground) + double(background)) ./ 2;
writeVideo(videoFile, uint8(blendedImage));
end
close(videoFile);
To learn more about more image segmentation techniques and circshift, you may refer to the MathWorks documentation links below:
  1. https://www.mathworks.com/discovery/image-segmentation.html
  2. https://www.mathworks.com/help/matlab/ref/circshift.html
Hope this helps.
Regards
Vinayak Luha

Community Treasure Hunt

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

Start Hunting!