日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波9 - 直方图处理 - 直方图匹配(规定化)灰度图像,彩色图像都适用

發布時間:2023/12/10 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波9 - 直方图处理 - 直方图匹配(规定化)灰度图像,彩色图像都适用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

直方圖匹配(規定化)

  • 連續灰度
    s=T(r)=(L?1)∫0rpr(w)dw(3.17)s = T(r) = (L-1) \int_{0}^{r} p_r(w) \textozvdkddzhkzd w \tag{3.17} s=T(r)=(L?1)0r?pr?(w)dw(3.17)
    定義關于變量zzz的一個函數GGG,它具有如下性質:
    G(z)=(L?1)∫0zpz(v)dv=s(3.18)G(z) = (L - 1) \int_{0}^{z} p_z(v) \textozvdkddzhkzd 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)

使用如下步驟可以得到一幅灰度級具有規定PDF的圖像:

  • 由輸入圖像得到式(3.17)中使用的pr(r)p_{r}(r)pr?(r)
  • 在式(3.18)中使用規定的PDF即pz(z)p_{z}(z)pz?(z)得到函數G(z)G(z)G(z)
  • 計算反變換z=G?1(s)z=G^{-1}(s)z=G?1(s);輸出圖像中的像素值是sss。對于均衡化后的圖像中值為sss的每個像素執行逆映射z=G?1(s)z=G^{-1}(s)z=G?1(s),得到輸出圖像中的對應像素。使用這個變換處理完所有像素后,輸出圖像的PDF即pz(z)p_{z}(z)pz?(z)將等于規定的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=0k?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=0q?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)

    離散直方圖規定化的過程

  • 計算輸入圖像的直方圖pr(r)p_{r}(r)pr?(r),并在式(3.20)中用它將輸入圖像中的灰度映射到直方圖均衡化后的圖像中的灰度。將得到的值sks_{k}sk?四舍五入到整數區間[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計算函數G(zq)G(z_{q})G(zq?)的所有值,其中pz(zi)p_{z}(z_{i})pz?(zi?)是規定直方圖的值。將GGG的值四舍五入到區間[0,L?1][0, L-1][0,L?1]內的整數。并存儲到一個查找表中。
  • 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?的對應值,使得G(zq)G(z_{q})G(zq?)最接近sks_{k}sk?。存儲從ssszzz的這些映射。當多個zqz_{q}zq?值給出相同的匹配(即映射不唯一)時,按照約定選擇最小的值。
  • 使用步驟3中找到的映射,將每個均衡化后的像素sks_{k}sk?值映射到直方圖規定化圖像中值為zqz_{q}zq?的對應像素,并形成直方圖規定化后的圖像。
  • 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 = 256# img_src -> input, img_dst -> matchinghist_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] = s# [原灰度級,均衡化的值] list_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] = s# 映射的關系列表初始化,以下映射關系算法list_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)# print(f'dst -> {temp_dst}, src -> {temp_src}, idx -> {idx}')list_dst_1[i][1] = idx#------------------------------Numpy-------------map_dict = np.array(list_dst_1)[:, 1]img_result = img_dst.copy()img_result = map_dict[img_result] #------------------------------loop----------------- # height, width = img_dst.shape[:2] # img_result = np.zeros([height, width], np.uint8) # for h in range(height): # for w in range(width): # img_result[h, w] = list_dst_1[img_dst[h, w]][1] # img_result = np.clip(img_result, 0, 255).astype(np.uint8)return img_result # Grayscale image histogram matching, different shape image works 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()

    # RGB 直方圖匹配 opencv讀入的是BGR圖像,[..., ::-1]可以把BGR轉為RGB的圖像 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()

    總結

    以上是生活随笔為你收集整理的第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波9 - 直方图处理 - 直方图匹配(规定化)灰度图像,彩色图像都适用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。