• 微信公众号：美女很有趣。 工作之余，放松一下，关注即送10G+美女照片！

# Histogram Processing

4小时前 2次浏览

• HISTOGRAM EQUALIZATION
• 代码示例
• HISTOGRAM MATCHING (SPECIFICATION)
• 其它

Gonzalez R. C. and Woods R. E. Digital Image Processing (Forth Edition).

(r_k, k = 0, 1,2, cdots, L-1) 表示图片密度值为(k),

[h(r_k) = n_k, : k = 0, 1, cdots, L-1,
]

[p(r_k) = frac{h(r_k)}{MN} = frac{n_k}{MN},
]

## HISTOGRAM EQUALIZATION

[s = T(r), quad 0 le r le L-1,
]

1. (T(r))(0le r le L-1)熵是一个单调函数;
2. (0 le T(r) le L-1, quad forall 0 le r le L-1).

[s = T(r) = (L-1) int_0^r p_r(w) mathrm{d} w.
]

[s sim U[0, L-1].
]

[s_k = T(r_k) = (L – 1) sum_{j=0}^k p_r (r_j), : k=0,1,cdots, L-1.
]

### 代码示例

``````import cv2
import matplotlib.pyplot as plt
import numpy as np
``````
``````# 加载图片
pollen.shape # (377, 376, 3) 由于是截的图, 所以是3通道的
pollen = cv2.cvtColor(pollen, cv2.COLOR_BGR2GRAY) # 先转成灰度图
pollen.shape # (377, 376)
plt.imshow(pollen, cmap='gray')
``````

``````# 来看一下r的分布
hist = cv2.calcHist([pollen], [0], None, [256], (0, 255)).squeeze()
plt.bar(x=np.arange(256), height=hist)
``````

``````# 自己的实现 img 是灰度图, 且 0, 1, ..., 255
def equalizeHist(img):
m, n = img.shape
hist = cv2.calcHist([img], [0], None, [256], (0, 255)).squeeze() / (m * n)
cum_sum = 0
for r in range(256):
cum_sum += hist[r]
img2 = img.copy()
for i in range(m):
for j in range(n):
r = img[i, j].item()
return np.array(img2)
``````
``````pollen2 = equalizeHist(pollen)
plt.imshow(pollen2, cmap='gray')
``````

``````hist = cv2.calcHist([pollen2], [0], None, [256], (0, 255)).squeeze()
plt.bar(x=np.arange(256), height=hist)
``````

``````# cv2 官方实现
pollen3 = cv2.equalizeHist(pollen)
plt.imshow(pollen3, cmap='gray')
``````

``````hist = cv2.calcHist([pollen3], [0], None, [256], (0, 255)).squeeze()
plt.bar(x=np.arange(256), height=hist)
``````

## HISTOGRAM MATCHING (SPECIFICATION)

[s = T(r) = (L-1) int_0^r p_r (w) mathrm{d} w,
]

[s = G(z) = (L-1) int_0^z p_z (v) mathrm{d} v,
]

(T(r) = s =G(z)), 既然在连续的情况下(s)是均匀的, 故

[z = G^{-1} T(r),
]

1. 通过

[T(r_k), : k = 0, 1, cdots, L-1,
]

建立字典

[d_{rs}={r_k:mathrm{round}(T(r_k))}.
]

2. 通过

[G(z_k), : k = 0, 1, cdots, L-1,
]

[d_{sz} = {s_k:z_j}.
]

1. (r rightarrow z):
[z = d_{sz}[d_{rs}[r]].
]

## 其它

[mu_n = sum_{i=0}^{L-1} (r_i-m)^n p(r_i), \
m = sum_{i=0}^{L-1}r_i p(r_i),
]

[sigma^2 = sum_{i=0}^{L-1} (r_i – m)^2 p(r_i),
]