使用直方图统计量增强图像
全局均值和方差
μn=∑i=0L−1(ri−m)np(ri)(3.24)\mu_{n} = \sum_{i=0}^{L-1} (r_{i} - m)^{n} p(r_{i}) \tag{3.24}μn=i=0∑L−1(ri−m)np(ri)(3.24)
m=∑i=0L−1rip(ri)(3.25)m = \sum_{i=0}^{L-1} r_{i} p(r_{i}) \tag{3.25}m=i=0∑L−1rip(ri)(3.25)
σ2=μ2=∑i=0L−1(ri−m)2p(ri)(3.26)\sigma^{2} = \mu_{2} = \sum_{i=0}^{L-1} (r_{i} - m)^{2} p(r_{i}) \tag{3.26}σ2=μ2=i=0∑L−1(ri−m)2p(ri)(3.26)
邻域均值和方差
令SxyS_{xy}Sxy是以(x,y)(x, y)(x,y)为中心的一个规定大小的邻域
mSxy=∑i=0L−1riPSxy(ri)(3.27)m_{S_{xy}} = \sum_{i=0}^{L-1}r_{i} P_{S_{xy}}(r_{i}) \tag{3.27}mSxy=i=0∑L−1riPSxy(ri)(3.27)
σSxy2=∑i=0L−1(ri−mSxy)2PSxy(ri)(3.28)\sigma_{S_{xy}}^2 = \sum_{i=0}^{L-1} (r_{i} - m_{S_{xy}})^2 P_{S_{xy}}(r_{i}) \tag{3.28}σSxy2=i=0∑L−1(ri−mSxy)2PSxy(ri)(3.28)
g(x,y)={Cf(x,y),k0mG≤mSxy≤k1mGANDk2σG≤σSxy≤k3σGf(x,y),others(3.29)g(x, y) =\begin{cases} Cf(x,y), & k_0 m_G \leq m_{S_{xy}} \leq k_1 m_G \;\text{AND}\; k_2 \sigma_G \leq \sigma_{S_{xy}} \leq k_3 \sigma_G \\ f(x,y), & \text{others}\end{cases} \tag{3.29}g(x,y)={Cf(x,y),f(x,y),k0mG≤mSxy≤k1mGANDk2σG≤σSxy≤k3σGothers(3.29)
k0,k1,k2,k3,Ck_0, k_1, k_2, k_3, Ck0,k1,k2,k3,C选择的一些方法:
如果 我们的关注点是比平均灰度的1/4更暗的区域,那么选择k0=0,k1=0.25k_0=0, k_1=0.25k0=0,k1=0.25。如果我们的兴趣是增强那么对比度比较低的区域,则k2=0,k3=0.1k_2=0, k_3=0.1k2=0,k3=0.1。
满足条件的乘以一个规定常数CCC,增大(或减小)其相对于图像剩下部分的灰度。
下图像中mG=161,σG=103m_G = 161, \sigma_G = 103mG=161,σG=103,图像和待增强区域的最大灰度值分别是是228和10,最小值是为。我们希望增强后的特征的最大值与图像的最大值相同,因此选择C=22.8C=22.8C=22.8
def histogram_statistic(img_ori, grid_size = (3, 3), k0 = 0., k1 = 0.1, k2 = 0., k3 = 0.1, C = 22.8):"""Histogram statistic enhance local imageparam: input img_ori: uint8[0, 255] grayscal imageparam: grid size : size of the sliding window, default is [3, 3]"""m_G = np.round(np.mean(img_ori)).astype(int)sigma_G = np.round(np.std(img_ori)).astype(int)
# max_img = img_ori.max()
# min_img = img_ori.min()
# img_roi = img_ori[12:120, 12:120]
# max_roi = img_roi.max()
# print(f'Global mean -> {m_G}, Global sigma -> {sigma_G}, Max -> {max_img}, Min -> {min_img}, ROI Max -> {max_roi}')img_dst = img_ori.copy()height, width = img_ori.shapem = grid_size[0]n = grid_size[1]for h in range(height):for w in range(width):temp = img_ori[h:h+m, w:w+n]temp_mean = np.round(np.mean(temp)).astype(int)temp_sigma = np.round(np.std(temp)).astype(int)if k0 * m_G <= temp_mean <= k1 * m_G and k2 * sigma_G <= temp_sigma <= k3 * sigma_G:temp = (C * temp)img_dst[h:h+m, w:w+n] = tempreturn img_dst
# 使用直方图统计增强局部图像
img_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0326(a)(embedded_square_noisy_512).tif', 0)
m_G = np.round(np.mean(img_ori)).astype(int)
sigma_G = np.round(np.std(img_ori)).astype(int)
max_img = img_ori.max()
min_img = img_ori.min()
img_roi = img_ori[12:120, 12:120]
max_roi = img_roi.max()
print(f'Global mean -> {m_G}, Global sigma -> {sigma_G}, Max -> {max_img}, Min -> {min_img}, ROI Max -> {max_roi}')# ROI 区域的最小值是11, 228/11=20.72,即20.8,实现看起来效果也不错
img_dst = histogram_statistic(img_ori, C=20.8)plt.figure(figsize=(15, 6))
plt.subplot(1, 3, 1), plt.imshow(img_ori, cmap='gray', vmin=0, vmax=255), plt.title('Original')
plt.subplot(1, 3, 2), plt.imshow(img_dst, cmap='gray', vmin=0, vmax=255), plt.title(f'Histogram statistic')
plt.tight_layout()
plt.show()
Global mean -> 161, Global sigma -> 103, Max -> 228, Min -> 0, ROI Max -> 11