# Mean filter with kernel size

조회 수: 43(최근 30일)
Rohit Kharat 2022년 1월 19일
댓글: Image Analyst 2022년 1월 20일
I want to create a function which does mean filtering with taking image and kernel size as the input. Please help in this regard.

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

### 답변(2개)

DGM 2022년 1월 19일
This is a routine uni project. There are a lot of existing examples on the forum.
Assuming the filter kernel is rectangular and uniform:
1. pad the image (padarray()) to accomodate half the width of the filter kernel to overhang
2. preallocate the output array to the size of the original image
3. indexing by row and column in two nested loops, extract a rectangular region about the current index
4. sum the pixel values in that sample
5. place the sum in the output array at the current index
6. repeat over entire image
7. divide the result by the product of the filter kernel geometry to obtain the mean
Assuming that the filter kernel is nonuniform:
1. create the filter kernel and normalize its sum (or just use fspecial())
2. pad the image (padarray()) to accomodate half the width of the filter kernel to overhang
3. preallocate the output array to the size of the original image
4. indexing by row and column in two nested loops, extract a rectangular region about the current index
5. multiply the sample region and the filter, sum the result
6. place the sum in the output array at the current index
7. repeat over entire image
Pay attention to image and filter class and data scaling. It will probably be easiest to convert the incoming image to floating-point by using im2double(). Moving the division outside the loop is not necessary, but it can significantly speed up processing. The division is not necessary in the second case, as the division is effected by the normalization of the filter kernel itself.
This example uses the second method described above. While it may be faster to implement flat filters without the multiplication (i.e. the first method above), this would be the more generalized approach.
##### 댓글 수: 1표시숨기기 없음
Rohit Kharat 2022년 1월 20일
Thank you!

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

Image Analyst 2022년 1월 19일
Here's some help:
Here's an example
function filteredImage = MyFilter(inputImage, windowSize)
kernel = ones(windowSize) / windowSize^2;
filteredImage = conv2(double(inputImage), kernel, 'same');
You can use other filters, like
1. imfilter()
2. medfilt2()
3. imnlmfilt()
4. stdfilt()
5. entropyfilt()
6. imguidedfilter()
etc.
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Image Analyst 2022년 1월 20일
" I want to implement this task" and I guess without any help of volunteers or else it wouldn't be your code. Anyway, I gave you the function structure so you just have to replace the inside with a double for loop where you loop over every pixel getting a window around it and getting the mean and assigning that mean value to an output array.
function filteredImage = MyFilter(inputImage, windowSize)
kernel = ones(windowSize) / windowSize^2;
[rows, columns, numberOfColorChannels] = size(inputImage)
filteredImage = zeros(size(inputImage));
for col = 1 : columns
for row = 1 : rows
subImage = .................
theMean = mean2(subImage);
filteredImage(row, col) = ................
end
end
Not sure how much more I can say without me just doing it for you, but then you'd risk getting into trouble if it's an assignment you have to turn in for a grade. Is it?

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

### Community Treasure Hunt

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

Start Hunting!