Write code for computing arithmetic_mean, geometric_mean, median, local noise reduction, and adaptive median filters. The input to your program is a 2D matrix.
filtering(): Write your code to perform image denoising/filtering here using the previous implemented filters. The steps can be used as a guideline for filtering. All the variable have already been initialized and can be used as self.image, self.filter_name, etc. The variable self.filter is a handle to each of the five filters functions.
The function returns the denoised image
filtering.py
import numpy as np
import math
import cv2
class Filtering:
def __init__(self, image, filter_name, filter_size, var = None):
"""initializes the variables of spatial filtering on an input image
takes as input:
image: the noisy input image
filter_name: the name of the mask to use
filter_size: integer value of the size of the mask
alpha_d: parameter of the alpha trimmed mean filter
order: parameter of the order for contra harmonic"""
self.image = image
if filter_name == 'arithmetic_mean':
self.filter = self.get_arithmetic_mean
elif filter_name == 'geometric_mean':
self.filter = self.get_geometric_mean
if filter_name == 'local_noise':
self.filter = self.get_local_noise
elif filter_name == 'median':
self.filter = self.get_median
elif filter_name == 'adaptive_median':
self.filter = self.get_adaptive_median
self.filter_size = filter_size
self.global_var = var
self.S_max = 15
def get_arithmetic_mean(self, roi):
"""Computes the arithmetic mean filter
takes as input:
kernel: a list/array of intensity values
returns the arithmetic mean value in the current kernel"""
return 0
def get_geometric_mean(self, roi):
"""Computes the geometric mean filter
takes as input:
kernel: a list/array of intensity values
returns the geometric mean value in the current kernel"""
return 0
def get_local_noise(self, kernel, roi):
"""Computes the result of local noise reduction
takes as input:
kernel: a list/array of intensity values
returns result of local noise reduction value of the current kernel"""
return
def get_median(self, roi):
"""Computes the median filter
takes as input:
kernel: a list/array of intensity values
returns the median value in the current kernel
"""
return 0
def get_adaptive_median(self, roi):
"""Computes the adaptive median filtering value
Note: Adaptive median filter may involve additional steps, you are welcome to create any additional functions as needed,
and you can change the signature of get_adaptive_median function as well.
takes as input:
kernel: a list/array of intensity values
returns the adaptive median filtering value"""
return 0
def filtering(self):
"""performs filtering on an image containing gaussian or salt & pepper noise
returns the denoised image
----------------------------------------------------------
Note: Filtering for the purpose of image restoration does not involve convolution.
For every pixel in the image, we select a neighborhood of values defined by the kernel and apply a mathematical
operation for all the elements with in the kernel. For example, mean, median and etc.
Steps:
1. add the necesssary zero padding to the noisy image that way we have sufficient values to perform the operations
on the border pixels. The number of rows and columns of zero padding is defined by the kernel size
2. Iterate through the image and every pixel (i,j) gather the neighbors defined by the kernel into a list (or any data structure)
3. Pass these values to one of the filters that will compute the necessary mathematical operations (mean, median, etc.)
4. Save the results at (i,j) in the ouput image.
5. return the output image
Please note that the adaptive median filter may involve additional steps, you are welcome to create any additional functions as needed,
and you can change the signature of get_adaptive_median function as well.
"""
return self.image
import numpy as np import math import cv2 class Filtering: def _
Copyright © 2012 - 2026 Apaxresearchers - All Rights Reserved.