教你用 MATLAB 一键生成建筑工地海报,谁还不是个打工人了

Posted slandarer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了教你用 MATLAB 一键生成建筑工地海报,谁还不是个打工人了相关的知识,希望对你有一定的参考价值。

使用效果:
在这里插入图片描述
在这里插入图片描述
以下是过程描述:

1 图片准备和导入

首先m文件所在文件夹内应该有如下两张图:
在这里插入图片描述
这俩图和完整代码可以从如下链接获取:
链接:
https://pan.baidu.com/s/17IGkFv8AzSq44sG5DiNZ9w
提取码: wq5o

此外还需要一张近似方形的图片也要一起放在文件夹中,之后通过以下代码导入和初步处理图片(选择幕布部分和去色):

bkgPic=imread('bkg.jpg');% 背景图
screenPic=bkgPic(190:555,(190:555)+160,:);% 幕布区域图片
grayscreenPic=rgb2gray(screenPic);% 灰度化幕布区域图片

screenRange=imread('screenRange.jpg');%幕布范围图(用来扣幕布边缘细节)
screenRange=rgb2gray(screenRange);

forePic=imread('untitled2.png');% 要做成海报的图片

在这里插入图片描述

2 图像倾斜

我们可以看出幕布是有些倾斜的,我们为了让图片和幕布更加贴近,我们将突破进行倾斜处理并将其调整至与幕布区域相似的大小:

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

在这里插入图片描述

3 像素映射

这部分参考之前布料贴图的部分呀(点击图片跳转链接):

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

在这里插入图片描述

4 正交叠底

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

在这里插入图片描述

5 边缘修整

上方正交叠底后,幕布外面还是灰色,而且幕布内容部分超出幕布范围,我们可以用幕布范围图修整幕布:

% 边缘修整=================================================================
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);

在这里插入图片描述

6 将背景图幕布区域更替

% 将背景图幕布区域换为新图=================================================
bkgPic(190:555,(190:555)+160,:)=screenPic;
imshow(bkgPic)

在这里插入图片描述

7 完整代码

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一键生成高质量色卡

还在纠结配色问题?手把手教你用MATLAB一键生成高质量色卡

手对手的教你用canvas画一个简单的海报

七夕节还不知道送啥,教你用MATLAB制作一款刮刮乐抽奖特效,让她的手气决定她的礼物

七夕节还不知道送啥,教你用MATLAB制作一款刮刮乐抽奖特效,让她的手气决定她的礼物

PS教程:大神教你用PS制作《大鱼海棠》海报