-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvol-calculation.py
More file actions
75 lines (60 loc) · 1.63 KB
/
Copy pathvol-calculation.py
File metadata and controls
75 lines (60 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import itk
import vtk
import numpy as np
import sys
import argparse
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk
import tkinter
import tkinter.filedialog
import os
import matplotlib.pyplot as plt
import SimpleITK as sitk
import csv
def readNIFTI(filename):
"""
Read NIFTI object
"""
reader = vtk.vtkNIFTIImageReader()
reader.SetFileName(filename)
reader.Update()
return reader
def vtkToNpArray(imageData):
"""
Convert vtk to np
"""
dimensions = imageData.GetDimensions()
imageArray = vtk_to_numpy(imageData.GetPointData().GetScalars()).reshape(imageData.GetDimensions(),order='F')
return imageArray
def calculateVolume(reader):
imageArray = vtkToNpArray(reader.GetOutput())
vol = (imageArray == 1).sum()
vol = vol*0.4863*0.4863*4
return vol
def writeCSV(names,volumes):
with open('volumesHEI.csv', 'w', newline='') as csvfile:
fieldnames = ['Data', 'Volume']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for i in range(0,48):
writer.writerow({'Data': names[i], 'Volume': volumes[i]})
def main():
rootDir = "D:/Data_Scans/TestAHE/InvertedN/output/predictions/testSessionDm/"
inpDir= rootDir+"predictions"
names=[]
volumes=[]
print("Processing >>>")
for image in os.listdir(inpDir):
filename = inpDir+"/"+image
print(image)
index=image.rindex(".nii")
name=image[:index]
names.append(name)
#read
reader1 = readNIFTI(filename)
#calculate
vol = calculateVolume(reader1)
volumes.append(vol)
writeCSV(names,volumes)
print(vol)
if __name__ == '__main__':
main()