您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么利用Matlab一鍵生成工地海報特效”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
首先m文件所在文件夾內應該有如下兩張圖:
此外還需要一張近似方形的圖片也要一起放在文件夾中,之后通過以下代碼導入和初步處理圖片(選擇幕布部分和去色代碼,可左右滑動):
bkgPic=imread('bkg.jpg'); % 背景圖 screenPic=bkgPic(190:555,(190:555)+160,:);% 幕布區域圖片 grayscreenPic=rgb2gray(screenPic); % 灰度化幕布區域圖片 screenRange=imread('screenRange.jpg'); % 幕布范圍圖(用來扣幕布邊緣細節) screenRange=rgb2gray(screenRange); forePic=imread('2048.jpg'); % 要做成海報的圖片
我們可以看出幕布是有些傾斜的,我們為了讓圖片和幕布更加貼近,我們將圖片進行傾斜處理并將其調整至與幕布區域相似的大小:
% 前景圖片變形============================================================= tform=affine2d([1 -0.02 0;-0.02 1 0; 0 0 1]); forePic=imwarp(forePic,tform); exforePic=imresize(forePic,size(grayscreenPic)+26);
這部分參考之前布料貼圖的部分呀:
% 扭曲置換================================================================= for i=1:size(grayscreenPic,1) for j=1:size(grayscreenPic,2) goffset=(double(grayscreenPic(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
% 正交疊底================================================================= newforePic=uint8((double(newforePic).*double(grayscreenPic))./220); % imshow(newforePic) % 舊版本的用下面這段代碼 % newforePicR=double(newforePic(:,:,1)).*double(grayscreenPic)./220; % newforePicG=double(newforePic(:,:,2)).*double(grayscreenPic)./220; % newforePicB=double(newforePic(:,:,3)).*double(grayscreenPic)./220; % newforePic(:,:,1)=newforePicR; % newforePic(:,:,2)=newforePicG; % newforePic(:,:,3)=newforePicB; % newforePic=uint8(newforePic);
上方正交疊底后,幕布外面還是灰色,而且幕布內容部分超出幕布范圍,我們可以用幕布范圍圖修整幕布:
% 邊緣修整================================================================= screenPicR=screenPic(:,:,1);newforePicR=newforePic(:,:,1); screenPicG=screenPic(:,:,2);newforePicG=newforePic(:,:,2); screenPicB=screenPic(:,:,3);newforePicB=newforePic(:,:,3); screenPicR(screenRange>20)=newforePicR(screenRange>20); screenPicG(screenRange>20)=newforePicG(screenRange>20); screenPicB(screenRange>20)=newforePicB(screenRange>20); screenPic(:,:,1)=screenPicR; screenPic(:,:,2)=screenPicG; screenPic(:,:,3)=screenPicB; screenPic=uint8(screenPic);
% 將背景圖幕布區域換為新圖================================================= bkgPic(190:555,(190:555)+160,:)=screenPic; imshow(bkgPic)
function consPoster bkgPic=imread('bkg.jpg'); % 背景圖 screenPic=bkgPic(190:555,(190:555)+160,:);% 幕布區域圖片 grayscreenPic=rgb2gray(screenPic); % 灰度化幕布區域圖片 screenRange=imread('screenRange.jpg'); % 幕布范圍圖(用來扣幕布邊緣細節) screenRange=rgb2gray(screenRange); forePic=imread('2048.jpg'); % 要做成海報的圖片 % 前景圖片變形============================================================= tform=affine2d([1 -0.02 0;-0.02 1 0; 0 0 1]); forePic=imwarp(forePic,tform); exforePic=imresize(forePic,size(grayscreenPic)+26); % 扭曲置換================================================================= for i=1:size(grayscreenPic,1) for j=1:size(grayscreenPic,2) goffset=(double(grayscreenPic(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 % 正交疊底================================================================= newforePic=uint8((double(newforePic).*double(grayscreenPic))./220); % imshow(newforePic) % 舊版本的用下面這段代碼 % newforePicR=double(newforePic(:,:,1)).*double(grayscreenPic)./220; % newforePicG=double(newforePic(:,:,2)).*double(grayscreenPic)./220; % newforePicB=double(newforePic(:,:,3)).*double(grayscreenPic)./220; % newforePic(:,:,1)=newforePicR; % newforePic(:,:,2)=newforePicG; % newforePic(:,:,3)=newforePicB; % newforePic=uint8(newforePic); % 邊緣修整================================================================= screenPicR=screenPic(:,:,1);newforePicR=newforePic(:,:,1); screenPicG=screenPic(:,:,2);newforePicG=newforePic(:,:,2); screenPicB=screenPic(:,:,3);newforePicB=newforePic(:,:,3); screenPicR(screenRange>20)=newforePicR(screenRange>20); screenPicG(screenRange>20)=newforePicG(screenRange>20); screenPicB(screenRange>20)=newforePicB(screenRange>20); screenPic(:,:,1)=screenPicR; screenPic(:,:,2)=screenPicG; screenPic(:,:,3)=screenPicB; screenPic=uint8(screenPic); % imshow(screenPic) % 將背景圖幕布區域換為新圖================================================= bkgPic(190:555,(190:555)+160,:)=screenPic; imshow(bkgPic) end
“怎么利用Matlab一鍵生成工地海報特效”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。