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

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

问题描述

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

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

解决方案

注意:这里假设安装了 Image Processing Toolbox.

<小时>

中值滤波的基本前提是分析图像中的像素邻域,对它们的强度进行排序,然后选择中间强度作为结果.我可以提出的一个建议是使用

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天全站免登陆