Use matlab to make raster animation (Moiré fringe animation)

Use matlab to make raster animation (Moiré fringe animation)


Raster animation is an animation method that uses a transparent grating to move quickly across the film, making it look like the film is moving. This kind of animation does not depend on electronic media and gifs, and only needs a prepared negative and a grating pattern printed on a transparent plastic sheet to experience the effect of the animation (of course, the electronic version of the ppt is also available).

This article tries to analyze the principle of raster animation, and use matlab programming to realize raster animation through this principle.

1 principle

The principle of grating animation first uses the continuity principle and closure principle of perceptual organization in psychology, so that people can use the broken information to make up a whole information, such as the following picture: After the broken information and the occlusion of the grating
PPT drawing, very ugly
, People will naturally imagine the picture of the two gears below.

Afterwards, the principle of raster animation is to stitch together the previously broken information in sequence, and as the position of the raster changes, it becomes a different pattern. Combining different broken patterns at different grating positions creates the negative.

Therefore, the general raster animation has a single color (single color is easy to brain supplement), short animation cycle (reduced grating spacing is conducive to brain supplement), simple animation and mainly thick lines or large blocks (refer to the principle of closure).

2 Expected animation preparation

Here, the double gear animation in the first section is still used as a demonstration to show the animation effect expected to be demonstrated. Choose T=6 for the exercise cycle, and it is not recommended to choose too large (of course, if it is too small, you will not get the effect).

clear
close all
figure('Color','white')
N=17;
T=6;
for k=1:T
    clf
    dt=2*pi/N/T*k;
    %第一个齿轮
    hold on
    for j=1:N
        theta=2*pi/N*j;
        R=1.1;
        plot([-1,-1+R*cos(theta+dt)],[0,R*sin(theta+dt)],'k','linewidth',5)
    end
    rectangle('Position',[-1.9,-0.9,1.8,1.8],'Curvature',1,'LineWidth',8)
    hold off
    %第二个齿轮
    hold on
    for j=1:N
        theta=2*pi/N*j;
        R=1.1;
        dth=2*pi/N/2;
        plot([1,1-R*cos(theta+dth+dt)],[0,R*sin(theta+dth+dt)],'k','linewidth',5)
    end
    rectangle('Position',[0.1,-0.9,1.8,1.8],'Curvature',1,'LineWidth',8)
    hold off
    
    xlim([-3,3])
    ylim([-2,2])
    axis off
    axis equal
    pause(0.2)
    
    
    %绘制动图
    cdata = print('-RGBImage','-r100'); %保存Figure上的图像
    frame.cdata=cdata;
    frame.colormap=[];
    imind=frame2im(frame);
    [imind,cm]=rgb2ind(imind,2);
    if k==1
         imwrite(imind,cm,'test.gif','gif', 'Loopcount',inf,'DelayTime',0.2);
    else
         imwrite(imind,cm,'test.gif','gif','WriteMode','append','DelayTime',0.2);
    end

end

The renderings are as follows:
This is a GIF

3 Negative drawing

Negatives are drawn relative to animation period and raster width. Since the previously set animation cycle is 6, the negative film is drawn in 6 times. The raster width is set to 1 here, that is, the width of each slit is 1 pixel.

code show as below:

%绘制底片

close all
figure('Color','white')
N=17;
T=6;
for k=1:T
    clf
    dt=2*pi/N/T*k;
    %第一个齿轮
    hold on
    for j=1:N
        theta=2*pi/N*j;
        R=1.1;
        plot([-1,-1+R*cos(theta+dt)],[0,R*sin(theta+dt)],'k','linewidth',5)
    end
    rectangle('Position',[-1.9,-0.9,1.8,1.8],'Curvature',1,'LineWidth',8)
    hold off
    %第二个齿轮
    hold on
    for j=1:N
        theta=2*pi/N*j;
        R=1.1;
        dth=2*pi/N/2;
        plot([1,1-R*cos(theta+dth+dt)],[0,R*sin(theta+dth+dt)],'k','linewidth',5)
    end
    rectangle('Position',[0.1,-0.9,1.8,1.8],'Curvature',1,'LineWidth',8)
    hold off
    
    xlim([-3,3])
    ylim([-2,2])
    axis off
    axis equal
    pause(0.2)
    
    
    %绘制底片
    cdata = print('-RGBImage','-r100');
    Size_cdata=size(cdata);
    if k==1
        cdata_N=cdata;
    else
        %每次绘制1个像素,间隔为1*T
        cdata_N(:,k:T:Size_cdata(2),:)=cdata(:,k:T:Size_cdata(2),:);
    end

end

clf
imshow(cdata_N)%展示底片
save('cdata_N.mat','cdata_N')%保存,待会要用

The result after negative drawing is as follows:
Negative image

4 Raster animation

Then use this raster negative image (saved as a mat file in the previous section) to make a raster animation image.

Here I first use the previous negatives to make a transparent grating in png format, save it in PPT, and add animation playback effects to play. Of course, if you can make the grating yourself, you can actually make it.

clear
clc

load('cdata_N.mat')
cdata_N=double(cdata_N);
cdata_Z=255+zeros(size(cdata_N));
%绘制光栅
cdata_Z(119:319,120:480,:)=0;
cdata_Z(119:319,120:T:480,:)=255;
imwrite(cdata_Z,'GuangShan.png','Alpha',cdata_Z(:,:,1)/255); %保存透明图像

The generated picture is as follows:
insert image description here

The final effect can also be demonstrated in matlab:

%绘制变形图和光栅叠加的动图
clear
close all
figure('Color','white')
N=17;
T=6;
%处理
load('cdata_N.mat')
cdata_N=double(cdata_N);
Size_cdata=size(cdata_N);
cdata_N=[cdata_N,255*ones(Size_cdata(1),00,Size_cdata(3))];

%生成size
Size_cdata=size(cdata_N);

k=[1:120,119:-1:2];
for j=1:length(k)
    t=k(j);
    cdata_Z=255*ones(Size_cdata);
    %绘制光栅
    cdata_Z(119:319,250-t:550-t,:)=0;
    cdata_Z(119:319,250-t:T:550-t,:)=255;
    %imshow(cdata_Z)
    %叠加
    cdata_Combine =imlincomb(1,cdata_N,1,cdata_Z,-255);
    imshow(cdata_Combine)
    pause(0.1)
end

The final effect is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42943114/article/details/103412616