MATLAB 制作抖音同款突出效果海报

效果如下:
在这里插入图片描述
在这里插入图片描述

步骤
1.导入图片,获取每个区域的平均颜色,构造随机数矩阵

导入图片后我们首先将图片划分成很多个15x15(可自行调整大小)的小格子,并求取每个格子的颜色平均值,作为柱状图每个小柱子的颜色,构造随机数矩阵作为每个小柱子的高度

oriPic=imread('test.jpg');
pixLen=15;
[H,W,~]=size(oriPic);


XList=1:pixLen:W-pixLen;
YList=1:pixLen:H-pixLen;
samplePic=zeros(length(YList),length(XList),3);
for x=XList
    for y=YList
        sampleSquareR=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,1));
        sampleSquareG=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,2));
        sampleSquareB=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,3));
        samplePic(y==YList,x==XList,1)=mean(sampleSquareR(:));
        samplePic(y==YList,x==XList,2)=mean(sampleSquareG(:));
        samplePic(y==YList,x==XList,3)=mean(sampleSquareB(:));
    end
end
randiMat=randi([0,250],[length(YList),length(XList)]);

原始图片:
在这里插入图片描述
如果用方块颜色矩阵和方块高度矩阵直接去画图的话,会因为每个颜色只有一个点,而出现如下的效果,
在这里插入图片描述
为了画出一个一个小平台的效果,我们要将每个点扩充成15X15个颜色相同,高度相同的点集

2.点集扩充,曲面绘制
expCMat=zeros(length(YList)*pixLen,length(XList)*pixLen,3);
expZMat=zeros(length(YList)*pixLen,length(XList)*pixLen);
for x=XList
    for y=YList
        expCMat(y:y-1+pixLen,x:x-1+pixLen,1)=samplePic(y==YList,x==XList,1);
        expCMat(y:y-1+pixLen,x:x-1+pixLen,2)=samplePic(y==YList,x==XList,2);
        expCMat(y:y-1+pixLen,x:x-1+pixLen,3)=samplePic(y==YList,x==XList,3);
        expZMat(y:y-1+pixLen,x:x-1+pixLen)=randiMat(y==YList,x==XList);
    end
end
[XMesh,YMesh]=meshgrid(1:length(XList)*pixLen,1:length(YList)*pixLen);
surf(XMesh,expZMat,YMesh(end:-1:1,:),'CData',expCMat./255,...
    'FaceColor','interp','edgeColor','none',...
    'SpecularStrength',0,'AmbientStrength',1,'DiffuseStrength',1)

效果:
我们会发现此时每个小方块界限并不明显,而且当正面看时,曲面几乎看不出任何突出效果,这两点可以通过设置前缩透视法,和增添光照来解决
在这里插入图片描述

在这里插入图片描述

3.坐标区属性设置及前缩透视法

以下大部分代码为了调整视角和颜色,ax.Projection=‘perspective’; 用来设置前缩透视

ax=gca;
hold(ax,'on')
ax.Projection='perspective'; 
ax.Color=[0.95 0.95 0.95];
ax.DataAspectRatioMode='manual';
ax.DataAspectRatio=[1,0.1,1];
ax.XLim=[0,W];
ax.ZLim=[0,H];
ax.View=[-0.4830   -0.0717];
ax.XTick=[];
ax.YTick=[];
ax.ZTick=[];
ax.XColor='none';
ax.YColor='none';
ax.ZColor='none';

效果:

4.光照设置

可以根据自己的喜好微调哦

h = light;
h.Style='local';
h.Color=[1,1,1].*0.5;
h.Position=[W/2,-1000,0];

效果:

5.完整代码
function pieceWisePic

oriPic=imread('test4.jpg');
pixLen=15;
[H,W,~]=size(oriPic);


XList=1:pixLen:W-pixLen;
YList=1:pixLen:H-pixLen;
samplePic=zeros(length(YList),length(XList),3);
for x=XList
    for y=YList
        sampleSquareR=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,1));
        sampleSquareG=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,2));
        sampleSquareB=double(oriPic(y:y-1+pixLen,x:x-1+pixLen,3));
        samplePic(y==YList,x==XList,1)=mean(sampleSquareR(:));
        samplePic(y==YList,x==XList,2)=mean(sampleSquareG(:));
        samplePic(y==YList,x==XList,3)=mean(sampleSquareB(:));
    end
end
randiMat=randi([0,250],[length(YList),length(XList)]);

expCMat=zeros(length(YList)*pixLen,length(XList)*pixLen,3);
expZMat=zeros(length(YList)*pixLen,length(XList)*pixLen);
for x=XList
    for y=YList
        expCMat(y:y-1+pixLen,x:x-1+pixLen,1)=samplePic(y==YList,x==XList,1);
        expCMat(y:y-1+pixLen,x:x-1+pixLen,2)=samplePic(y==YList,x==XList,2);
        expCMat(y:y-1+pixLen,x:x-1+pixLen,3)=samplePic(y==YList,x==XList,3);
        expZMat(y:y-1+pixLen,x:x-1+pixLen)=randiMat(y==YList,x==XList);
    end
end
[XMesh,YMesh]=meshgrid(1:length(XList)*pixLen,1:length(YList)*pixLen);
surf(XMesh,expZMat,YMesh(end:-1:1,:),'CData',expCMat./255,...
    'FaceColor','interp','edgeColor','none',...
    'SpecularStrength',0,'AmbientStrength',1,'DiffuseStrength',1)


ax=gca;
hold(ax,'on')
ax.Projection='perspective'; 
ax.Color=[0.95 0.95 0.95];
ax.DataAspectRatioMode='manual';
ax.DataAspectRatio=[1,0.1,1];
ax.XLim=[0,W];
ax.ZLim=[0,H];
ax.View=[-0.4830   -0.0717];
ax.XTick=[];
ax.YTick=[];
ax.ZTick=[];
ax.XColor='none';
ax.YColor='none';
ax.ZColor='none';

h = light;
h.Style='local';
h.Color=[1,1,1].*0.5;
h.Position=[W/2,-1000,0];

end

尝试的其他几个图
在这里插入图片描述
当方块取的较小时(5x5)
在这里插入图片描述

另:
在这里插入图片描述

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

slandarer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值