利用Matlab制作抖音同款含褶皺面料圖
效果如下


步驟
1.導(dǎo)入圖片
我們需要導(dǎo)入一張褶皺圖片(background.jpg)以及一張前景圖片(foreground.jpg),將褶皺圖片灰度化,將前景圖調(diào)整至與褶皺圖片相同大?。?/p>
bkgPic=imread('background.jpg');
bkgPic=rgb2gray(bkgPic);
forePic=imread('foreground.jpg');
forePic=imresize(forePic,size(bkgPic));
原圖在這里:


2.圖片擴(kuò)張
因?yàn)槲覀円獙?duì)前景圖片進(jìn)行拉伸,難免邊角處缺一塊,因此我們首先將邊緣處顏色往外擴(kuò)展幾圈(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
擴(kuò)展后圖片(圖片下側(cè)明顯一點(diǎn)):

3.像素映射
原理借鑒ps扭曲置換的原理,亮度較大的像素(大于128)取右下角像素RGB值進(jìn)行置換,亮度較小的像素(小于128)取左上角像素RGB值進(jìn)行置換,由于
(255-128)/10=12.7
(0-128)/10=-12.8
各個(gè)像素點(diǎn)與替換像素點(diǎn)的距離不超過13,因此上一步共擴(kuò)展了13圈。
同時(shí)因?yàn)楦鱾€(gè)像素分布為整數(shù)點(diǎn)位置,而位置差計(jì)算一般都不是整數(shù),因此我們要對(duì)偏移距離向上向下取整,獲得兩個(gè)像素點(diǎn)RGB值,并對(duì)這兩點(diǎn)數(shù)值進(jìn)行線性插值即可
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
像素值映射結(jié)果:

4.正片疊底
將兩張圖片疊加起來
公式:混合色×基色 / 255=結(jié)果色
由于正片疊底后所出圖片較暗,這里我們選擇除以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);
改為(三個(gè)通道分別處理):
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);
到此這篇關(guān)于利用Matlab制作抖音同款含褶皺面料圖的文章就介紹到這了,更多相關(guān)Matlab褶皺面料圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Matlab實(shí)現(xiàn)數(shù)據(jù)的動(dòng)態(tài)顯示方法
這篇文章主要為大家詳細(xì)介紹了Matlab使用Plot函數(shù)實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)顯示方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
C語言中動(dòng)態(tài)內(nèi)存管理圖文詳解
在編寫程序時(shí),通常并不知道需要處理的數(shù)據(jù)量,或者難以評(píng)估所需處理數(shù)據(jù)量的變動(dòng)程度,下面這篇文章主要給大家介紹了關(guān)于C語言中動(dòng)態(tài)內(nèi)存管理的相關(guān)資料,需要的朋友可以參考下2022-06-06
C語言數(shù)據(jù)結(jié)構(gòu)中樹與森林專項(xiàng)詳解
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)中樹與森林,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11
MATLAB中subplot函數(shù)的語法與使用實(shí)例
subplot()是將多個(gè)圖畫到一個(gè)平面上的工具,下面這篇文章主要給大家介紹了關(guān)于MATLAB中subplot函數(shù)的語法與使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
C++中vector和數(shù)組之間的轉(zhuǎn)換及其效率問題詳解
c++?vector轉(zhuǎn)數(shù)組是一種將vector容器的元素轉(zhuǎn)換為數(shù)組的方法,主要能幫助提高程序的性能和效率,下面這篇文章主要給大家介紹了關(guān)于C++中vector和數(shù)組之間的轉(zhuǎn)換及其效率問題的相關(guān)資料,需要的朋友可以參考下2023-03-03
C語言實(shí)現(xiàn)簡單學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單學(xué)生成績管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

