本文主要是介绍MATLAB 制作抖音同款炫光海报,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这篇其实步骤比较多,看效果:
步骤
0.图片导入
oriPic=imread('test.jpg');
原始图片:
抖音上是做了铜板雕刻的像素化处理,由于MATLAB本身不具备这个功能,因此我们使用添加椒盐噪声和动态模糊的方法来模拟铜板雕刻的效果
1.添加椒盐噪声
正常彩色图片添加椒盐噪声,其噪声点也是彩色的,这里我们想要为彩图添加黑白的椒盐噪声,就可以先构造一张和原图等大的灰度图,增添椒盐噪声后,把噪声点移植到原图上:
disp('正在进行噪声添加')
grayPic=uint8(150.*ones([size(oriPic,1),size(oriPic,2)]));
J = imnoise(grayPic,'salt & pepper',0.1);
Rchannel=oriPic(:,:,1);Rchannel(J==255)=255;Rchannel(J==0)=0;
Gchannel=oriPic(:,:,2);Gchannel(J==255)=255;Gchannel(J==0)=0;
Bchannel=oriPic(:,:,3);Bchannel(J==255)=255;Bchannel(J==0)=0;
spPic(:,:,1)=Rchannel;
spPic(:,:,2)=Gchannel;
spPic(:,:,3)=Bchannel;
imshow(spPic)
效果:
2.图像添加动态模糊
disp('正在动态模糊')
H = fspecial('motion', 40, 0);
movePic=imfilter(spPic, H, 'replicate');
imshow(movePic)
3.径向模糊
disp('正在进行径向模糊')
radialPic=radialConv(movePic,60);
imshow(radialPic)function radialPic=radialConv(oriPic,k)[h,w,c]=size(oriPic);radialPic=zeros(h,w,c);for y=1:hfor x=1:wr=sqrt((y-h/2)^2+(x-w/2)^2);ang=atan2(y-h/2,x-w/2);pix=[0 0 0];pixNum=0;for i=0:1:k%r-i是距离当前像素的距离为r-i的像素,在当前像素和中心像素同一条直线上yy=round(abs(r-i)*sin(ang)+h/2);xx=round(abs(r-i)*cos(ang)+w/2);if yy>=1 && yy<=h && xx>=1 && xx<=wpix(1)=pix(1)+double(oriPic(yy,xx,1));pix(2)=pix(2)+double(oriPic(yy,xx,2));pix(3)=pix(3)+double(oriPic(yy,xx,3));pixNum=pixNum+1;endendpix(1)=pix(1)/pixNum;pix(2)=pix(2)/pixNum;pix(3)=pix(3)/pixNum;radialPic(y,x,:)=pix;endendradialPic=uint8(radialPic);end
效果:
4.顺时针及逆时针旋转图片
disp('正在进行顺时针方向旋转')
L_rotatePic=rotateImage(radialPic,-10);
disp('正在进行逆时针方向旋转')
R_rotatePic=rotateImage(radialPic,10);function rotatePic=rotateImage(oriPic,degree)[w,h,c]=size(oriPic);rotatePic=zeros(w,h,c);swirl_degree=degree./1000;for y=1:hfor x=1:wYoffset=y-h/2;Xoffset=x-w/2;radian=atan2(Yoffset,Xoffset);radius=sqrt(Xoffset^2+Yoffset^2);xx=int32(radius*cos(radian+radius*swirl_degree)+w/2);yy=int32(radius*sin(radian+radius*swirl_degree)+h/2);xx(xx>w)=w;yy(yy>h)=h;xx(xx<1)=1;yy(yy<1)=1;rotatePic(x,y,:)=oriPic(xx,yy,:);endendrotatePic=uint8(rotatePic);end
效果:
5.图片叠加
可以选择变亮或者滤色两种混合模式:
变亮
disp('正在进行混合')
RL=L_rotatePic(:,:,1);RR=R_rotatePic(:,:,1);RL(RL<RR)=RR(RL<RR);
GL=L_rotatePic(:,:,2);GR=R_rotatePic(:,:,2);GL(GL<GR)=GR(GL<GR);
BL=L_rotatePic(:,:,3);BR=R_rotatePic(:,:,3);BL(BL<BR)=BR(BL<BR);
dazzleLightPic(:,:,1)=RL;
dazzleLightPic(:,:,2)=GL;
dazzleLightPic(:,:,3)=BL;
imshow(dazzleLightPic)
滤色
disp('正在进行混合')
dazzleLightPic=uint8(255-double(255-L_rotatePic).*double(255-R_rotatePic)./220);
imshow(dazzleLightPic)
变亮效果:
滤色效果:
6.完整代码
function dazzleLight
oriPic=imread('test9.jpeg');disp('正在进行噪声添加')
grayPic=uint8(150.*ones([size(oriPic,1),size(oriPic,2)]));
J = imnoise(grayPic,'salt & pepper',0.1);
Rchannel=oriPic(:,:,1);Rchannel(J==255)=255;Rchannel(J==0)=0;
Gchannel=oriPic(:,:,2);Gchannel(J==255)=255;Gchannel(J==0)=0;
Bchannel=oriPic(:,:,3);Bchannel(J==255)=255;Bchannel(J==0)=0;
spPic(:,:,1)=Rchannel;
spPic(:,:,2)=Gchannel;
spPic(:,:,3)=Bchannel;
imshow(spPic)disp('正在动态模糊')
H = fspecial('motion', 40, 0);
movePic=imfilter(spPic, H, 'replicate');
imshow(movePic)disp('正在进行径向模糊')
radialPic=radialConv(movePic,60);
imshow(radialPic)disp('正在进行顺时针方向旋转')
L_rotatePic=rotateImage(radialPic,-10);imshow(L_rotatePic)
disp('正在进行逆时针方向旋转')
R_rotatePic=rotateImage(radialPic,10);imshow(R_rotatePic)disp('正在进行变亮混合')
RL=L_rotatePic(:,:,1);RR=R_rotatePic(:,:,1);RL(RL<RR)=RR(RL<RR);
GL=L_rotatePic(:,:,2);GR=R_rotatePic(:,:,2);GL(GL<GR)=GR(GL<GR);
BL=L_rotatePic(:,:,3);BR=R_rotatePic(:,:,3);BL(BL<BR)=BR(BL<BR);
dazzleLightPic(:,:,1)=RL;
dazzleLightPic(:,:,2)=GL;
dazzleLightPic(:,:,3)=BL;
% dazzleLightPic=uint8(255-double(255-L_rotatePic).*double(255-R_rotatePic)./220);
imshow(dazzleLightPic)function radialPic=radialConv(oriPic,k)[h,w,c]=size(oriPic);radialPic=zeros(h,w,c);for y=1:hfor x=1:wr=sqrt((y-h/2)^2+(x-w/2)^2);ang=atan2(y-h/2,x-w/2);pix=[0 0 0];pixNum=0;for i=0:1:k%r-i是距离当前像素的距离为r-i的像素,在当前像素和中心像素同一条直线上yy=round(abs(r-i)*sin(ang)+h/2);xx=round(abs(r-i)*cos(ang)+w/2);if yy>=1 && yy<=h && xx>=1 && xx<=wpix(1)=pix(1)+double(oriPic(yy,xx,1));pix(2)=pix(2)+double(oriPic(yy,xx,2));pix(3)=pix(3)+double(oriPic(yy,xx,3));pixNum=pixNum+1;endendpix(1)=pix(1)/pixNum;pix(2)=pix(2)/pixNum;pix(3)=pix(3)/pixNum;radialPic(y,x,:)=pix;endendradialPic=uint8(radialPic);endfunction rotatePic=rotateImage(oriPic,degree)[w,h,c]=size(oriPic);rotatePic=zeros(w,h,c);swirl_degree=degree./1000;for y=1:hfor x=1:wYoffset=y-h/2;Xoffset=x-w/2;radian=atan2(Yoffset,Xoffset);radius=sqrt(Xoffset^2+Yoffset^2);xx=int32(radius*cos(radian+radius*swirl_degree)+w/2);yy=int32(radius*sin(radian+radius*swirl_degree)+h/2);xx(xx>w)=w;yy(yy>h)=h;xx(xx<1)=1;yy(yy<1)=1;rotatePic(x,y,:)=oriPic(xx,yy,:);endendrotatePic=uint8(rotatePic);endend
另
可以通过将旋转方向和旋转角度做出调整获得不同的效果,例如同向但旋转幅度不同时:
这篇关于MATLAB 制作抖音同款炫光海报的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!