首頁 > 軟體

利用Matlab一鍵生成工地海報特效

2022-03-29 19:01:32

1.使用效果

這篇的本質還是扭曲置換,其實看過前面幾篇文章的應該都能猜出是怎樣做到的,下面開始講解:

2.圖片準備和匯入

首先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');               % 要做成海報的圖片

3.影象傾斜

我們可以看出幕布是有些傾斜的,我們為了讓圖片和幕布更加貼近,我們將圖片進行傾斜處理並將其調整至與幕布區域相似的大小:

% 前景圖片變形=============================================================
tform=affine2d([1 -0.02 0;-0.02 1 0; 0 0 1]);
forePic=imwarp(forePic,tform);
exforePic=imresize(forePic,size(grayscreenPic)+26);

4.扭曲置換

這部分參考之前布料貼圖的部分呀:

% 扭曲置換=================================================================
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

5.正交疊底

% 正交疊底=================================================================
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);

6.緣修整(摳圖)

上方正交疊底後,幕布外面還是灰色,而且幕布內容部分超出幕布範圍,我們可以用幕布範圍圖修整幕布:

% 邊緣修整=================================================================
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);

7.背景影象替換

% 將背景圖幕布區域換為新圖=================================================
bkgPic(190:555,(190:555)+160,:)=screenPic;
imshow(bkgPic)

8.完整程式碼

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一鍵生成工地海報特效的詳細內容,更多關於Matlab海報特效的資料請關注it145.com其它相關文章!


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