• 跳至主要導覽
  • 跳至主要內容
  • 跳至主要資訊欄
Office 指南

Office 指南

辦公室工作實用教學

  • Excel
  • Word
  • PowerPoint
  • Windows
  • PowerShell
  • R

Python SimpleITK 影像處理教學:Allen Mouse Brain Atlas 老鼠標準腦

介紹如何在 Python 以 SimpleITK 模組讀取 Allen Mouse Brain Atlas 老鼠標準腦、繪製切面圖,並計算各種基本統計量。

讀取標準腦影像

老鼠的標準腦 volume 影像可從 Allen Mouse Brain Atlas 的網站 下載,解壓縮之後即可使用 Python 的 SimpleITK 模組來讀取:

import SimpleITK as sitk

# 讀取 Allen Mouse Brain Atlas 影像
image = sitk.ReadImage("atlasVolume/atlasVolume.mhd")

# 影像大小
print("Image Size:", image.GetSize())

# 像素類型
print("Pixel Type:", image.GetPixelIDTypeAsString())

# Voxel Size (um)
print("Voxel Size:", image.GetSpacing())
Image Size: (528, 320, 456)
Pixel Type: 8-bit unsigned integer
Voxel Size: (25.0, 25.0, 25.0)

顯示影像切面:

import matplotlib.pyplot as plt

# 轉為 NumPy 陣列
nda = sitk.GetArrayViewFromImage(image)

# 顯示影像切面
z_index = 228
slice = nda[z_index,:,:]
plt.imshow(slice)
plt.show()
Allen Mouse Brain Atlas 影像
Allen Mouse Brain Atlas 影像

以 seaborn 模組繪製像素值分佈圖(要先安裝 statsmodels 模組):

# 像素值分佈
import seaborn as sns
sns.set()
sns.distplot(nda.flatten())
plt.show()
像素值分佈
像素值分佈

計算像素值的各種基本統計量:

import numpy as np

print("Min:", nda.min())                 # 像素值最小值
print("Max:", nda.max())                 # 像素值最大值
print("Mean:", nda.mean())               # 像素值平均值
print("Variance:", nda.var() )           # 像素值變異數
print("Standard Deviation:", nda.std())  # 像素值標準差
print("Median:", np.median(nda))         # 像素值中位數
Min: 0
Max: 255
Mean: 23.121027711323762
Variance: 1350.532555098347
Standard Deviation: 36.749592584113735
Median: 0.0

排除零值

排除像素值為 0 的資料,重新繪製像素值分佈:

# 排除 0 值
ndaNonZero = np.extract(nda > 0, nda)

# 像素值分佈(排除 0 值)
sns.distplot(ndaNonZero.flatten())
plt.show()
像素值分佈(排除 0 值)
像素值分佈(排除 0 值)

排除像素值為 0 的資料後,重新計算各種基本統計量:

print("Min:", ndaNonZero.min())                 # 像素值最小值
print("Max:", ndaNonZero.max())                 # 像素值最大值
print("Mean:", ndaNonZero.mean())               # 像素值平均值
print("Variance:", ndaNonZero.var())            # 像素值變異數
print("Standard Deviation:", ndaNonZero.std())  # 像素值標準差
print("Median:", np.median(ndaNonZero))         # 像素值中位數
Min: 1
Max: 255
Mean: 48.98333937406607
Variance: 1594.3650678915421
Standard Deviation: 39.92950122267422
Median: 41.0

分類:Python 標籤:ITK

讀者互動方式

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

主要資訊欄

搜尋

近期文章

  • C++ 語言使用 Crypto++ 實作 RSA 數位簽章教學與範例
  • C++ 語言使用 Crypto++ 實作 RSA-OAEP 搭配 SHA256 加密教學與範例
  • C++ 語言使用 Crypto++ 實作 AES 加密、解密、認證加密教學與範例
  • C++ 語言使用 Crypto++ 實作 MD5、SHA1、SHA2、BLAKE2 雜湊教學與範例
  • Ubuntu Linux 安裝、使用 Crypto++ 加密函式庫教學與範例
  • C 語言使用 OpenSSL 實作橢圓曲線 ECDH 金鑰交換教學與範例
  • Python 以 eciespy 實作 ECC 非對稱式加密方法教學與範例
  • C 語言使用 OpenSSL 實作 PBKDF2 教學與範例

推薦網站

  • Udemy 線上教學課程
  • Coursera 線上教學課程

關注本站

  • 電子郵件
  • Facebook

公益

  • 家扶基金會
  • 台灣世界展望會
  • Yahoo 奇摩公益
  • igiving 公益網
  • 兒福聯盟

Copyright © 2021 · Office Guide