matlab練習程式(波紋扭曲)

Dsp Tian發表於2014-07-19

其實就是用sin或cos對x,y座標進行變換,處理的時候依然是反向變換。

類似的,用不同的函式能得到不同的扭曲效果,比如log,1/x,exp等等。

效果如下:

程式碼如下(還給出瞭如何生成gif圖片的程式碼):

clear all;close all;clc;

img=imread('lena.jpg');
[h w]=size(img);

wave=[10,100]; %[幅度,週期]
newh=h+2*wave(1);
neww=w+2*wave(1);
rot=0;

for i=1:10
    imgn=zeros(newh,neww);    

    rot=rot+0.2;
    for y=1:newh
        for x=1:neww

            yy=round((y-wave(1))-(wave(1)*cos(2*pi/wave(2)*x+rot)));    %依然是逆變換
            xx=round((x-wave(1))-(wave(1)*cos(2*pi/wave(2)*y+rot)));

           if yy>=1 && yy<=h && xx>=1 && xx<=w
                imgn(y,x)=img(yy,xx);
           end

        end
    end

    figure(1);
    imshow(imgn,[]);
    
    imgn(:,:,2)=imgn;       %生成gif圖片
    imgn(:,:,3)=imgn(:,:,1);
    [I,map]=rgb2ind(mat2gray(imgn),256);  

    if i==0
      imwrite(I,map,'re.gif','Loopcount',inf,'DelayTime',1.5);
    else
      imwrite(I,map,'re.gif','DelayTime',0.1,'WriteMode','Append');
    end

end

相關文章