且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Matlab 中值滤波器代码

更新时间:2022-10-15 15:42:10

注意:这里假设安装了 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?