我应该通过哪些措施在 MATLAB 中设置高斯滤波器的大小? [英] By which measures should I set the size of my Gaussian filter in MATLAB?

查看:72
本文介绍了我应该通过哪些措施在 MATLAB 中设置高斯滤波器的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MATLAB 学习图像处理,并且我已经阅读了有关图像过滤器的信息.通过考虑此代码:

gaussianFilter = fspecial('gaussian', [7, 7], 5) ,

这将构建一个 7 行 7 列的高斯滤波器矩阵,标准差为 5.因此,滤波器矩阵的大小为 7 x 7 .

这个矩阵的大小如何对过滤有效?(这个矩阵有什么作用?)

我应该通过哪些措施在代码中设置过滤器矩阵的大小?

解决方案

用于确定高斯滤波器的大小和最终标准偏差的最常见和启发式方法之一是所谓的 3-sigma规则.如果您回忆一下概率,高斯分布的大部分值都集中在 [mu - 3*sigma, mu + 3*sigma] 之间,其中 mu 是分布和 sigma 是分布的标准偏差.这实际上称为

通过查看[mu - 3*sigma, mu + 3*sigma],大部分变化可以包含在高斯分布下总面积的 99% 内.作为旁注,在 [mu - 2*sigma, mu + 2*sigma] 之间,这覆盖了大约总面积的 95%,最后是 [mu - sigma, mu + sigma],这大约占总面积的 68%.

因此,人们通常做的是查看图像并找出最小特征是什么.他们测量特征的宽度或高度,并确保特征的宽度/高度/跨度符合 99% 的置信区间.测量横向给我们的总宽度为 6*sigma.然而,因为我们处理的是离散域,我们还需要适应高斯的中心.因此,您希望确保总宽度为:2 * floor(3*sigma) + 1.因此,您需要做的是弄清楚您想要的宽度.一旦你这样做了,你就可以弄清楚需要什么 sigma 才能满足这个宽度.例如,假设我们最小特征的宽度是 19.然后你会弄清楚你的 sigma 是什么:

19 = 2*floor(3*sigma) + 119 = 6*西格玛 + 118 = 6*西格玛西格玛 = 3

因此,您可以像这样创建高斯核:

h = fspecial('gaussian', [19 19], 3);

如果您想玩弄掩码大小,只需使用上述等式每次操作和求解 sigma.现在回答您关于尺寸的问题,这是一个低通滤波器.因此,增加矩阵的大小实际上会增加 LPF 的效果.随着尺寸的增加,您的图像将变得越来越模糊.玩弄大小,看看你得到了什么.如果您在尝试此操作时没有考虑任何特定图像,则可以改用 MATLAB 中的任何内置图像.因此,请尝试执行以下操作:

%//读入图像 - MATLAB 路径的一部分im = imread('cameraman.tif');%//确定宽度和标准偏差宽度 1 = 3;sigma1 = (width1-1)/6;宽度 2 = 7;sigma2 = (width2-1)/6;宽度 3 = 13;sigma3 = (width3-1)/6;宽度4 = 19;sigma4 = (width4-1)/6;%//创建高斯核h1 = fspecial('gaussian', [width1 width1], sigma1);h2 = fspecial('gaussian', [width2 width2], sigma2);h3 = fspecial('gaussian', [width3 width3], sigma3);h4 = fspecial('gaussian', [width4 width4], sigma4);%//使用每个内核过滤图像out1 = imfilter(im, h1, 'replicate');out2 = imfilter(im, h2, 'replicate');out3 = imfilter(im, h3, 'replicate');out4 = imfilter(im, h4, 'replicate');%//将它们全部显示在图形上数字;子图(2,2,1);imshow(out1);标题(['宽度 = 3']);子图(2,2,2);imshow(out2);标题(['宽度 = 7']);子图(2,2,3);imshow(out3);标题(['宽度 = 13']);子图(2,2,4);imshow(out4);标题(['宽度 = 19']);

您将获得以下输出:

I'm trying to learn image processing using MATLAB and I have read about filters on images. By considering this code:

gaussianFilter = fspecial('gaussian', [7, 7], 5)  ,

this builds a Gaussian filter matrix of 7 rows and 7 columns, with standard deviation of 5. As such, the size of filter matrix is 7 x 7 .

How can the size of this matrix be effective on filtering? (What does this matrix do ?)

By which measures should I set the size of filter matrix in my code?

解决方案

One of the most common and heuristic measures on determining the size and ultimately the standard deviation of the Gaussian filter is what is known as the 3-sigma rule. If you recall from probability, the Gaussian distribution has most of its values centered between [mu - 3*sigma, mu + 3*sigma] where mu is the mean of the distribution and sigma is the standard deviation of the distribution. This is actually known as a 99% confidence interval. A good diagram of this is shown below:

Source: Wikipedia

By taking a look at [mu - 3*sigma, mu + 3*sigma], most of the variation can be contained within 99% of the total area underneath the Gaussian distribution. As a sidenote, between [mu - 2*sigma, mu + 2*sigma], this covers roughly 95% of the total area and finally for [mu - sigma, mu + sigma], this covers roughly 68% of the total area.

As such, what people usually do is take a look at an image and figure out what the smallest feature is. They measure the width or height of the feature and ensure that the width / height / span of the feature fits within the 99% confidence interval. Measuring across gives us a total width of 6*sigma. However, because we are dealing in the discrete domain, we need to also accommodate for the centre of the Gaussian as well. As such, you want to ensure that the total width is thus: 2 * floor(3*sigma) + 1. Therefore, what you need to do is figure out the width you want. Once you do that, you can figure out what sigma is required to satisfy this width. As an example, let's say the width of our smallest feature was 19. You would then figure out what your sigma was by:

19 = 2*floor(3*sigma) + 1
19 = 6*sigma + 1
18 = 6*sigma
sigma = 3

Therefore, you would create your Gaussian kernel like so:

h = fspecial('gaussian', [19 19], 3);

If you want to play around with the mask size, simply use the above equation to manipulate and solve for sigma each time. Now to answer your question about size, this is a low-pass filter. As such, increasing the size of the matrix will actually increase the effects of the LPF. Your image will become more progressively blurred as you increase its size. Play around with the size and see what you get. If you don't have any particular image in mind when trying this out, you can use any built-in image in MATLAB instead. As such, try doing the following:

%// Read in the image - Part of MATLAB path
im = imread('cameraman.tif');

%// Determine widths and standard deviations
width1 = 3; sigma1 = (width1-1) / 6;
width2 = 7; sigma2 = (width2-1) / 6;
width3 = 13; sigma3 = (width3-1) / 6;
width4 = 19; sigma4 = (width4-1) / 6;

%// Create Gaussian kernels
h1 = fspecial('gaussian', [width1 width1], sigma1);
h2 = fspecial('gaussian', [width2 width2], sigma2);
h3 = fspecial('gaussian', [width3 width3], sigma3);
h4 = fspecial('gaussian', [width4 width4], sigma4);

%// Filter the image using each kernel
out1 = imfilter(im, h1, 'replicate');
out2 = imfilter(im, h2, 'replicate');
out3 = imfilter(im, h3, 'replicate');
out4 = imfilter(im, h4, 'replicate');

%// Display them all on a figure
figure;
subplot(2,2,1);
imshow(out1);
title(['Width = 3']);
subplot(2,2,2);
imshow(out2);
title(['Width = 7']);
subplot(2,2,3);
imshow(out3);
title(['Width = 13']);
subplot(2,2,4);
imshow(out4);
title(['Width = 19']);

You'll get the following output:

这篇关于我应该通过哪些措施在 MATLAB 中设置高斯滤波器的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆