亲宝软件园·资讯

展开

Matlab海报特效

slandarer 人气:0

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

加载全部内容

相关教程
猜你喜欢
用户评论