问题
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?
回答1:
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?
回答2:
I have written a code, it might help:
reading the image and adding noise to it:
I=imread('cameraman.tif');
if(size(I,3)~=1)
I=rgb2gray(I);
end
rr=0.1;
h=imnoise(I,'salt & pepper',rr);
imshow(h);
[M,N]=size(h);
new=h-h;
asking the size of the kernel you need:
disp('***************Note:zero-padding method is used!***********');
disp(' ');
kernel_size=input('enter the size of the kernel for the Median-ranking? 3 or 5 or 7 or 9= ');
k=zeros(kernel_size); %k is the kernel used.
applying the median filter to the image:
start=kernel_size-floor(kernel_size*0.5);
for x=start:1:M-floor(kernel_size*0.5)
for y=start:1:N-floor(kernel_size*0.5)
%defining x1 & y1 as the 1st coordinates in the kernel
x1=x-(floor(kernel_size*0.5));
y1=y-(floor(kernel_size*0.5));
%specifying image pixels to the kernel
for p=1:1:kernel_size
for q=1:1:kernel_size
k(p,q)=h(x1+p-1,y1+q-1);
end
end
d=reshape(k,1,[]); %k values into an array d
[r,c]=size(d);
%*****Ordering kernel members***************
for j=1:1:c-1
for i=1:1:c-1
a=d(1,i);
b=d(1,i+1);
if(a>b)
d(1,i)=b;
d(1,i+1)=a;
end
end
end
Median=d(1,floor(kernel_size*kernel_size*0.5)+1);
%*****************end of ordering***********
%*******************************************
new(x,y)=Median;
end
end
displaying the result:
figure;imshow(new,[]);
回答3:
I am telling you how to start your task. All you need is to built up a sorting algorithm and then pick up the center value. Big portion is implementing the sorting algorithm. Some of them:
- Bucket sort
- List item
- Bubble sort
- Insertion sort
- Selection sort
- Heapsort
- Mergesort
They are not hard to understand and write your own. You can find easily already written. Type to your search engine sorting algorithms matlab
. You said I'm looking for the code of the Built-in function of median filter in Matlab.
Matlab allows to seen source code of meddilt2()
and ordfilt2()
, you can see the code of these functions, but remember copyright rules.
After implementing the sorting algorithm, you can easily pick the center value of your filter mask by writing mask( ( numel(mask) + 1 ) / 2 )
.
That' s it.
If you were looking for someone to write the whole solution for your task from scratch, you would have tough work.
Good luck.
There are two MATLAB built in function for median filtering: medfilt2()
and ordfilt2()
. Actually medfilt2()
calls ordfilt2()
in its body. Because median filtering is special case of rank order filtering.
回答4:
Here is the code I've written myself. I hope it works.
clc;
clear all;
close all;
A=imread('2.jpg');
A=rgb2gray(A);
figure;imshow(A);
Y=imnoise(A,'salt & pepper',0.5);
figure;imshow(Y);
[n,m]=size(A);
%median filter
for i=1:n
for j=1:m
mat=zeros(3,3);
if((i-1) == 0 && (j-1) ~= 0 && j~=m)
mat(2:3,1:3)=Y(i:i+1,j-1:j+1);
mat=sort(mat,1);
mat=sort(mat,2);
Y(i,j)=mat(2,2);
elseif(i==n && (j-1)~=0 && j~=m)
mat(1:2,:)=Y(i-1:i,j-1:j+1);
mat=sort(mat,1);
mat=sort(mat,2);
Y(i,j)=mat(2,2);
elseif((i-1)~=0 &&(j-1)~=0 && j~=m && i~=n)
mat(:,:)=Y(i-1:i+1,j-1:j+1);
mat=sort(mat,1);
mat=sort(mat,2);
Y(i,j)=mat(2,2);
elseif((j-1)==0 && (i-1)~=0 && i~=n)
mat(:,2:3)=Y(i-1:i+1,j:j+1);
mat=sort(mat,1);
mat=sort(mat,2);
Y(i,j)=mat(2,2);
elseif(j==m && (i-1)~=0 && i~=n)
mat(:,1:2)=Y(i-1:i+1,j-1:j);
mat=sort(mat,1);
mat=sort(mat,2);
Y(i,j)=mat(2,2);
end
end
end
figure;
imshow(Y)
来源:https://stackoverflow.com/questions/27535535/matlab-median-filter-code