Mean filter with kernel size

44 visualizaciones (últimos 30 días)
Rohit Kharat
Rohit Kharat el 19 de En. de 2022
Comentada: Image Analyst el 20 de En. de 2022
I want to create a function which does mean filtering with taking image and kernel size as the input. Please help in this regard.

Respuestas (2)

DGM
DGM el 19 de En. de 2022
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 is one example, though I admit that the thread title is completely misleading.
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.

Image Analyst
Image Analyst el 19 de En. de 2022
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 comentarios
Rohit Kharat
Rohit Kharat el 20 de En. de 2022
Actually I want to implement this task from scratch i.e. without any help of in-built functions.
Image Analyst
Image Analyst el 20 de En. de 2022
" 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?

Iniciar sesión para comentar.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by