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

查看:628
本文介绍了我应该通过哪些措施在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 。但是,因为我们处理的是离散域,所以我们也需要适应高斯的中心。因此,您需要确保总宽度为: floor(6 * sigma)+ 1 。因此,您需要做的是找出您想要的宽度。完成后,您可以确定需要 sigma 来满足此宽度。举个例子,假设我们最小的特征的宽度是 19 。然后你会弄明白你的 sigma 是什么:

  19 = 6 * sigma + 1 
18 = 6 * sigma
sigma = 3

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

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

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

 %//读入图像 -  MATLAB路径的一部分
im = imread( 'cameraman.tif');

%//确定宽度和标准差
width1 = 3; sigma1 =(width1-1)/ 6;
width2 = 7; sigma2 =(width2-1)/ 6;
width3 = 13; sigma3 =(width3-1)/ 6;
width4 = 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');

%//在
数字上显示所有数字;
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']);

您将获得以下输出:




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: floor(6*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 = 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天全站免登陆