Gaussian filters和2D convolution

mark


2D convolution




function [mag,ax,ay, or] = Canny(im, sigma) % Magic numbers GaussianDieOff = .0001; % Design the filters - a gaussian and its derivative pw = 1:30; % possible widths ssq = sigma^2; width = find(exp(-(pw.*pw)/(2*ssq))>GaussianDieOff,1,'last'); if isempty(width) width = 1; % the user entered a really small sigma end gau=fspecial('gaussian',2*width+1,1); % Find the directional derivative of 2D Gaussian (along X-axis) % Since the result is symmetric along X, we can get the derivative along % Y-axis simply by transposing the result for X direction. [x,y]=meshgrid(-width:width,-width:width); dgau2D=-x.*exp(-(x.*x+y.*y)/(2*ssq))/(pi*ssq); % Convolve the filters with the image in each direction % The canny edge detector first requires convolution with % 2D gaussian, and then with the derivitave of a gaussian. % Since gaussian filter is separable, for smoothing, we can use % two 1D convolutions in order to achieve the effect of convolving % with 2D Gaussian. We convolve along rows and then columns. %smooth the image out aSmooth=imfilter(im,gau,'conv','replicate'); % run the filter across rows aSmooth=imfilter(aSmooth,gau','conv','replicate'); % and then across columns %apply directional derivatives ax = imfilter(aSmooth, dgau2D, 'conv','replicate'); ay = imfilter(aSmooth, dgau2D', 'conv','replicate'); mag = sqrt((ax.*ax) + (ay.*ay)); magmax = max(mag(:)); if magmax>0 mag = mag / magmax; % normalize end or = atan2(-ay, ax); % Angles -pi to + pi. neg = or<0; % Map angles to 0-pi. or = or.*~neg + (or+pi).*neg; or = or*180/pi; % Convert to degrees. end
最新发布
05-22
`filters.gaussian_filter` 是一个 SciPy 库中的函数,它可以对输入的 N 维数组进行高斯滤波。高斯滤波是一种常见的图像滤波方法,它可以通过降低图像中高频部分的强度来减少图像中的噪声,并模糊图像中的细节。高斯滤波器是一个线性滤波器,它使用高斯核函数(或者高斯分布)来对图像进行卷积。 `filters.gaussian_filter` 函数的语法为: ```python scipy.ndimage.filters.gaussian_filter(input, sigma, order=0, output=None, mode='reflect', cval=0.0, truncate=4.0) ``` 其中,参数含义如下: - `input`:要进行滤波的 N 维数组。 - `sigma`:高斯核函数的标准差。较大的值将导致更强的模糊效果。 - `order`:用于计算高斯核函数的导数的阶数。默认为0,表示不计算导数。 - `output`:可选参数,指定输出的数组。 - `mode`:指定输入数组的边界模式。默认为 'reflect',表示输入数组在边界处被反射。 - `cval`:当 `mode` 为 'constant' 时,用于指定输入数组边界外的常数值。 - `truncate`:高斯核函数的截断参数,用于控制高斯函数的半径。默认为 4.0。 下面是一个使用 `filters.gaussian_filter` 进行高斯滤波的示例: ```python import scipy.ndimage as ndimage import numpy as np import matplotlib.pyplot as plt # 生成一张随机噪声图像 np.random.seed(0) img = np.random.rand(512, 512) # 对图像进行高斯滤波 filtered_img = ndimage.gaussian_filter(img, sigma=10) # 显示原始图像和滤波后的图像 fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8, 4)) axes[0].imshow(img, cmap='gray') axes[0].set_title('Original Image') axes[1].imshow(filtered_img, cmap='gray') axes[1].set_title('Filtered Image') plt.show() ``` 运行结果如下图所示,可以看到经过高斯滤波后的图像变得更加模糊,噪声也得到了一定的抑制。 ![gaussian_filter_demo](https://img-blog.csdnimg.cn/20211013192950643.png)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值