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.
72 lines
2.5 KiB
72 lines
2.5 KiB
import numpy as np
|
|
import cv2
|
|
import math
|
|
def noise_desc():
|
|
image = cv2.imread("image/test.png", cv2.IMREAD_GRAYSCALE)
|
|
output = np.zeros(image.shape, np.uint8)
|
|
# 遍历图像,获取叠加噪声后的图像
|
|
for i in range(image.shape[0]):
|
|
for j in range(image.shape[1]):
|
|
if image[i][j]<100:
|
|
# 添加食盐噪声
|
|
output[i][j]=255
|
|
elif image[i][j]>200:
|
|
# 添加胡椒噪声
|
|
output[i][j]=0
|
|
else:
|
|
# 不添加噪声
|
|
output[i][j]=image[i][j]
|
|
cv2.imwrite("image/result.jpg",output)
|
|
print(np.sum(output))
|
|
|
|
def aver_filter():
|
|
image = cv2.imread("image/test.png", cv2.IMREAD_GRAYSCALE)
|
|
output = np.zeros(image.shape, np.uint8)
|
|
# 遍历图像,进行均值滤波
|
|
for i in range(image.shape[0]):
|
|
for j in range(image.shape[1]):
|
|
# 计算均值,完成对图片src的几何均值滤波
|
|
ji = 1.0
|
|
for n in range(-1,2):
|
|
# 防止越界
|
|
if 0 <= j + n < image.shape[1]:
|
|
ji *= image[i][j + n]
|
|
output[i][j] = int(math.pow(ji,1/3))
|
|
|
|
# 滤波器的大小为1*3
|
|
|
|
######### End #########
|
|
# 展示均值滤波后的图片
|
|
cv2.imwrite("image/result.jpg", output)
|
|
|
|
def sort_filter():
|
|
image = cv2.imread("image/test.png", cv2.IMREAD_GRAYSCALE)
|
|
# 待输出的图片
|
|
output = np.zeros(image.shape, np.uint8)
|
|
for i in range(image.shape[0]):
|
|
for j in range(image.shape[1]):
|
|
# 最大值滤波器
|
|
max=0
|
|
for m in range(-1, 2):
|
|
for n in range(-1, 2):
|
|
# 防止越界
|
|
if 0 <= i + m < image.shape[0] and 0 <= j + n < image.shape[1]:
|
|
if max<image[i+m][j+n]:
|
|
max=image[i+m][j+n]
|
|
# 更新最大值
|
|
output[i][j]=max
|
|
cv2.imwrite("image/result.jpg",output)
|
|
|
|
def opt_filter():
|
|
image = cv2.imread("image/test.png", cv2.IMREAD_GRAYSCALE)
|
|
output = np.zeros(image.shape, np.uint8)
|
|
max=200
|
|
for i in range(image.shape[0]):
|
|
for j in range(image.shape[1]):
|
|
if image[i][j] <= max:
|
|
output[i][j] = 0
|
|
else:
|
|
output[i][j] = image[i][j]
|
|
cv2.imwrite("image/result.jpg",output)
|
|
|