# 估计噪声参数

```# 一些重要的噪声对应灰度的直方图
# 竖图[40:210, 35:60]，横图[40:60, 35:220]
img_gauss    = add_gaussian_noise(img_ori, mu=0, sigma=0.05)[40:60, 35:220]
img_average   = add_average_noise(img_ori, mean=10, sigma=1.5)[40:60, 35:220]

ps = 0.05
pp = 0.02
img_salt_pepper = add_salt_pepper(img_ori, ps=ps, pp=pp)[40:60, 35:220]

show_list = ['img_gauss', 'img_rayleigh', 'img_gamma', 'img_exponent', 'img_average', 'img_salt_pepper']

fig = plt.figure(figsize=(15, 15))

for i in range(len(show_list)):
if i >= 3:
# 显示图像
ax = fig.add_subplot(4, 3, i + 3 + 1)
ax.imshow(eval(show_list[i]), 'gray'), ax.set_xticks([]), ax.set_yticks([]), ax.set_title(show_list[i].split('_')[-1])
# 对应图像的直方图
ax = fig.add_subplot(4, 3, i + 1 + 6)
hist, bins = np.histogram(eval(show_list[i]).flatten(), bins=255, range=[0, 255], density=True)
bar = ax.bar(bins[:-1], hist[:]), ax.set_xticks([]), ax.set_yticks([]),
else:
# 显示图像
ax = fig.add_subplot(4, 3, i + 1)
ax.imshow(eval(show_list[i]), 'gray'), ax.set_xticks([]), ax.set_yticks([]), ax.set_title(show_list[i].split('_')[-1])
# 对应图像的直方图
ax = fig.add_subplot(4, 3, i + 1 + 3)
hist, bins = np.histogram(eval(show_list[i]).flatten(), bins=255, range=[0, 255], density=True)
bar = ax.bar(bins[:-1], hist[:]), ax.set_xticks([]), ax.set_yticks([]),

plt.tight_layout()
plt.show()```

```# 椒盐噪声的参数估计
hist, bins = np.histogram(img_salt_pepper.flatten(), bins=255, range=[0, 255], density=True)
print(f"Original pp -> {pp:.3f}, ps -> {ps:.3f}")
print(f'Estimate PP -> {hist[0]:.3f}, PS -> {hist[-1]:.3f}')```
```Original pp -> 0.020, ps -> 0.050
Estimate PP -> 0.018, PS -> 0.050```
```# 内嵌图像
fig, main_ax = plt.subplots()
hist, bins = np.histogram(img_gauss.flatten(), bins=255, range=[0, 255], density=True)
bar = main_ax.bar(bins[:-1], hist[:]), main_ax.set_xticks([]), main_ax.set_yticks([])

inset_ax = fig.add_axes([0.1, 0.3, 0.2, 0.5])
inset_ax.imshow(img_gauss.reshape(185, 20), 'gray'), inset_ax.set_xticks([]), inset_ax.set_yticks([])

plt.show()```