HOW TO APPLY A MEAN FILTER FOR 3x3
Afficher commentaires plus anciens
I NEED TO APPLY THE 3x3 MEAN FILTER TO THE GRAY SCALE IMAGE. PLS SEND ME THE CODE FOR THAT
1 commentaire
Xheni
le 17 Jan 2023
thank you
Réponse acceptée
Plus de réponses (5)
Image Analyst
le 27 Sep 2012
filteredImage = conv2(single(grayscaleImage), ones(3)/9);
7 commentaires
Sehairi K.
le 10 Août 2016
filteredImage = conv2(grayscaleImage, ones(3)/9,'same');
Image Analyst
le 10 Août 2016
This will work if your grayscaleImage is already converted to double or single. Also, there are 3 edge options: 'same', 'full' (the default if none is supplied), and 'valid'. Usually for image processing the 'same' option is used.
Atalay Asa
le 7 Juin 2020
How can we use it for 7x7 arithmetic mean filter?
Image Analyst
le 7 Juin 2020
windowWidth = 7;
kernel = ones(windowWidth) / windowWidth^2;
outputImage = imfilter(grayImage, kernel);
SP
le 7 Juin 2021
Hello, Sir. Can you explain to me that why the value in neighborhood need to be 1/9 (or 0.111)?
Ridho Liwardana
le 23 Mar 2022
It's depend of matrix that you wanna use as filter, if it's a 3x3 matrix, the value is 1/(3x3) -> 1/9.
Image Analyst
le 23 Mar 2022
@SP The reason you divide by windowWidth^2 (1/9 in this case) is because if you don't, the filtering is the sum of the filter window times the image to be filtered. So if the image were all 255 under the filter, you'd get an output value of 9*255 instead of 255. So the image would be 9 times as bright. To ensure that the filtered image lies in the same intensity range as the original image, you need to divide the filter values by windowWidth^2.
If you didn't have a uniform filter (like here where they are all 1's) you'd have to divide by the sum of the kernel:
% Divide unnormalized kernel by the sum of the values in it.
% This will ensure the output image has the same intensity range as the original.
kernel = kernel / sum(kernel(:));
outputImage = imfilter(grayImage, kernel);
Actually, that is the most general approach which will work for any filter.
Mohamed Batran
le 11 Juil 2015
0 votes
thank you for your reply simple but helped a lot for customizing the filter i want
Celal Can Elgün
le 24 Oct 2015
0 votes
How can we make it without using filter2(or any command) ?
Geetha raja
le 19 Août 2018
0 votes
I NEED TO APPLY THE bilateral FILTER TO THE GRAY SCALE IMAGE for denoising. PLS SEND ME THE CODE FOR THAT
1 commentaire
Image Analyst
le 19 Août 2018
Geetha, there is a part of the web site called "File Exchange". You can look there for community-contributed programs: Search for bilateral filter
Jahid Hasan
le 19 Avr 2022
0 votes
Sorry to ask here, how to write mean and median filter without using a in-built function? What are the way to do it? Any help or code syntax. Thank you
4 commentaires
Image Analyst
le 19 Avr 2022
You'd use a for loop in the obvious way where you just sum the elements and divide by the number of elements.
For median you'd need to write your own sorting routine (see Wikipedia or somewhere) and then take the middle element.
Jahid Hasan
le 19 Avr 2022
Thanks for your reply. Do you have any examples of it as a reference to understand it.
Walter Roberson
le 19 Avr 2022
Potential reference implementation. Not tested.
sum_of_elements = 0;
index_of_element = 0;
while true
index_of_element = index_of_element + 1;
try
sum_of_elements = sum_of_elements + YourVector(index_of_element);
catch ME
break; %we ran off the end of the array
end
end
number_of_elements = index_of_element - 1; %loop over-counts by 1
mean_of_YourVector = sum_of_elements ./ number_of_elements;
sorted_vector = bogosort(YourVector, number_of_elements);
index_of_element = 0;
while true
index_of_element = index_of_element + 1;
if index_of_element + index_of_element == number_of_elements
median_of_YourVector = YourVector(index_of_element);
break
elseif index_of_element + index_of_element + 1 == number_of_elements
median_of_YourVector = (YourVector(index_of_element) + YourVector(index_of_element+1)) / 2;
break
end
end
function sortedVector = bogosort(vectorToSort, number_of_elements)
%this is a legitimate sort
while true
neworder = randperm(number_of_elements);
sortedVector = vectorToSort(neworder);
index_of_element = 0;
while true
index_of_element = index_of_element + 1;
if index_of_element >= number_of_elements
%we got to the end of the list without detecting out-of-order
return;
end
if YourVector(index_of_element) > YourVector(index_of_element+1)
%detected something out of order, try again with a new order
break
end
end
end
end
DGM
le 19 Avr 2022
As far as sliding filter/convolution examples go, here are some links. I'm sure there are others. These are just the ones I have in my notes.
general sliding/moving window filter base
same thing only with a gaussian mean setup
basic mean filter example
basic 2D convolution build (full size)
basic 2D convolution build (same size)
Catégories
En savoir plus sur Image Filtering dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

