这篇文章主要介绍了如何利用Matlab制作抖音的同款含褶皱面料图,文中的示例代码讲解详细,对我们学习Matlab有一定帮助,需要的可以参考一下
效果如下
步骤
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制作抖音同款含褶皱面料图的文章就介绍到这了,更多相关Matlab褶皱面料图内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:利用Matlab制作抖音同款含褶皱面料图
基础教程推荐
- 详解c# Emit技术 2023-03-25
- C语言 structural body结构体详解用法 2022-12-06
- C++详细实现完整图书管理功能 2023-04-04
- C++使用easyX库实现三星环绕效果流程详解 2023-06-26
- C++中的atoi 函数简介 2023-01-05
- C语言基础全局变量与局部变量教程详解 2022-12-31
- C/C++编程中const的使用详解 2023-03-26
- C利用语言实现数据结构之队列 2022-11-22
- 一文带你了解C++中的字符替换方法 2023-07-20
- 如何C++使用模板特化功能 2023-03-05