利用Matlab制作抖音同款含褶皺面料圖
效果如下
步驟
1.導(dǎo)入圖片
我們需要導(dǎo)入一張褶皺圖片(background.jpg)以及一張前景圖片(foreground.jpg),將褶皺圖片灰度化,將前景圖調(diào)整至與褶皺圖片相同大小:
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)文章
C語言中查詢進(jìn)程信號(hào)是否被遮罩或擱置的簡單方法
這篇文章主要介紹了C語言中查詢進(jìn)程信號(hào)是否被遮罩或擱置的簡單方法,包括sigprocmask函數(shù)和sigpending函數(shù)的簡介,需要的朋友可以參考下2015-09-09LeetCode題解C++生成每種字符都是奇數(shù)個(gè)的字符串
這篇文章主要為大家介紹了LeetCode題解C++生成每種字符都是奇數(shù)個(gè)的字符串示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10淺析C++?atomic?和?memory?ordering
這篇文章主要介紹了C++?atomic?和?memory?ordering的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04線程崩潰不會(huì)導(dǎo)致?JVM?崩潰的原因解析
網(wǎng)上看到一個(gè)很有意思的據(jù)說是美團(tuán)的面試題:為什么線程崩潰崩潰不會(huì)導(dǎo)致?JVM?崩潰,這個(gè)問題我看了不少回答,但都沒答到根本原因,所以決定答一答,相信大家看完肯定會(huì)有收獲,本文分以下幾節(jié)來探討,需要的朋友可以參考下2022-06-06C語言數(shù)據(jù)結(jié)構(gòu)之順序數(shù)組的實(shí)現(xiàn)
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之順序數(shù)組的實(shí)現(xiàn)的相關(guān)資料,這里提供實(shí)現(xiàn)實(shí)例,希望通過本文能幫助到大家,需要的朋友可以參考下2017-08-08C語言實(shí)現(xiàn)學(xué)生管理系統(tǒng)的源碼分享
這篇文章主要為大家詳細(xì)介紹了如何利用C語言實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07C語言中strspn()函數(shù)和strcspn()函數(shù)的對(duì)比使用
這篇文章主要介紹了C語言中strspn()函數(shù)和strcspn()函數(shù)的對(duì)比使用,strspn是計(jì)算屬于字符串的字符數(shù)而strcspn則是判斷不屬于,需要的朋友可以參考下2015-08-08