目录1.使用效果2.图片准备和导入3.图像倾斜4.扭曲置换5.正交叠底6.缘修整(抠图)7.背景图像替换8.完整代码1.使用效果 这篇的本质还是扭曲置换,其实看过前面几篇文章的应
这篇的本质还是扭曲置换,其实看过前面几篇文章的应该都能猜出是怎样做到的,下面开始讲解:
首先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一键生成工地海报特效的详细内容,更多关于Matlab海报特效的资料请关注编程网其它相关文章!
--结束END--
本文标题: 利用Matlab一键生成工地海报特效
本文链接: https://lsjlt.com/news/144377.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0