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

Office 指南

辦公室工作實用教學

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

Python 使用 ITK StreamingImageFilter 串流分批處理大型影像教學與範例

介紹如何在 Python 中使用 ITK 的 StreamingImageFilter 將大型影像自動分割成小區域,以串流分批處理。


在使用 ITK 處理影像資料時,若遇到影像過大、記憶體不足時,可以使用串流(streaming)的方式將資料切割成小區域分批處理,以節省記憶體。

ITK 的 StreamingImageFilter 可以自動將資料區域(region)切割後,讓其之前的資料處理管線(pipeline)都串流方式運作,在 StreamingImageFilter 組合成完整的影像後再進行後續輸出,以下是一個簡單的範例。

import itk

# 設定分割數量
numberOfSplits = 4

# 設定影像類型
Dimension = 3
PixelType = itk.US
ImageType = itk.Image[PixelType, Dimension]

# 讀取 MHA 影像檔案
imageFilename = "input.mha"
reader = itk.ImageFileReader[ImageType].New(FileName=imageFilename)

# 建立 PipelineMonitorImageFilter 查看 Streaming 每次處理的範圍
monitorFilter = itk.PipelineMonitorImageFilter[ImageType].New()
monitorFilter.SetInput(reader.GetOutput())

# 建立 StreamingImageFilter 將資料以 Streaming 分批處理
streamingFilter = itk.StreamingImageFilter[ImageType, ImageType].New()
streamingFilter.SetInput(monitorFilter.GetOutput())
streamingFilter.SetNumberOfStreamDivisions(numberOfSplits)

# 實際執行
streamingFilter.Update()

# 輸出影像完整的 Region 資訊
print('The output LargestPossibleRegion is: ' +
      str(streamingFilter.GetOutput().GetLargestPossibleRegion()))

# 輸出 Streaming 的分批 Region 資訊
updatedRequestedRegions = monitorFilter.GetUpdatedRequestedRegions()
print("Updated RequestedRegion's:")
for r in range(len(updatedRequestedRegions)):
    print('  ' + str(updatedRequestedRegions[r]))
The output LargestPossibleRegion is: itkImageRegion3([0, 0, 0], [249, 278, 424])
Updated RequestedRegion's:
  itkImageRegion3([0, 0, 0], [249, 278, 106])
  itkImageRegion3([0, 0, 106], [249, 278, 106])
  itkImageRegion3([0, 0, 212], [249, 278, 106])
  itkImageRegion3([0, 0, 318], [249, 278, 106])

這裡的影像資料處理流程分為三個部分,首先以 ImageFileReader 讀取影像資料,接著加上一個 PipelineMonitorImageFilter 方便觀察串流分割的實際情況,最後接上 StreamingImageFilter 處理資料分割、產生串流。

除了使用 StreamingImageFilter 建立串流處理流程之外,也可以利用 ImageFileWriter 本身的串流功能,將整個處理流程串流化。

參考資料:ITKExamples、ITK Community

分類:Python 標籤:ITK

主要資訊欄

搜尋

近期文章

  • Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤
  • Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例
  • Linux 網路設定 ip 指令用法教學與範例
  • Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例
  • Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例
  • Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例
  • Linux 以 Cryptsetup 與 LUKS 加密磁碟教學與範例
  • Linux 使用 age 簡潔的加密、解密工具使用教學與範例

推薦網站

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

關注本站

  • 電子郵件
  • Facebook

公益

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

Copyright © 2021 · Office Guide