diff --git a/cerr/dataclasses/structure.py b/cerr/dataclasses/structure.py index 291647e..c851018 100644 --- a/cerr/dataclasses/structure.py +++ b/cerr/dataclasses/structure.py @@ -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(): @@ -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. diff --git a/cerr/viewer.py b/cerr/viewer.py index 0fba15a..d7397fe 100644 --- a/cerr/viewer.py +++ b/cerr/viewer.py @@ -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, @@ -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 = []