Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dicom convert #85

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/gt_image_convert
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def gt_image_convert(input, output, pixeltype, flip, accessionnumber, **kwargs):
inputImages.append(itk.imread(input[0]))
else:
series = gt.separate_series(input)
series = gt.separate_sequenceName_series(series)
if accessionnumber:
series = gt.separate_accessionNumber_series(series)
for serie in series.keys():
Expand Down
25 changes: 23 additions & 2 deletions gatetools/image_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def read_dicom_properties(self, slice, nextSlice=None):
ip = slice[0x20, 0x32].value #Image Position
elif Tag(0x54, 0x22) in slice and len(slice[0x54, 0x22].value) >0 and Tag(0x20, 0x32) in slice[0x54, 0x22][0]:
ip = slice[0x54, 0x22][0][0x20, 0x32].value #Image Position
if ip == None:
ip = [0.0, 0.0, 0.0]
self.origin = [ip[0], ip[1], ip[2]]
if Tag(0x20, 0x37) in slice:
self.io = slice[0x20, 0x37].value #Image Orientation
Expand Down Expand Up @@ -134,6 +136,26 @@ def separate_accessionNumber_series(series):
files[new_key].append(file)
return files

def separate_sequenceName_series(series):
"""
Read dicom and return a dictionary with the different series separated
"""
files = {}
#Load dicom files
for serie in series.keys():
for file in series[serie]:
try:
sequenceName = pydicom.read_file(file)[0x0018, 0x0024].value
except pydicom.errors.InvalidDicomError:
ds = pydicom.read_file(file, force=True)
ds.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
sequenceName = ds[0x0018, 0x0024].value
new_key = str(serie) + "_" + str(sequenceName)
if new_key not in files:
files[new_key] = []
files[new_key].append(file)
return files

def read_dicom(dicomFiles):
"""

Expand Down Expand Up @@ -187,8 +209,7 @@ def read_dicom(dicomFiles):
dicomProperties.read_dicom_properties(slices[0])

# create 3D array
dicomProperties.img_shape[0] = len(slices)
dicomProperties.img_shape.append(slices[0].pixel_array.shape[0])
dicomProperties.img_shape = [len(slices)] + dicomProperties.img_shape
img3d = np.float32(np.zeros(dicomProperties.img_shape))

# fill 3D array with the images from the files
Expand Down
29 changes: 15 additions & 14 deletions gatetools/roi_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,20 +363,21 @@ def __init__(self,ds=None,roi_id=None,verbose=False, contours_list = None):
for contour in roi.ContourSequence:
ref = contour.ContourImageSequence[0].ReferencedSOPInstanceUID
npoints = int(contour.NumberOfContourPoints)
# check assumption on number of contour coordinates
assert(len(contour.ContourData)==3*npoints)
points = np.array([float(coord) for coord in contour.ContourData]).reshape(npoints,3)
zvalues = set(points[:,2])
# check assumption that all points are in the same xy plane (constant z)
assert(len(zvalues)==1)
zvalue = zvalues.pop()
if zvalue in self.zlist:
ic = self.zlist.index(zvalue)
self.contour_layers[ic].add_contour(points,ref)
else:
self.contour_layers.append(contour_layer(points,ref))
self.zlist.append(zvalue)
self.bb.should_contain_all(points)
if npoints > 2:
# check assumption on number of contour coordinates
assert(len(contour.ContourData)==3*npoints)
points = np.array([float(coord) for coord in contour.ContourData]).reshape(npoints,3)
zvalues = set(points[:,2])
# check assumption that all points are in the same xy plane (constant z)
assert(len(zvalues)==1)
zvalue = zvalues.pop()
if zvalue in self.zlist:
ic = self.zlist.index(zvalue)
self.contour_layers[ic].add_contour(points,ref)
else:
self.contour_layers.append(contour_layer(points,ref))
self.zlist.append(zvalue)
self.bb.should_contain_all(points)
if verbose:
logger.info("roi {}={} has {} points on {} contours with z range [{},{}]".format(
self.roinr,self.roiname,self.npoints_total,self.ncontours,self.bb.zmin,self.bb.zmax))
Expand Down
Loading