C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > Matlab褶皱面料图

利用Matlab制作抖音同款含褶皱面料图

作者:slandarer

这篇文章主要介绍了如何利用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褶皱面料图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文