您当前的位置: 首页 >  matlab

slandarer

暂无认证

  • 0浏览

    0关注

    248博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

教你使用MATLAB制作水波倒影特效

slandarer 发布时间:2021-08-02 16:10:04 ,浏览量:0

注: 本文算法参考大佬 grafx 的这篇博客: 图像处理算法之水面倒影特效

由于本文使用MATLAB复现,因此很多语法上会显得比较简洁,同时本博文对原大佬文章部分内容进行了改写,详见本文:

0效果展示

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

1图像翻转及白化

导入图像: 这部分其实没啥好说的:

% 图片导入 
oriPic=imread('test.jpg');
[Row,Col,~]=size(oriPic);

在这里插入图片描述

翻转及白化图像:

翻转就是单纯的将行索引倒过来; 白化就是将当前像素的颜色按比例和白色取个带权均值,行索引越大白色权重也越大,图像也就越白。

% 图片翻转及白化 ==========================================================
whiteMat=((1:Row)./Row./1.2)'*ones(1,Col); % 白化比例矩阵
flipPic=zeros(Row,Col,3);                  % 翻转后矩阵初始化
for i=1:3
    tempChannel=double(oriPic(:,:,i));     % 获得通道图
    tempChannel=tempChannel(end:-1:1,:);   % 翻转
    tempChannel=tempChannel.*(1-whiteMat)+255.*whiteMat; % 白化
    flipPic(:,:,i)=tempChannel;
    
end

在这里插入图片描述 当然如果我们将这一行:

tempChannel=tempChannel.*(1-whiteMat)+255.*whiteMat;

改写为:

tempChannel=tempChannel.*(1-whiteMat)+0.*whiteMat;

就变成了一个黑化的过程:

在这里插入图片描述 当然你也可以尝试其他颜色,例如将整段改写为:

Color=[255,0,0];
colorMat=((1:Row)./Row./1.2)'*ones(1,Col); % 比例矩阵
flipPic=zeros(Row,Col,3);                  % 翻转后矩阵初始化
for i=1:3
    tempChannel=double(oriPic(:,:,i));     % 获得通道图
    tempChannel=tempChannel(end:-1:1,:);   % 翻转
    tempChannel=tempChannel.*(1-colorMat)+Color(i).*colorMat; % 渐变
    flipPic(:,:,i)=tempChannel;
    
end
imshow(uint8(flipPic))

在这里插入图片描述

2波纹图像构造

生成噪声并模糊

noiseMat=ones(Row,Col);
noiseMat=imnoise(noiseMat,'gaussian',0,5); % 噪声添加
gaussOpt=fspecial('gaussian',[3 3],1);
noiseMat=imfilter(noiseMat,gaussOpt);

噪声图:

在这里插入图片描述

模糊后噪声图:

在这里插入图片描述 浮雕特效

实际上浮雕特效就是用以下类似形式的矩阵对图像进行卷积,卷积结果在加上RGB范围的均值,[0,1]区间就加0.5,[0,255]区间就加128: [ 1 0 0 0 0 0 0 0 − 1 ] [ 2 0 2 0 0 0 − 2 0 − 2 ] \begin{bmatrix} 1 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & -1 \end{bmatrix} \begin{bmatrix} 2 & 0 & 2 \\ 0 & 0 & 0 \\ -2 & 0 & -2 \end{bmatrix} ⎣⎡​100​000​00−1​⎦⎤​⎣⎡​20−2​000​20−2​⎦⎤​ 数值和位置不重要,重要的是相对位置互为相反数,浮雕过程描述如下:

H=[cos(pi+pi/4),0,cos(pi-pi/4);
   cos(pi+2*pi/4),0,cos(pi-2*pi/4);
   cos(pi+3*pi/4),0,cos(pi-3*pi/4)];
noiseMat=imfilter(noiseMat,H,'conv')+0.5;  
noiseMat=noiseMat.*255;
noiseMat(noiseMat            
关注
打赏
1664692598
查看更多评论
0.0398s