# sun
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
old_up_path = r"C:UserssunDesktopdevide_8"
new_up_path = r"C:UserssunDesktopfen_lei"
panel_name = os.listdir(old_up_path)
for panel_names in panel_name:
picture_name = os.listdir(panel_names)
old_path = os.path.join(old_up_path, panel_names)
new_path = os.path.join(new_up_path, panel_names)
for picture_names in picture_name:
# 读取图片
path = os.path.join(old_path, picture_names)
path2 = os.path.join(new_path, picture_names)
# 读取图片
img = cv2.imread(path)
# source = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 中值滤波
result = cv2.medianBlur(img, 3)
# 保存图像
cv2.imwrite(path2, result)
# 前后对比展示
# titles = ['原始图像', '中值滤波']
# images = [img, result]
# for i in range(2):
# plt.subplot(1, 2, i + 1), plt.imshow(images[i], 'gray')
# plt.title(titles[i])
# plt.xticks([]), plt.yticks([])
# plt.show()
# # 绘制直方图
# plt.hist(img.ravel(), 256)
# plt.xlabel('x')
# plt.ylabel('y')
# plt.show()
#
# plt.hist(result.ravel(), 256)
# plt.xlabel('x')
# plt.ylabel('y')
# plt.show()
经过中值变化后,两个图的直方图还是有所区别的。



