赞
踩
- % matlab 2012b
- % 彩色图的双边滤波
- clc;
- clear;
- src=imread('cs.jpg'); %加载彩色图
- %-------双边滤波 参数 -----------------
- r = 20; %半径 窗口 长 = 宽 = 2*r+1
- sigma_space = 15.0; %空间标准差
- sigma_color = 10.0; %相似标准差
- w_space = zeros(1,r+1); %空间权重
- for i = 1:r+1 %i为横坐标之差的绝对值 + 1
- for j = 1:r+1 %j为纵坐标之差的绝对值 + 1
- w_space(i,j) = exp(-double((i-1)^2 + (j-1)^2) / (2 * sigma_space^2));
- end
- end
- w_color = zeros(1,256); %相似权重
- for i = 1:256 %i为像素值之差的绝对值 + 1
- w_color(i) = exp(-double((i-1)^2) / (2 * sigma_color^2));
- end
- %disp(w_space);
- %disp(w_color);
- %break;
- %--------- 进行滤波 ------------------
- src=double(src);
- [height,width,channel] = size(src);
- if channel ~= 3 %判断是否为3通道彩色图
- break; %不是则退出函数
- end
- dst = src;
- for h = 1+r:height-r
- for w = 1+r:width-r %窗口中心
- for c = 1:channel %彩色图 三通道
- w_sum = 0; %权值和
- p_sum = 0; %权值未归一化与像素值的乘积和
- p_c = src(h,w,c); %中心像素点的值
- for j = h-r:h+r
- for i = w-r:w+r %遍历窗口内的像素
- p_t = src(j,i,c); %像素点的值 以及未归一化的权值
- w_tmp = w_space(abs(i-w)+1,abs(j-h)+1) * w_color(abs(p_c-p_t)+1);
- p_sum = p_sum + p_t * w_tmp; %权值未归一化与像素值的乘积和
- w_sum = w_sum + w_tmp; %权值和
- end
- end
- p_sum = p_sum / w_sum; %归一化
- dst(h,w,c) = p_sum; %赋值
- end
- end
- end
-
- %转换格式 显示
- src = uint8(src);
- dst = uint8(dst);
- subplot(121),imshow(src);
- subplot(122),imshow(dst);
参考:
https://blog.csdn.net/l_eop/article/details/81812277
https://blog.csdn.net/qq_36359022/article/details/80198890
opencv源码 bilateral_filter.cpp
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。