長條圖(Histograms)
長條圖(Histograms) 會依像素強度把影像中的每個元素分到不同區間,並測量各區間的大小,以顯示影像中數值的分佈。
長條圖下方的面積稱為 累積分佈函式(cumulative distribution function)。它衡量某個像素強度範圍在影像中出現的頻率。
在這個練習中,請透過計算長條圖與累積分佈函式,並把它們一起顯示,來描述 im 的強度分佈。
本練習屬於課程
Python 的生醫影像分析
練習說明
- 將
scipy.ndimage匯入為ndi。 - 產生涵蓋
np.uint8全範圍的 256 區間長條圖,來源為im。 - 計算
im的累積分佈函式。先對hist做累積和,然後除以hist中像素總數。 - 在分開的子圖上繪製
hist與cdf。這部分已為你完成。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import SciPy's "ndimage" module
import ____ as ____
# Create a histogram, binned at each possible value
hist = ____
# Create a cumulative distribution function
cdf = hist.cumsum() / ____
# Plot the histogram and CDF
fig, axes = plt.subplots(2, 1, sharex=True)
axes[0].plot(hist, label='Histogram')
axes[1].plot(cdf, label='CDF')
format_and_render_plot()