I NEED TO APPLY THE 3x3 MEAN FILTER TO THE GRAY SCALE IMAGE. PLS SEND ME THE CODE FOR THAT

 채택된 답변

Wayne King
Wayne King 2012년 9월 27일

1 개 추천

How about just:
h = 1/3*ones(3,1);
H = h*h';
% im be your image
imfilt = filter2(H,im);

댓글 수: 6

ajith
ajith 2012년 9월 27일
편집: ajith 2012년 9월 27일
GREAT SIR THANKS A LOT
suadad najim
suadad najim 2023년 2월 9일
Can you tell me about the meaning of (ones) in your code please?
h = 1/3*ones(3,1);
H = h*h';
H
H = 3×3
0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111
Notice this is the same as
ones(3,3)/9
ans = 3×3
0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111
which in turn is the same as
[1/9, 1/9, 1/9;
1/9, 1/9, 1/9;
1/9, 1/9, 1/9]
ans = 3×3
0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111
The ones() that @Wayne King used is a shorter way of writing out the individual components of the matrix.
See Image Analyst's discussion below https://www.mathworks.com/matlabcentral/answers/49226-how-to-apply-a-mean-filter-for-3x3#comment_2060839 about why you need to divide by 9 in this context.
suadad najim
suadad najim 2023년 2월 10일
@Walter Roberson Thank you for quick answer
I want you to see the image before and after applying the mean filter you gave. Is that normal?
Orginal image Resulted image
Walter Roberson
Walter Roberson 2023년 2월 10일
Check class() of the input image and class() of the output image. I suspect that class of the output is double and that you would get what you want if you uint8() that.
What is the class and scale of the input and output? The rendering of an image depends on an assumption of where white and black are. Floating-point images are expected to be unit-scale (i.e. 0-1). If you're using the above code, then it will be uint8-scale (0-255)
% this is a uint8 image
im = imread('cameraman.tif');
% create filter
h = 1/3*ones(3,1);
H = h*h';
% the output of this will be class 'double', but scaled as if uint8
imfilt = filter2(H,im);
% cast it to a class appropriate for its scale
imfilt = uint8(imfilt);
% display it
imshow(imfilt)
See also:

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Image Analyst
Image Analyst 2012년 9월 27일

3 개 추천

filteredImage = conv2(single(grayscaleImage), ones(3)/9);

댓글 수: 7

filteredImage = conv2(grayscaleImage, ones(3)/9,'same');
Image Analyst
Image Analyst 2016년 8월 10일
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
Atalay Asa 2020년 6월 7일
How can we use it for 7x7 arithmetic mean filter?
windowWidth = 7;
kernel = ones(windowWidth) / windowWidth^2;
outputImage = imfilter(grayImage, kernel);
SP
SP 2021년 6월 7일
Hello, Sir. Can you explain to me that why the value in neighborhood need to be 1/9 (or 0.111)?
Ridho Liwardana
Ridho Liwardana 2022년 3월 23일
It's depend of matrix that you wanna use as filter, if it's a 3x3 matrix, the value is 1/(3x3) -> 1/9.
@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
Mohamed Batran 2015년 7월 11일

0 개 추천

thank you for your reply simple but helped a lot for customizing the filter i want
Geetha raja
Geetha raja 2018년 8월 19일

0 개 추천

I NEED TO APPLY THE bilateral FILTER TO THE GRAY SCALE IMAGE for denoising. PLS SEND ME THE CODE FOR THAT

댓글 수: 1

Image Analyst
Image Analyst 2018년 8월 19일
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
Jahid Hasan 2022년 4월 19일

0 개 추천

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

Image Analyst
Image Analyst 2022년 4월 19일
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
Jahid Hasan 2022년 4월 19일
Thanks for your reply. Do you have any examples of it as a reference to understand it.
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

댓글을 달려면 로그인하십시오.

질문:

2012년 9월 27일

댓글:

DGM
2023년 2월 10일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by