Skip to content

Commit

Permalink
routine for bsplines smooting of structure contours
Browse files Browse the repository at this point in the history
  • Loading branch information
adityaapte committed Aug 20, 2024
1 parent 86c6bda commit 5b6bc3b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
80 changes: 80 additions & 0 deletions cerr/dataclasses/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from cerr.radiomics.preprocess import imgResample3D
import cerr.utils.mask as maskUtils
import warnings
from scipy.interpolate import splprep, splev


def get_empty_list():
Expand Down Expand Up @@ -1127,6 +1128,85 @@ def getGaussianBlurredMask(structNum, sigmaVoxel, planC, saveFlag=False,\
return blurredMask3M, planC


def getBsplineSmoothing(structNum, smootingFactor, planC, \
replaceFlag=None, procSructName=None):
"""
Args:
structNum:
smootingFactor:
planC:
saveFlag:
replaceFlag:
procSructName:
Returns:
"""

assocScanNum = scn.getScanNumFromUID(planC.structure[structNum].assocScanUID, planC)
numSlcs = len(planC.structure[structNum].contour)

if replaceFlag is None or replaceFlag == False:
struct_meta = Structure()
struct_meta.structureColor = getColorForStructNum(structNum)
struct_meta.strUID = uid.createUID("structure")
struct_meta.structSetSopInstanceUID = generate_uid()
struct_meta.assocScanUID = planC.structure[structNum].assocScanUID
struct_meta.structureFileFormat = planC.structure[structNum].structureFileFormat
struct_meta.structureName = planC.structure[structNum].structureName
struct_meta.dateWritten = planC.structure[structNum].dateWritten
struct_meta.roiNumber = planC.structure[structNum].roiNumber

contour_list = [[] for slc in range(numSlcs)]

for slc in range(numSlcs):
if planC.structure[structNum].contour[slc]:
sopClassUID = planC.scan[assocScanNum].scanInfo[slc].sopClassUID
sopInstanceUID = planC.scan[assocScanNum].scanInfo[slc].sopInstanceUID
segments_list = []
if replaceFlag is None or replaceFlag == False:
contObj = Contour()
else:
contObj = planC.structure[structNum].contour[slc]
for seg in planC.structure[structNum].contour[slc].segments:
seg.points[:,0], seg.points[:,1]
numPts = len(seg.points[:,0])
if numPts > 3:
tck, _ = splprep([seg.points[:,0], seg.points[:,1]], s = smootingFactor, per = True)
smoothX, smoothY = splev(np.linspace(0, 1, numPts*2), tck, der = 0)
zVals = np.full(len(smoothX), seg.points[0,2])
ptsM = np.asarray((smoothX, smoothY, zVals)).T
else:
ptsM = seg.points

if replaceFlag is None or replaceFlag == False:
segment = Segment()
segment.points = ptsM
else:
segment = seg
segment.points = ptsM
segments_list.append(segment)

contObj.segments = segments_list
contObj.referencedSopInstanceUID = sopInstanceUID
contObj.referencedSopClassUID = sopClassUID
contour_list[slc] = contObj


if replaceFlag is None or replaceFlag == False:
struct_meta.contour = contour_list
planC.structure.append(struct_meta)
structNum = len(planC.structure) - 1
else:
planC.structure[structNum].contour = contour_list

planC.structure[structNum].rasterSegments = rs.generateRastersegs(planC.structure[structNum], planC)

return planC



def getLabelMap(strNumV, planC, labelDict=None, dim=3):
"""
Function to create label map for user-specified structures.
Expand Down
9 changes: 5 additions & 4 deletions cerr/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,18 +589,19 @@ def showNapari(planC, scan_nums=0, struct_nums=[], dose_nums=[], vectors_dict={}
colormap=cmap)
struct_layer.append(labl)
elif displayMode.lower() == '2d':
# polygons = getContourPolygons(str_num, scan_num, planC)
cmap = DirectLabelColormap(color_dict={None: None, int(1): colr, int(0): np.array([0,0,0,0])})
#polygons = getContourPolygons(str_num, scan_num, planC)
# polygons =cerrStr.getContourPolygons(str_num, planC, rcsFlag=True)
#
# shp = viewer.add_shapes(polygons, shape_type='polygon', edge_width=2,
# edge_color=colr, face_color=[0]*4,
# affine=scan_affine, name=str_name)

# show as labels
mask3M = rs.getStrMask(str_num,planC)
isocenter = cerrStr.calcIsocenter(str_num, planC)
mask3M[mask3M] = 1 #int(str_num + 1)
# From napari 0.4.19 onwards
# from napari.utils import DirectLabelColormap
cmap = DirectLabelColormap(color_dict={None: None, int(1): colr, int(0): np.array([0,0,0,0])})
shp = viewer.add_labels(mask3M, name=str_name, affine=scan_affine,
blending='translucent',
colormap = cmap,
Expand All @@ -609,8 +610,8 @@ def showNapari(planC, scan_nums=0, struct_nums=[], dose_nums=[], vectors_dict={}
'structNum': str_num,
'assocScanNum': scan_num,
'isocenter': isocenter})
# shp.colormap = cmap
shp.contour = 2

struct_layer.append(shp)

dvf_layer = []
Expand Down

0 comments on commit 5b6bc3b

Please sign in to comment.