首頁 > 軟體

利用Matlab製作抖音同款含褶皺面料圖

2022-03-01 13:04:10

效果如下

步驟

1.匯入圖片

我們需要匯入一張褶皺圖片(background.jpg)以及一張前景圖片(foreground.jpg),將褶皺圖片灰度化,將前景圖調整至與褶皺圖片相同大小:

bkgPic=imread('background.jpg');
bkgPic=rgb2gray(bkgPic);
forePic=imread('foreground.jpg');
forePic=imresize(forePic,size(bkgPic));

原圖在這裡:

2.圖片擴張

因為我們要對前景圖片進行拉伸,難免邊角處缺一塊,因此我們首先將邊緣處顏色往外擴充套件幾圈(13圈)

exforePic=uint8(zeros(size(forePic)+[26,26,0]));
exforePic(14:end-13,14:end-13,1)=forePic(:,:,1);
exforePic(14:end-13,14:end-13,2)=forePic(:,:,2);
exforePic(14:end-13,14:end-13,3)=forePic(:,:,3);

for i=1:13
    exforePic(i,14:end-13,:)=forePic(1,:,:);
    exforePic(end+1-i,14:end-13,:)=forePic(end,:,:);
    exforePic(14:end-13,i,:)=forePic(:,1,:);
    exforePic(14:end-13,end+1-i,:)=forePic(:,end,:);
end
for i=1:3
    exforePic(1:13,1:13,i)=forePic(1,1,i);
    exforePic(end-13:end,end-13:end,i)=forePic(end,end,i);
    exforePic(end-13:end,1:13,i)=forePic(end,1,i);
    exforePic(1:13,end-13:end,i)=forePic(1,end,i);
end

擴充套件後圖片(圖片下側明顯一點):

3.畫素對映

原理借鑑ps扭曲置換的原理,亮度較大的畫素(大於128)取右下角畫素RGB值進行置換,亮度較小的畫素(小於128)取左上角畫素RGB值進行置換,由於

(255-128)/10=12.7

(0-128)/10=-12.8

各個畫素點與替換畫素點的距離不超過13,因此上一步共擴充套件了13圈。

同時因為各個畫素分佈為整數點位置,而位置差計算一般都不是整數,因此我們要對偏移距離向上向下取整,獲得兩個畫素點RGB值,並對這兩點數值進行線性插值即可

newforePic=uint8(zeros(size(forePic)));
for i=1:size(bkgPic,1)
    for j=1:size(bkgPic,2)
        goffset=(double(bkgPic(i,j))-128)/10;
        offsetLim1=floor(goffset)+13;
        offsetLim2=ceil(goffset)+13;
        sep1=goffset-floor(goffset);
        sep2=ceil(goffset)-goffset;
        c1=double(exforePic(i+offsetLim1,j+offsetLim1,:));
        c2=double(exforePic(i+offsetLim2,j+offsetLim2,:));
        if sep1==0
            c=double(exforePic(i+offsetLim1,j+offsetLim1,:));
        else
            c=c2.*sep1+c1.*sep2;
        end
        newforePic(i,j,:)=c;
    end
end

畫素值對映結果:

4.正片疊底

將兩張圖片疊加起來

公式:混合色×基色 / 255=結果色

由於正片疊底後所出圖片較暗,這裡我們選擇除以220而不是255:

newforePic=uint8((double(newforePic).*double(bkgPic))./220);
imwrite(newforePic,'result.jpg')
imshow(newforePic)

5.完整程式碼

function clothFold
bkgPic=imread('background.jpg');
bkgPic=rgb2gray(bkgPic);
forePic=imread('foreground.jpg');
forePic=imresize(forePic,size(bkgPic));

exforePic=uint8(zeros(size(forePic)+[26,26,0]));
exforePic(14:end-13,14:end-13,1)=forePic(:,:,1);
exforePic(14:end-13,14:end-13,2)=forePic(:,:,2);
exforePic(14:end-13,14:end-13,3)=forePic(:,:,3);

for i=1:13
    exforePic(i,14:end-13,:)=forePic(1,:,:);
    exforePic(end+1-i,14:end-13,:)=forePic(end,:,:);
    exforePic(14:end-13,i,:)=forePic(:,1,:);
    exforePic(14:end-13,end+1-i,:)=forePic(:,end,:);
end
for i=1:3
    exforePic(1:13,1:13,i)=forePic(1,1,i);
    exforePic(end-13:end,end-13:end,i)=forePic(end,end,i);
    exforePic(end-13:end,1:13,i)=forePic(end,1,i);
    exforePic(1:13,end-13:end,i)=forePic(1,end,i);
end
    
newforePic=uint8(zeros(size(forePic)));
for i=1:size(bkgPic,1)
    for j=1:size(bkgPic,2)
        goffset=(double(bkgPic(i,j))-128)/10;
        offsetLim1=floor(goffset)+13;
        offsetLim2=ceil(goffset)+13;
        sep1=goffset-floor(goffset);
        sep2=ceil(goffset)-goffset;
        c1=double(exforePic(i+offsetLim1,j+offsetLim1,:));
        c2=double(exforePic(i+offsetLim2,j+offsetLim2,:));
        if sep1==0
            c=double(exforePic(i+offsetLim1,j+offsetLim1,:));
        else
            c=c2.*sep1+c1.*sep2;
        end
        newforePic(i,j,:)=c;
    end
end

%grayForePic=rgb2gray(newforePic);
%rate=double(bkgPic)./double(grayForePic);

newforePic=uint8((double(newforePic).*double(bkgPic))./220);
imwrite(newforePic,'result.jpg')
imshow(newforePic)
end

注:

若是17年及之前版本,需將程式碼最後的

newforePic=uint8((double(newforePic).*double(bkgPic))./220);

改為(三個通道分別處理):

newforePic(:,:,1)=uint8((double(newforePic(:,:,1)).*double(bkgPic))./220);
newforePic(:,:,2)=uint8((double(newforePic(:,:,2)).*double(bkgPic))./220);
newforePic(:,:,3)=uint8((double(newforePic(:,:,3)).*double(bkgPic))./220);

到此這篇關於利用Matlab製作抖音同款含褶皺面料圖的文章就介紹到這了,更多相關Matlab褶皺面料圖內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com