import numpy as np
import matplotlib.pyplot as plt
mu=0 #均值
sigma=0.8 #标准差
x=np.linspace(-3,3,60)
y=np.exp((-(x-mu)**2)/(2*(sigma**2)))/(np.sqrt(2*np.pi)*sigma)
plt.plot(x,y,"b-",)
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-3, 3.1, 0.1, dtype=np.float64).reshape(-1, 1)
Y = np.arange(-3, 3.1, 0.1, dtype=np.float64)
mux, muy = 0, 0
sigmax, sigmay = 0.8, 0.8
expont = -0.5 * (((X - mux) / sigmax) ** 2 + ((Y - muy) / sigmay) ** 2)
Z = np.exp(expont) / (2 * np.pi * sigmax * sigmay)
ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap=cm.viridis)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
X = np.arange(-2, 3, 1,dtype=np.float64).reshape(-1,1)#转置
Y = np.arange(-2, 3, 1,dtype=np.float64) #[-2. -1. 0. 1. 2.]
mux,muy=0,0
sigmax,sigmay = 0.8,0.8
h=((X-mux)/sigmax)**2
hh=((Y-muy)/sigmay)**2
hhh=h+hh
expont = -0.5*(((X-mux)/sigmax)**2 + ((Y-muy)/sigmay)**2)
Z=np.exp(expont)/(2*np.pi*sigmax*sigmay)
Z=Z/np.sum(Z) #归一化
print(Z)
sns.heatmap(Z) ##热力图
plt.show()
import numpy as np
import cv2
def pre_convolve(img,fill):
fill_height = fill.shape[0]#卷积高
fill_width = fill.shape[1]#卷积宽
P1,P2=fill_height//2,fill_width//2
img=np.pad(img,((P1,P1),(P2,P2)),'constant',constant_values=(0,0))
img_height = img.shape[0]#图像高
img_width = img.shape[1]#图像宽
#图像进行卷积后的图像大小
img_pro_height = img_height - fill_height + 1
img_pro_width = img_width - fill_width + 1
pro_rgb = np.zeros((img_pro_height,img_pro_width),dtype='uint8')
for i in range(img_pro_height):
for j in range(img_pro_width):
pro_rgb[i][j] = pro_statistic(img[i:i + fill_height,j:j + fill_width],fill)
return pro_rgb
def pro_statistic(img,fill):
res = (img * fill).sum()
if(res > 255):
res = 255
elif(res < 0):
res = abs(res) # 让负边缘也显现出来
return res
def convolve(img,fill):#彩色图像分为RGB
rgb_r = img[:,:,0]
rgb_g = img[:,:,1]
rgb_b = img[:,:,2]
pro_rgb_r = pre_convolve(rgb_r,fill)#每层进行卷积操作
pro_rgb_g = pre_convolve(rgb_g,fill)
pro_rgb_b = pre_convolve(rgb_b,fill)
img_pro = np.dstack((pro_rgb_r,pro_rgb_g,pro_rgb_b))#合并三层,返回图像
return img_pro
def gaussian(kernel_size,sigma):
sigma_3 = 3 * sigma
X = np.linspace(-sigma_3, sigma_3, kernel_size)
Y = np.linspace(-sigma_3, sigma_3, kernel_size)
x, y = np.meshgrid(X, Y)
gauss_1 = 1 / (2 * np.pi * sigma ** 2) * np.exp(- (x ** 2 + y ** 2) / (2 * sigma ** 2))
Z = gauss_1.sum() # 计算归一化系数
gauss_2 = (1 / Z) * gauss_1
return gauss_2
kernel=gaussian(5,1)
print(kernel)
img=cv2.imread('img1.png')
blur=convolve(img,kernel)
blur1=cv2.GaussianBlur(img, (5, 5), 1)
cv2.imshow('image',np.hstack((blur,blur1)))
cv2.imwrite('image.png',np.hstack((blur,blur1)))
cv2.waitKey()
这里发现自己手写的和opencv实现的高斯滤波还是存在很大的区别的,可能是高斯卷积核不一样
import matplotlib.pyplot as plt
import cv2
plt.rc('font', family='Youyuan', size='9')
img = cv2.imread('img1.png')
img_ret1 = cv2.GaussianBlur(img, (3, 3), 0)
img_ret2 = cv2.GaussianBlur(img, (5, 5), 0)
img_ret3 = cv2.GaussianBlur(img, (11, 11), 0)
# 显示图像
fig, ax = plt.subplots(2, 2)
ax[0, 0].set_title('原图')
ax[0, 0].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # matplotlib显示图像为rgb格式
ax[0, 1].set_title('GaussianBlur ksize=3')
ax[0, 1].imshow(cv2.cvtColor(img_ret1, cv2.COLOR_BGR2RGB))
ax[1, 0].set_title('GaussianBlur ksize=5')
ax[1, 0].imshow(cv2.cvtColor(img_ret2, cv2.COLOR_BGR2RGB))
ax[1, 1].set_title('GaussianBlur ksize=11')
ax[1, 1].imshow(cv2.cvtColor(img_ret3, cv2.COLOR_BGR2RGB))
ax[0, 0].axis('off');
ax[0, 1].axis('off');
ax[1, 0].axis('off');
ax[1, 1].axis('off') # 关闭坐标轴显示
plt.show()
import matplotlib.pyplot as plt
import cv2
plt.rc('font', family='Youyuan', size='9')
img = cv2.imread('img1.png')
img_ret1 = cv2.GaussianBlur(img, (5, 5), 0.5)
img_ret2 = cv2.GaussianBlur(img, (5, 5), 10)
img_ret3 = cv2.GaussianBlur(img, (5, 5), 25)
# 显示图像
fig, ax = plt.subplots(2, 2)
ax[0, 0].set_title('原图')
ax[0, 0].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # matplotlib显示图像为rgb格式
ax[0, 1].set_title('GaussianBlur ksize=5 sigma=0.5')
ax[0, 1].imshow(cv2.cvtColor(img_ret1, cv2.COLOR_BGR2RGB))
ax[1, 0].set_title('GaussianBlur ksize=5 sigma=10')
ax[1, 0].imshow(cv2.cvtColor(img_ret2, cv2.COLOR_BGR2RGB))
ax[1, 1].set_title('GaussianBlur ksize=5 sigma=25')
ax[1, 1].imshow(cv2.cvtColor(img_ret3, cv2.COLOR_BGR2RGB))
ax[0, 0].axis('off');
ax[0, 1].axis('off');
ax[1, 0].axis('off');
ax[1, 1].axis('off') # 关闭坐标轴显示
plt.show()