iT邦幫忙

1

影像處理:利用 Numpy 的 histogram 視覺化灰階影像的強度分佈

前言

這邊要聊聊,在處理醫學影像中的X光片時,要如何將影像的 灰階值分布視覺化 呈現。
醫學影像常見的儲存格式是 dicom 檔,而X光片通常儲存的位元深度是 12 或 16 bits,不過這邊會用轉換成 png 檔案格式、數值型態 uint8 的影像來做示範。

uint8:unsigned interger 8 bits, 0~255

  • unsigned,是「無符號的意思」,也就是儲存的都是正數
  • cf. int8: -255~255,儲存範圍從負的開始

方法與說明

主要是透過 numpy 中的 histogram [1] 得到數值分布

numpy.histogram(a, bins=10, range=None, normed=None, weights=None, density=None)[source]

再藉由 matplotlib.pyplot 中的 hist [2] 繪製出來

matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, *, data=None, **kwargs)

程式碼

import math
import numpy as np
import matplotlib.pyplot as plt

# 以5作為bin的間隔
base = 5

# 設定上界 (upper_bound) 的目的,是要方便得到bin的分界值
upper_bound = math.ceil(image.max()/base + 1) * base

# 計算影像中,每個bin累計的數值
counts, bin_edges = np.histogram(image, bins=np.arange(0, upper_bound, 5))
plt.hist(bin_edges[:-1], bin_edges, weights=counts)
plt.xlim(0, 255)

結果

References

[1] Documentation: numpy.histogram
[2] Documentation: matplotlib.pyplot.hist


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言