直方圖匹配(規(guī)定化)
- 連續(xù)灰度
s=T(r)=(L?1)∫0rpr(w)dw(3.17)s = T(r) = (L-1) \int_{0}^{r} p_r(w) \textze8trgl8bvbq w \tag{3.17} s=T(r)=(L?1)∫0r?pr?(w)dw(3.17)
定義關(guān)于變量zzz的一個函數(shù)GGG,它具有如下性質(zhì):
G(z)=(L?1)∫0zpz(v)dv=s(3.18)G(z) = (L - 1) \int_{0}^{z} p_z(v) \textze8trgl8bvbq v = s \tag{3.18} G(z)=(L?1)∫0z?pz?(v)dv=s(3.18)
因此zzz必定滿足條件:
z=G?1(s)=G?1[T(r)](3.19)z = G^{-1}(s) = G^{-1}[T(r)] \tag{3.19}z=G?1(s)=G?1[T(r)](3.19)
使用如下步驟可以得到一幅灰度級具有規(guī)定PDF的圖像:
由輸入圖像得到式(3.17)中使用的pr(r)p_{r}(r)pr?(r)。在式(3.18)中使用規(guī)定的PDF即pz(z)p_{z}(z)pz?(z)得到函數(shù)G(z)G(z)G(z)。計算反變換z=G?1(s)z=G^{-1}(s)z=G?1(s);輸出圖像中的像素值是sss。對于均衡化后的圖像中值為sss的每個像素執(zhí)行逆映射z=G?1(s)z=G^{-1}(s)z=G?1(s),得到輸出圖像中的對應(yīng)像素。使用這個變換處理完所有像素后,輸出圖像的PDF即pz(z)p_{z}(z)pz?(z)將等于規(guī)定的PDF。
- 離散灰度
sk=T(rk)=(L?1)∑j=0kpr(rj),k=0,1,2,…,L?1(3.20)s_{k} = T(r_{k}) = (L -1) \sum_{j=0}^k p_{r}(r_{j}),\quad k = 0, 1, 2, \dots, L-1 \tag{3.20}sk?=T(rk?)=(L?1)j=0∑k?pr?(rj?),k=0,1,2,…,L?1(3.20)
G(zq)=(L?1)∑i=0qpz(zi),q=0,1,2,…,L?1(3.21)G(z_{q}) = (L -1) \sum_{i=0}^q p_{z}(z_{i}),\quad q = 0, 1, 2, \dots, L-1 \tag{3.21}G(zq?)=(L?1)i=0∑q?pz?(zi?),q=0,1,2,…,L?1(3.21)
G(zq)=sk(3.22)G(z_{q}) = s_{k} \tag{3.22}G(zq?)=sk?(3.22)
zq=G?1(sk)(3.23)z_{q} = G^{-1}(s_{k}) \tag{3.23}zq?=G?1(sk?)(3.23)
離散直方圖規(guī)定化的過程
計算輸入圖像的直方圖pr(r)p_{r}(r)pr?(r),并在式(3.20)中用它將輸入圖像中的灰度映射到直方圖均衡化后的圖像中的灰度。將得到的值sks_{k}sk?四舍五入到整數(shù)區(qū)間[0,L?1][0, L-1][0,L?1]。用式(3.21)對q=0,1,2,…,L?1q = 0, 1, 2, \dots, L-1q=0,1,2,…,L?1計算函數(shù)G(zq)G(z_{q})G(zq?)的所有值,其中pz(zi)p_{z}(z_{i})pz?(zi?)是規(guī)定直方圖的值。將GGG的值四舍五入到區(qū)間[0,L?1][0, L-1][0,L?1]內(nèi)的整數(shù)。并存儲到一個查找表中。對sk,k=0,1,2,…,L?1s_{k}, k=0, 1, 2, \dots, L-1sk?,k=0,1,2,…,L?1的每個值,用步驟2中存儲的GGG值找到zqz_{q}zq?的對應(yīng)值,使得G(zq)G(z_{q})G(zq?)最接近sks_{k}sk?。存儲從sss到zzz的這些映射。當(dāng)多個zqz_{q}zq?值給出相同的匹配(即映射不唯一)時,按照約定選擇最小的值。使用步驟3中找到的映射,將每個均衡化后的像素sks_{k}sk?值映射到直方圖規(guī)定化圖像中值為zqz_{q}zq?的對應(yīng)像素,并形成直方圖規(guī)定化后的圖像。
def my_histogram_matching(img_src
, img_dst
):"""historgram specification for two input imagesparam: input img_src: input image used for histogram specification , uint8[0, 255] grayscale imageparam: input img_dst: input image for histogram specification , uint8[0, 255] grayscale imagereturn: uint8[0, 255] grayscale image after histogram specification """bins
= 256hist_src
, bins_src
= my_hist
(img_src
, bins
=256, normalized
=True)hist_dst
, bins_dst
= my_hist
(img_dst
, bins
=256, normalized
=True)list_src
= list([x
, y
] for x
in np
.arange
(bins
) for y
in np
.arange
(bins
) if y
== 0) for i
in bins_src
:s
= np
.round(255 * hist_src
[:i
].sum()).astype
(int)list_src
[i
][1] = slist_dst
= list([x
, y
] for x
in np
.arange
(bins
) for y
in np
.arange
(bins
) if y
== 0)for i
in bins_dst
:s
= np
.round(255 * hist_dst
[:i
].sum()).astype
(int)list_dst
[i
][1] = slist_dst_1
= list([x
, y
] for x
in np
.arange
(bins
) for y
in np
.arange
(bins
) if y
== 0)for i
in range(256):minv
= 1temp_dst
= list_dst
[i
][1]for j
in range(256):temp_src
= list_src
[j
][1]if abs(temp_dst
- temp_src
) < minv
:minv
= abs(temp_dst
- temp_src
)idx
= int(j
)list_dst_1
[i
][1] = idxmap_dict
= np
.array
(list_dst_1
)[:, 1]img_result
= img_dst
.copy
()img_result
= map_dict
[img_result
]
return img_result
img_1
= cv2
.imread
('DIP_Figures/DIP3E_Original_Images_CH03/Fig0307(a)(intensity_ramp).tif', 0)
img_2
= cv2
.imread
('DIP_Figures/DIP3E_Original_Images_CH03/Fig0316(4)(bottom_left).tif', 0)
hist_1
, bins_1
= my_hist
(img_1
, bins
=256, normalized
=True)
hist_2
, bins_2
= my_hist
(img_2
, bins
=256, normalized
=True)img_dst
= my_histogram_matching
(img_1
, img_2
,)
hist_dst
, bins_dst
= my_hist
(img_dst
, bins
=256, normalized
=True)plt
.figure
(figsize
=(15, 10))
plt
.subplot
(2, 3, 1), plt
.imshow
(img_1
, 'gray', vmin
=0, vmax
=255), plt
.title
('src')
plt
.subplot
(2, 3, 2), plt
.imshow
(img_2
, 'gray', vmin
=0, vmax
=255), plt
.title
('dst')
plt
.subplot
(2, 3, 3), plt
.imshow
(img_dst
, 'gray', vmin
=0, vmax
=255), plt
.title
('Result')
plt
.subplot
(2, 3, 4), plt
.bar
(bins_1
, hist_1
), plt
.xlim
([0, 255])
plt
.subplot
(2, 3, 5), plt
.bar
(bins_2
, hist_2
), plt
.xlim
([0, 255])
plt
.subplot
(2, 3, 6), plt
.bar
(bins_dst
, hist_dst
), plt
.xlim
([0, 255])
plt
.tight_layout
()
plt
.show
()
img_1
= cv2
.imread
('DIP_Figures/DIP3E_Original_Images_CH06/Fig0651(a)(flower_no_compression).tif',)[..., ::-1]
img_2
= cv2
.imread
('DIP_Figures/DIP3E_Original_Images_CH06/Fig0638(a)(lenna_RGB).tif', )[..., ::-1]img_dst
= np
.zeros_like
(img_2
, dtype
=np
.uint8
)for i
in range(3):img_dst
[..., i
] = my_histogram_matching
(img_1
[..., i
], img_2
[..., i
])plt
.figure
(figsize
=(15, 10))
plt
.subplot
(2, 3, 1), plt
.imshow
(img_1
, vmin
=0, vmax
=255), plt
.title
('src')
plt
.subplot
(2, 3, 2), plt
.imshow
(img_2
, vmin
=0, vmax
=255), plt
.title
('dst')
plt
.subplot
(2, 3, 3), plt
.imshow
(img_dst
, vmin
=0, vmax
=255), plt
.title
('Result')
plt
.tight_layout
()
plt
.show
()
總結(jié)
以上是生活随笔為你收集整理的第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波9 - 直方图处理 - 直方图匹配(规定化)灰度图像,彩色图像都适用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。