Matlab中值滤波器代码 [英] Matlab Median Filter Code

查看:352
本文介绍了Matlab中值滤波器代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在MATLAB中实现图像的中值滤波。但是,我不允许在MATLAB中使用 medfilt2 ordfilt2 函数。我们最近也开始学习MATLAB。

有没有可用的中值滤波器或高斯滤波器的代码?

中值滤波背后的基本前提是分析图像中的像素邻域,对其强度进行排序,然后选择中等强度作为结果。我可以做的一个建议是使用 im2col 将每个像素邻域转换为单个向量,并将所有这些列向量创建为单个矩阵。每列将表示像素邻域内的像素强度。接下来,使用 sort ,然后沿着每列的行进行排序,然后抓取这个排序矩阵的 middle ,它表示每个像素邻域的中间值。这将是表示每个像素邻域的中值的单行向量。完成之后,只需将该矢量重新整形为与原始图像相同的大小即可获得中值滤波结果。您可以使用 col2im 来帮助实现最后一步。

但是,使用 im2col 时,它只能获取像素邻域在原始图像的范围内。因为您需要沿图像边界的中值过滤器像素,所以在使用 im2col 处理之前,需要填充图像边界。使用 padarray 为你做这个。因此,给定一个灰度图像<$ c $,我们将假设边界被零填充以使事情变得更简单。 c> im 和一个对称邻域来分析 N x N ,其中 N 是你的邻居的宽度/高度,你的代码可能看起来像这样。我也假定 N 是奇数,以允许选择中位数更容易:

  im_pad = padarray(im,[floor(N / 2)floor(N / 2)]); 
im_col = im2col(im_pad,[N N],'sliding');
sorted_cols = sort(im_col,1,'ascend');
med_vector = sorted_cols(floor(N * N / 2)+ 1,:);
out = col2im(med_vector,[N N],size(im_pad),'sliding');






我们来做一个例子。假设我们的过滤器大小为 5 x 5 ,我们将使用 cameraman.tif ,这是图像处理的一部分工具箱。如果我们执行下面的代码,那么运行刚才看到的中值过滤器代码:

$ $ $ $ $ $ $ $ $ N = 5;
im = imread('cameraman.tif');

我们对原始图像和经过中值滤波的最终图像进行了如下处理。 / p>





这是我们期望的,因为在进行图像平滑时,中值滤波可以(或多或少)保持边缘的良好维护。中值滤波对于椒盐噪声是特别有用的,这些噪声像素很可能出现在开始和结束时排序像素邻域,所以选择中间值很可能会过滤掉这些噪声值。 p>




奖金



你的帖子也是要求先找到代码高斯滤波原理。几天前我回答了这个问题。



检查这个帖子:

/ a>


I am required to implement median filtering in MATLAB for images. However, I'm not allowed to use the medfilt2 or ordfilt2 functions in MATLAB. We also have recently started learning MATLAB.

Is there any code available for the median filter or Gaussian filter available?

解决方案

NB: This assumes that the Image Processing Toolbox is installed.


The basic premise behind median filtering is to analyze pixel neighbourhoods in your image, sort their intensities, then choose the middle intensity as the result. One suggestion I can make is to use im2col to transform each pixel neighbourhood into a single column vector and take all of these column vectors to create a single matrix. Each column would represent pixel intensities within a pixel neighbourhood. Next, use sort and sort along the rows for each column, then grab the middle of this sorted matrix which represents the middle value for each pixel neighbourhood. This will be a single row vector that represents the median value of each pixel neighbourhood. Once you're done, simply reshape this vector back into the same size as the original image to get your median filtered result. You can use col2im to help facilitate this last step.

However, with im2col, it only grabs pixel neighbourhoods that are within the bounds of the original image. Because you'll want to median filter pixels along the borders of the image, you'll need to pad the image borders before processing with im2col. Use padarray to do this for you. I'm going to assume that the border gets padded with zeroes to make things simpler.

Therefore, given a grayscale image im, and a symmetric neighbourhood to analyze that's N x N, where N is the width/height of your neighbourhood, your code may look something like this. I'm also going to assume that N is odd to allow picking the median to be easier:

im_pad = padarray(im, [floor(N/2) floor(N/2)]);
im_col = im2col(im_pad, [N N], 'sliding');
sorted_cols = sort(im_col, 1, 'ascend');
med_vector = sorted_cols(floor(N*N/2) + 1, :);
out = col2im(med_vector, [N N], size(im_pad), 'sliding');


Let's do an example. Let's say our filter size was 5 x 5, and we'll use cameraman.tif that's part of the Image Processing Toolbox. If we perform the code below then run the median filter code just seen above:

N = 5;
im = imread('cameraman.tif');

We get the following, with the original image, and the final image that's filtered with median filtering.

This is what we expect as median filtering is expected to (more or less) keep the edges well maintained while doing image smoothing. Median filtering is particularly useful for salt-and-pepper noise where it is highly probable that these noisy pixels will appear the beginning and at the end when sorting pixel neighbourhoods, so choosing the middle value will most likely filter out these noisy values.


Bonus

Your post also is asking to find code from first principles for doing Gaussian filtering. I answered this a couple of days ago for someone else.

Check this post here: How do I create and apply a Gaussian filter in MATLAB without using fspecial, imfilter or conv2?

这篇关于Matlab中值滤波器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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