温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-03-30 19:19:11 来源:亿速云 阅读:143 作者:iii 栏目:开发技术

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

1.使用效果

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

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

2.图片准备和导入

首先m文件所在文件夹内应该有如下两张图:

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

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

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

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');               % 要做成海报的图片

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

3.图像倾斜

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

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

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

4.扭曲置换

这部分参考之前布料贴图的部分呀:

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

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

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

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

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

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

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

7.背景图像替换

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

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI