千家信息网

怎么利用Matlab一键生成工地海报特效

发表于:2024-11-23 作者:千家信息网编辑
千家信息网最后更新 2024年11月23日,本篇内容介绍了"怎么利用Matlab一键生成工地海报特效"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成
千家信息网最后更新 2024年11月23日怎么利用Matlab一键生成工地海报特效

本篇内容介绍了"怎么利用Matlab一键生成工地海报特效"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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;    endend

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 consPosterbkgPic=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;    endend% 正交叠底=================================================================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一键生成工地海报特效"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0