You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
10195101475/去噪滤波器集合.py

92 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import sys
import random
import cv2
import numpy as np
import matplotlib.pyplot as plt
def Arithmetic_mean_filtering(img_path):
img = cv2.imread(img_path,0) # 读取图片
result1 = np.zeros(img.shape, np.uint8)
# 算数均值滤波
for i in range(img.shape[0]):
for j in range(img.shape[1]):
sum = 0
for m in range(-1, 2):
for n in range(-1, 2):
if 0 <= i + m < img.shape[0] and 0 <= j + n < img.shape[1]:
# 像素值求和
sum += img[i + m][j + n]
result1[i][j] = (sum / 9).astype(np.int32)
cv2.imwrite('./output/Arithmetic_mean_filtering.jpg', result1)
def Maximum_mean_filtering(img_path):
img = cv2.imread(img_path,0) # 读取图片
result = np.zeros(img.shape, np.uint8)
# 最大值滤波器
for i in range(img.shape[0]):
for j in range(img.shape[1]):
# 最大值滤波器
max_ = 0
for m in range(-1, 2):
for n in range(-1, 2):
if 0 <= i + m < img.shape[0] and 0 <= j + n < img.shape[1]:
# 通过比较判断是否需要更新最大值
if img[i + m][j + n] > max_:
# 更新最大值
max_ = img[i + m][j + n]
result[i][j] = max_
cv2.imwrite('./output/Maximum_filter.jpg', result)
def GaussianBlur_filtering(img_path):
img = cv2.imread(img_path) # 读取图片
blur = cv2.GaussianBlur(img, (5, 5), 0, 0)
cv2.imwrite('./output/GaussianBlur_filter.jpg', blur)
def medianBlur_filtering(img_path):
img = cv2.imread(img_path) # 读取图片
median = cv2.medianBlur(img, 5)
cv2.imwrite('./output/median_filter.jpg', median)
def Geometric_mean_filtering(img_path):
img = cv2.imread(img_path,0) # 读取图片
result1 = np.zeros(img.shape, np.uint8)
# 几何均值滤波
for i in range(img.shape[0]):
for j in range(img.shape[1]):
ji = 1.0
for m in range(-1, 2):
for n in range(-1, 2):
if 0 <= i + m < img.shape[0] and 0 <= j + n < img.shape[1]:
ji = ji * img[i + m][j + n]
result1[i][j] = pow(ji, 1 / 9)
cv2.imwrite('./output/Geometric_mean_filtering.jpg', result1)
def filtering(img_path, type):
if type == 1:
Arithmetic_mean_filtering(img_path)
elif type == 2:
Geometric_mean_filtering(img_path)
elif type == 3:
Maximum_mean_filtering(img_path)
elif type == 4:
GaussianBlur_filtering(img_path)
elif type == 5:
medianBlur_filtering(img_path)
'''
输入img_path:添加了噪声的图片
type:滤波种类
输出:滤波的结果
'''
if __name__ == '__main__':
filtering(sys.argv[1], eval(sys.argv[2]))