簡體   English   中英

Python中3D model的計算量

[英]Calculating volume of 3D model in Python

我有一個腎臟的 nii 文件,我上傳到 Python 中。 itk或vtk中是否有任何function來計算腎臟的體積?

我與您分享 SimpleITK 的解決方案。

您需要腎臟的蒙版圖像。 掩碼是一個二值圖像,其中器官為 1 值,否則為 0 值。 如果您還沒有這個,您可以使用 3DSlicer 來分割圖像,請參見此處: 3DSlicer Segmentation 在此之后,您將擁有一個 nucleus_mask.nii(或 nrrd,默認格式)文件。

您可以使用以下腳本來計算音量。

# script file image_volume.py
import argparse                 # argument parser
import numpy as np
import SimpleITK as sitk

def volume( mask_image ):
    # Input:
    # image = sitk.Image, mask or binary image (1 values where organ, 0 values otherwise)
    # Output:
    # vol = float, volume in mm3 
    space = mask_image.GetSpacing()         # image spacing
    voxel = np.prod(space)                  # voxel volume
    img = sitk.GetArrayFromImage(mask_image)
    vol = voxel*np.sum(img)
    return vol

def main():
    # Arguments details
    parser = argparse.ArgumentParser(description='Compute volume of a mask image')
    parser.add_argument("input", type=str, 
                        help='Input file name')

    # Parse arguments
    args = parser.parse_args()
    image_file = args.input

    # Read the image
    try:
        image = sitk.ReadImage(image_file)
    except:
        print('Unable to read input image file.')

    # Calculate the volume
    print('Volume: {:f} [mm3]'.format(volume(image)))
    print('Volume: {:f} [cm3]'.format(volume(image)/1000.0))

    return
    
if __name__ == "__main__":
    # execute only if run as a script
    main()

將腳本稱為:

python image_volume.py '/path/folder/kidney_mask.nii'

這是您可以在 SimpleITK 中執行此操作的方法(另一個答案使用 numpy 進行體素計數)

import SimpleITK as sitk

stats = sitk.StatisticsImageFilter()

# img is a SimpleITK image
stats.Execute(img)

# get the number of voxels with label 1
nvoxels = stats.GetCount(1)

spacing = img.GetSpacing()
voxvol = spacing[0]*spacing[1]*spacing[2]

volume = nvoxels * voxvol

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM