Skip to content

Commit

Permalink
added smoothing factor, which is just skipping every x coord. Also ad…
Browse files Browse the repository at this point in the history
…ded adjustments for high res structure sets
  • Loading branch information
Mathis Rasmussen committed Jan 20, 2023
1 parent 68ff65c commit 24842bf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 8 additions & 3 deletions rt_utils/image_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def get_contours_coords(roi_data: ROIData, series_data):
mask_slice = create_pin_hole_mask(mask_slice, roi_data.approximate_contours)

# Get contours from mask
contours, _ = find_mask_contours(mask_slice, roi_data.approximate_contours)
contours, _ = find_mask_contours(mask_slice,
roi_data.approximate_contours,
smoothing_factor=roi_data.smoothing_factor,
scaling_factor=roi_data.scaling_factor)
validate_contours(contours)

# Format for DICOM
Expand All @@ -82,7 +85,7 @@ def get_contours_coords(roi_data: ROIData, series_data):
return series_contours


def find_mask_contours(mask: np.ndarray, approximate_contours: bool):
def find_mask_contours(mask: np.ndarray, approximate_contours: bool, smoothing_factor=1, scaling_factor=1):
approximation_method = (
cv.CHAIN_APPROX_SIMPLE if approximate_contours else cv.CHAIN_APPROX_NONE
)
Expand All @@ -93,8 +96,10 @@ def find_mask_contours(mask: np.ndarray, approximate_contours: bool):
contours = list(
contours
) # Open-CV updated contours to be a tuple so we convert it back into a list here

assert smoothing_factor > 0
for i, contour in enumerate(contours):
contours[i] = [[pos[0][0], pos[0][1]] for pos in contour]
contours[i] = [[(contour[i][0][0]/scaling_factor), (contour[i][0][1]/scaling_factor)] for i in range(0, len(contour), smoothing_factor)]
hierarchy = hierarchy[0] # Format extra array out of data

return contours, hierarchy
Expand Down
7 changes: 7 additions & 0 deletions rt_utils/rtstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def add_roi(
use_pin_hole: bool = False,
approximate_contours: bool = True,
roi_generation_algorithm: Union[str, int] = 0,
smoothing_factor: int = 1,
):
"""
Add a ROI to the rtstruct given a 3D binary mask for the ROI's at each slice
Expand All @@ -43,6 +44,10 @@ def add_roi(
If approximate_contours is set to False, no approximation will be done when generating contour data, leading to much larger amount of contour data
"""

## If upscaled coords are given, they should be adjusted accordingly
rows = self.series_data[0][0x00280010].value
scaling_factor = int(mask.shape[0] / rows)

# TODO test if name already exists
self.validate_mask(mask)
roi_number = len(self.ds.StructureSetROISequence) + 1
Expand All @@ -56,6 +61,8 @@ def add_roi(
use_pin_hole,
approximate_contours,
roi_generation_algorithm,
smoothing_factor,
scaling_factor,
)

self.ds.ROIContourSequence.append(
Expand Down
6 changes: 5 additions & 1 deletion rt_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class SOPClassUID:
@dataclass
class ROIData:
"""Data class to easily pass ROI data to helper methods."""

mask: str
color: Union[str, List[int]]
number: int
Expand All @@ -51,6 +50,11 @@ class ROIData:
use_pin_hole: bool = False
approximate_contours: bool = True
roi_generation_algorithm: Union[str, int] = 0
smoothing_factor: int = 1
scaling_factor: int = 1




def __post_init__(self):
self.validate_color()
Expand Down

0 comments on commit 24842bf

Please sign in to comment.