import matplotlib.pyplot as plt
import numpy as np
import math
from PIL import Image
import cv2
import os
def rgb2gray(rgb):
return np.dot(rgb[..., :3], [0.5,0.5,0])
def change_all_xml(path):
filelist = os.listdir(path)
print(filelist)
for kkk in filelist[:]:
img_road = path+kkk
img = plt.imread(img_road)
width = 517
height = 517
img = Image.open(img_road)
img = img.resize((height,width), Image.ANTIALIAS)
img = np.array(img)
print(img.shape)
sigma1 = sigma2 = 0.5
sum = 0
img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
gaussian = np.zeros([5, 5])
for i in range(5):
for j in range(5):
gaussian[i, j] = math.exp(-1 / 2 * (np.square(i - 3) / np.square(sigma1) # 生成二维高斯分布矩阵
+ (np.square(j - 3) / np.square(sigma2)))) / (2 * math.pi * sigma1 * sigma2)
sum = sum + gaussian[i, j]
gaussian = gaussian / sum
# step1.高斯滤波
gray = rgb2gray(img)
W, H = gray.shape
new_gray = np.zeros([W , H ])
for i in range(W - 5):
for j in range(H - 5):
new_gray[i, j] = np.sum(gray[i:i+5, j:j + 5] * gaussian) # 与高斯矩阵卷积实现滤波
# if new_gray[i, j] <100:
# new_gray[i, j] = 25
# else:
# new_gray[i, j] = 0
ROAD = 'model/'+kkk
cv2.imwrite(ROAD,new_gray)
change_all_xml('picture/')
Python改变图片大小+高斯滤波
猜你喜欢
转载自www.cnblogs.com/WalterJ726/p/12914751.html
今日推荐
周排行