1.高斯图像σ的影响
import numpy as np
import math
import matplotlib.pyplot as plt
def g_draw_sigma(x, sigma = 1):
#高斯系数
xi_shu = 1 / (np.sqrt(2 * math.pi) * np.sqrt(sigma))
#高斯函数
gs = np.exp(- x ** 2 / (2 * sigma))
return xi_shu * gs
if __name__ == '__main__':
# x轴
x = np.arange(-4, 5, 0.1)
#
y_1 = g_draw_sigma(x, 0.2)
y_2 = g_draw_sigma(x, 1.0)
y_3 = g_draw_sigma(x, 5.0)
# 绘图
plt.plot(x, y_1, color="r")
plt.plot(x, y_2, color='g')
plt.plot(x, y_3, color='b')
# 设置坐标系
plt.xlim(-5.0, 5.0)
plt.ylim(0, 1)
ax = plt.gca()
#去除右边线与顶边线
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
#底边线
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
#左边线
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
#图列
plt.legend(labels=['$\sigma^2=0.2$', '$\sigma^2=1.0$', '$\sigma^2=5.0$'])
plt.show()
能得到:
图中红色是标准差为0.2的效果,绿色标准差为0.5,蓝色标准差为5.0,结果表面随着标准差的增大,中心位置的占比就越大,两边的占比就越小,说明大部分信息集中在中心。
高斯卷积核心
import cv2
import numpy as np
def main():
K_size = 5 #卷积核的大小
l = 150 #每个区域的像素大小
pad = K_size // 2 #用于寻找核的中心
img = np.zeros([K_size * l, K_size * l, 3], dtype=np.uint8)
for i in range(K_size):
for j in range(K_size):
x = i * l
y = j * l
img[x + 3: x + l - 3, y + 3: y + l - 3] = (100, 100, 0)
text = "(" + str(i - pad) + ", " + str(j - pad) + ")"
cv2.putText(img, text, (j * l + 20, (i + 1) * l - 60), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (200, 200, 0), 2)
cv2.imshow("img", img)
3.卷积核位置可视化
import cv2
import numpy as np
def main():
K_size = 5
l = 150
pad = K_size // 2
img = np.zeros([K_size * l, K_size * l, 3], dtype=np.uint8)
for i in range(K_size):
for j in range(K_size):
if (i - pad)**2 + (j - pad)**2 == 0:
x = i * l
y = j * l
img[x + 3: x + l - 3, y + 3: y + l - 3] = (71, 56, 219)
text = "(" + str(i - pad) + ", " + str(j - pad) + ")"
cv2.putText(img, text, (j * l + 20, (i + 1) * l - 60), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 0), 2)
elif (i - pad)**2 + (j - pad)**2 == 1:
x = i * l
y = j * l
img[x + 3: x + l - 3, y + 3: y + l - 3] = (90, 140, 252)
text = "(" + str(i - pad) + ", " + str(j - pad) + ")"
cv2.putText(img, text, (j * l + 20, (i + 1) * l - 60), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 0), 2)
elif (i - pad)**2 + (j - pad)**2 == 2:
x = i * l
y = j * l
img[x + 3: x + l - 3, y + 3: y + l - 3] = (146, 223, 255)
text = "(" + str(i - pad) + ", " + str(j - pad) + ")"
cv2.putText(img, text, (j * l + 20, (i + 1) * l - 60), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 0), 2)
elif (i - pad)**2 + (j - pad)**2 == 4:
x = i * l
y = j * l
img[x + 3: x + l - 3, y + 3: y + l - 3] = (243, 241, 230)
text = "(" + str(i - pad) + ", " + str(j - pad) + ")"
cv2.putText(img, text, (j * l + 20, (i + 1) * l - 60), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 0), 2)
elif (i - pad)**2 + (j - pad)**2 == 5:
x = i * l
y = j * l
img[x + 3: x + l - 3, y + 3: y + l - 3] = (224, 190, 144)
text = "(" + str(i - pad) + ", " + str(j - pad) + ")"
cv2.putText(img, text, (j * l + 20, (i + 1) * l - 60), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 0), 2)
else:
x = i * l
y = j * l
img[x + 3: x + l - 3, y + 3: y + l - 3] = (178, 116, 75)
text = "(" + str(i - pad) + ", " + str(j - pad) + ")"
cv2.putText(img, text, (j * l + 20, (i + 1) * l - 60), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 0), 2)