MATLAB 制作抖音同款含褶皱面料图
MATLAB 制作 抖音 同款
2023-09-11 14:20:18 时间
效果如下:!!!
步骤
1.导入图片
我们需要导入一张褶皱图片(background.jpg)以及一张前景图片(foreground.jpg),将褶皱图片灰度化,将前景图调整至与褶皱图片相同大小:
bkgPic=imread('background.jpg');
bkgPic=rgb2gray(bkgPic);
forePic=imread('foreground.jpg');
forePic=imresize(forePic,size(bkgPic));
原图在这里:
2.图片扩张
因为我们要对前景图片进行拉伸,难免边角处缺一块,因此我们首先将边缘处颜色往外扩展几圈(13圈)
exforePic=uint8(zeros(size(forePic)+[26,26,0]));
exforePic(14:end-13,14:end-13,1)=forePic(:,:,1);
exforePic(14:end-13,14:end-13,2)=forePic(:,:,2);
exforePic(14:end-13,14:end-13,3)=forePic(:,:,3);
for i=1:13
exforePic(i,14:end-13,:)=forePic(1,:,:);
exforePic(end+1-i,14:end-13,:)=forePic(end,:,:);
exforePic(14:end-13,i,:)=forePic(:,1,:);
exforePic(14:end-13,end+1-i,:)=forePic(:,end,:);
end
for i=1:3
exforePic(1:13,1:13,i)=forePic(1,1,i);
exforePic(end-13:end,end-13:end,i)=forePic(end,end,i);
exforePic(end-13:end,1:13,i)=forePic(end,1,i);
exforePic(1:13,end-13:end,i)=forePic(1,end,i);
end
扩展后图片(图片下侧明显一点):
3.像素映射
原理借鉴ps扭曲置换的原理,亮度较大的像素(大于128)取右下角像素RGB值进行置换,亮度较小的像素(小于128)取左上角像素RGB值进行置换,由于
(255-128)/10=12.7
(0-128)/10=-12.8
各个像素点与替换像素点的距离不超过13,因此上一步共扩展了13圈。
同时因为各个像素分布为整数点位置,而位置差计算一般都不是整数,因此我们要对偏移距离向上向下取整,获得两个像素点RGB值,并对这两点数值进行线性插值即可
newforePic=uint8(zeros(size(forePic)));
for i=1:size(bkgPic,1)
for j=1:size(bkgPic,2)
goffset=(double(bkgPic(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.正片叠底
将两张图片叠加起来
公式:混合色×基色 / 255=结果色
由于正片叠底后所出图片较暗,这里我们选择除以220而不是255:
newforePic=uint8((double(newforePic).*double(bkgPic))./220);
imwrite(newforePic,'result.jpg')
imshow(newforePic)
5.完整代码
function clothFold
bkgPic=imread('background.jpg');
bkgPic=rgb2gray(bkgPic);
forePic=imread('foreground.jpg');
forePic=imresize(forePic,size(bkgPic));
exforePic=uint8(zeros(size(forePic)+[26,26,0]));
exforePic(14:end-13,14:end-13,1)=forePic(:,:,1);
exforePic(14:end-13,14:end-13,2)=forePic(:,:,2);
exforePic(14:end-13,14:end-13,3)=forePic(:,:,3);
for i=1:13
exforePic(i,14:end-13,:)=forePic(1,:,:);
exforePic(end+1-i,14:end-13,:)=forePic(end,:,:);
exforePic(14:end-13,i,:)=forePic(:,1,:);
exforePic(14:end-13,end+1-i,:)=forePic(:,end,:);
end
for i=1:3
exforePic(1:13,1:13,i)=forePic(1,1,i);
exforePic(end-13:end,end-13:end,i)=forePic(end,end,i);
exforePic(end-13:end,1:13,i)=forePic(end,1,i);
exforePic(1:13,end-13:end,i)=forePic(1,end,i);
end
newforePic=uint8(zeros(size(forePic)));
for i=1:size(bkgPic,1)
for j=1:size(bkgPic,2)
goffset=(double(bkgPic(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
%grayForePic=rgb2gray(newforePic);
%rate=double(bkgPic)./double(grayForePic);
newforePic=uint8((double(newforePic).*double(bkgPic))./220);
imwrite(newforePic,'result.jpg')
imshow(newforePic)
end
注:
若是17年及之前版本,需将代码最后的
newforePic=uint8((double(newforePic).*double(bkgPic))./220);
改为(三个通道分别处理):
newforePic(:,:,1)=uint8((double(newforePic(:,:,1)).*double(bkgPic))./220);
newforePic(:,:,2)=uint8((double(newforePic(:,:,2)).*double(bkgPic))./220);
newforePic(:,:,3)=uint8((double(newforePic(:,:,3)).*double(bkgPic))./220);
相关文章
- MATLAB 制作GIF图像
- matlab 点云最小二乘拟合空间球
- matlab 点云可视化(2)——pcshowpair()可视化两点云之间的差异
- 【转】C++调用Matlab的.m文件
- 【转载】利用Matlab制作钟表
- Matlab:成功解决In an assignment A(I)=B,the number of elements in B and I must be the same
- Matlab:序列分析法MATLAB代码
- 考虑寿命损耗的微网电池储能容量优化配置(Matlab代码实现)
- 基于 ANFIS 的非线性回归(Matlab代码实现)
- 负荷预测|一种改进支持向量机的电力负荷预测方法研究(Matlab代码实现)
- 基于MATLAB的车牌识别系统研究(Matlab代码实现)
- 【MATLAB】控制菜单的使用
- Matlab 棋盘制作
- MATLAB实现高斯混合分布的EM算法及二维时概率密度曲面、置信椭圆绘制
- Matlab R2018a无法重新加载 /usr/local/MATLAB/R2018a/bin/glnxa64/libmwxcp_dwarf.so
- Matlab:MATLAB GUI不同控件函数间变量传递的三种方法详解
- 【语音编码】基于matlab ADPCM编解码(Matlab代码实现)
- 【语音编码】基于matlab ADPCM编解码【G.723.1】(Matlab代码实现)
- m基于神经网络预测模型的室内温度调节控制系统matlab仿真
- 基于TPC算法的WSN网络资源分配matlab仿真
- 基于FPGA的图像sobel边缘提取算法实现,包含testbench和matlab验证程序
- Matlab Tips: 结构体递归式打印--Dump matlab struct content recursively
- Matlab使用笔记(三):matlab设置代码自动补全功能