Harmonic Mean Filter: How To Use Harmonic Mean Filter On Images - C# Guide - Epoch Abuse
import random
import cv2
from matplotlib import pyplot
import numpy as np
def add_salt_noise(img):
# Getting the dimensions of the image
row, col = img.shape
# Randomly pick some pixels in the
# image for coloring them white
# Pick a random number between 300 and 10000
number_of_pixels = random.randint(300, 10000)
for i in range(number_of_pixels):
# Pick a random y coordinate
y_coord = random.randint(0, row - 1)
# Pick a random x coordinate
x_coord = random.randint(0, col - 1)
# Color that pixel to white
img[y_coord][x_coord] = 255
return img
def add_pepper_noise(img):
# Getting the dimensions of the image
row, col = img.shape
# Randomly pick some pixels in
# the image for coloring them black
# Pick a random number between 300 and 10000
number_of_pixels = random.randint(300, 10000)
for i in range(number_of_pixels):
# Pick a random y coordinate
y_coord = random.randint(0, row - 1)
# Pick a random x coordinate
x_coord = random.randint(0, col - 1)
# Color that pixel to black
img[y_coord][x_coord] = 0
return img
img = cv2.imread(r"C:\Users\jimore\Pictures\girl.jpeg",cv2.IMREAD_GRAYSCALE)
pyplot.figure(1)
pyplot.imshow(img)
salt_img=add_salt_noise(img)
# Showing the image
pyplot.figure(2)
pyplot.imshow(salt_img)
def harmonic_mean_filter(image, kernel_size):
height, width = image.shape
border = kernel_size // 2
filtered_image = np.zeros_like(image, dtype=np.float32)
for y in range(border, height - border):
for x in range(border, width - border):
neighborhood = image[y - border:y + border + 1, x - border:x + border + 1]
inverse = 1.0 / (neighborhood + np.finfo(float).eps) # Add epsilon to avoid division by zero
filtered_image[y, x] = (kernel_size ** 2) / np.sum(inverse)
filtered_image = np.clip(filtered_image, 0, 255).astype(np.uint8)
return filtered_image
filtered_image1 = harmonic_mean_filter(salt_img, kernel_size=3)
pyplot.figure(3)
pyplot.imshow(filtered_image1)
pyplot.show()
filtered_image2 = harmonic_mean_filter(pepper_img, kernel_size=3)
pyplot.figure(3)
pyplot.imshow(filtered_image2)
pyplot.show()
The harmonic mean filter works well for salt noise, but fails for pepper noise.
Comtraharmonic Filter: 反调和均值滤波器_chenhonghai123的博客-CSDN博客
def contraharmonic_filter(image, kernel_size, Q):
# 获取图像的高度和宽度
height, width = image.shape
# 创建一个与原始图像大小相同的输出图像
filtered_image = np.zeros_like(image, dtype=np.float64)
# 将图像转换为浮点类型
image = image.astype(np.float64)
# 计算滤波器的边界
border = kernel_size // 2
# 对图像进行遍历,并应用滤波器
for i in range(border, height - border):
for j in range(border, width - border):
numerator_sum = 0
denominator_sum = 0
# 在滤波器范围内计算分子和分母的和
for k in range(-border, border + 1):
for l in range(-border, border + 1):
pixel_value = image[i + k, j + l]
numerator_sum += pixel_value ** (Q + 1)
denominator_sum += pixel_value ** Q
# 计算滤波后的像素值
filtered_pixel = numerator_sum / denominator_sum
# 将滤波后的像素值存储在输出图像中
filtered_image[i, j] = filtered_pixel
# 将输出图像转换为8位无符号整型
filtered_image = filtered_image.astype(np.uint8)
return filtered_image
When the Q is less than 0, it is available to the salt noise; and when the Q is bigger than 0, it is available to the pepper noise.
Max Filter
def max_filter(image, kernel_size):
height, width = image.shape[:2]
padding = kernel_size // 2
padded_image = np.pad(image, padding, mode='constant', constant_values=0)
filtered_image = np.zeros_like(image)
for i in range(height):
for j in range(width):
patch = padded_image[i:i+kernel_size, j:j+kernel_size]
filtered_image[i, j] = np.max(patch)
return filtered_image
It has an excellent performance on pepper noise.
Min Filter
def min_filter(image, kernel_size):
height, width = image.shape[:2]
padding = kernel_size // 2
padded_image = np.pad(image, padding, mode='constant', constant_values=0)
filtered_image = np.zeros_like(image)
for i in range(height):
for j in range(width):
patch = padded_image[i:i+kernel_size, j:j+kernel_size]
filtered_image[i, j] = np.min(patch)
return filtered_image
It has an excellent perfomance on salt noise.
Median Filter
def median_filter(image, kernel_size):
height, width = image.shape[:2]
padding = kernel_size // 2
padded_image = np.pad(image, padding, mode='constant', constant_values=0)
filtered_image = np.zeros_like(image)
for i in range(height):
for j in range(width):
patch = padded_image[i:i+kernel_size, j:j+kernel_size]
filtered_image[i, j] = np.median(patch)
return filtered_image
It is good at handling the Salt-and-Pepper Noise.
Mean Filter
def mean_filter(image, kernel_size):
height, width = image.shape[:2]
padding = kernel_size // 2
padded_image = np.pad(image, padding, mode='constant', constant_values=0)
filtered_image = np.zeros_like(image)
for i in range(height):
for j in range(width):
patch = padded_image[i:i+kernel_size, j:j+kernel_size]
filtered_image[i, j] = np.mean(patch)
return filtered_image
Adaptive Median Filter
def adaptive_median_filter(image, max_window_size):
height, width = image.shape
filtered_image = np.copy(image)
border_size = max_window_size // 2
for i in range(border_size, height - border_size):
for j in range(border_size, width - border_size):
window_size = 3 # 初始窗口大小为3x3
while window_size <= max_window_size:
window = image[i - border_size:i + border_size + 1, j - border_size:j + border_size + 1].flatten()
median = np.median(window)
min_val = np.min(window)
max_val = np.max(window)
current_pixel = image[i, j]
if min_val < median < max_val:
if current_pixel <= min_val or current_pixel >= max_val:
filtered_image[i, j] = median
break # 跳出窗口大小的循环
else:
window_size += 2 # 增加窗口大小
return filtered_image
It has an better performance than the Median Filter.