|
a |
|
b/utils/preprocessing/FiducialsToBoundingBox.py |
|
|
1 |
import csv |
|
|
2 |
import re |
|
|
3 |
import numpy as np |
|
|
4 |
import SimpleITK as sitk |
|
|
5 |
|
|
|
6 |
def extract_coordinates(file): |
|
|
7 |
|
|
|
8 |
csvfile = open(file,'r') |
|
|
9 |
reader_iter = csv.reader(csvfile,delimiter=',') |
|
|
10 |
|
|
|
11 |
fiducials_coord = list() |
|
|
12 |
|
|
|
13 |
for row in reader_iter: |
|
|
14 |
#Find row whose first column is a Fiducial MRML node |
|
|
15 |
match = re.search('vtkMRMLMarkupsFiducialNode.*',row[0]) |
|
|
16 |
if match == None: |
|
|
17 |
pass |
|
|
18 |
else: |
|
|
19 |
if match.end() == len(row[0]): |
|
|
20 |
#Extract x,y,z and package them in a list |
|
|
21 |
fiducials_coord.append([row[1],row[2],row[3]]) |
|
|
22 |
|
|
|
23 |
#print ("Extracted Fiducial Coordinages "+str(fiducials_coord)) |
|
|
24 |
|
|
|
25 |
return fiducials_coord |
|
|
26 |
|
|
|
27 |
def extract_indices_annotation(coordinates,image): |
|
|
28 |
|
|
|
29 |
annotation_index = list() |
|
|
30 |
|
|
|
31 |
img = sitk.ReadImage(image) |
|
|
32 |
cor_arr = np.asarray(coordinates) |
|
|
33 |
cor_arr = cor_arr.astype(np.float) |
|
|
34 |
|
|
|
35 |
for point in cor_arr: |
|
|
36 |
#multiple by -1 x and y to convert RAS (Slicer) to LPS (nifti format) |
|
|
37 |
point[0] = point[0]*-1 |
|
|
38 |
point[1] = point[1]*-1 |
|
|
39 |
annotation_index.append(img.TransformPhysicalPointToIndex(point)) |
|
|
40 |
|
|
|
41 |
return annotation_index |