retuve.hip_us.handlers.segs
Handles Segmentaitions that are not valid for analysis.
1# Copyright 2024 Adam McArthur 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15""" 16Handles Segmentaitions that are not valid for analysis. 17""" 18 19from typing import Dict, List 20 21from PIL import Image 22 23from retuve.classes.seg import SegObject 24from retuve.hip_us.classes.enums import HipLabelsUS 25 26 27def remove_bad_objs( 28 hip_objs: Dict[HipLabelsUS, List[SegObject]], img: Image.Image 29) -> Dict[HipLabelsUS, SegObject]: 30 """ 31 Remove bad segmentation objects from the Hip Objects. 32 33 For example, if there are multiple segmentations for the Illium and Acetabulum, 34 only keep the one that is the most left of the image. 35 36 :param hip_objs: Dict of Hip Objects. 37 :param img: Image object. 38 39 """ 40 41 rejection_reasons = [] 42 43 fem_head_ilium_wrong_way_round = False 44 if len(hip_objs[HipLabelsUS.IlliumAndAcetabulum]) > 1: 45 hip_objs[HipLabelsUS.IlliumAndAcetabulum] = [ 46 sorted( 47 hip_objs[HipLabelsUS.IlliumAndAcetabulum], 48 key=lambda seg_obj: seg_obj.box[0], 49 )[0] 50 ] 51 52 # replace each value with the largest object 53 for k, v in hip_objs.items(): 54 if len(v) != 0: 55 hip_objs[k] = max(v, key=lambda seg_obj: seg_obj.area()) 56 else: 57 hip_objs[k] = SegObject(empty=True) 58 59 fem_head = hip_objs.get(HipLabelsUS.FemoralHead, None) 60 61 illium = hip_objs.get(HipLabelsUS.IlliumAndAcetabulum, None) 62 if illium and illium.box is not None and illium.box[0] > img.shape[1] / 2: 63 # check if the femoral head box is left of the illium box 64 if ( 65 fem_head 66 and fem_head.box is not None 67 and illium.box[0] > fem_head.box[0] 68 ): 69 fem_head_ilium_wrong_way_round = True 70 71 # Femoral Heads should be at least 2.5% 72 expected_min_fem_size = img.shape[0] * img.shape[1] * 0.025 73 if fem_head and fem_head.area() < expected_min_fem_size: 74 hip_objs[HipLabelsUS.FemoralHead] = SegObject(empty=True) 75 rejection_reasons.append("Femoral Head too small") 76 77 return hip_objs, fem_head_ilium_wrong_way_round, rejection_reasons
def
remove_bad_objs( hip_objs: Dict[retuve.hip_us.classes.enums.HipLabelsUS, List[retuve.classes.seg.SegObject]], img: PIL.Image.Image) -> Dict[retuve.hip_us.classes.enums.HipLabelsUS, retuve.classes.seg.SegObject]:
28def remove_bad_objs( 29 hip_objs: Dict[HipLabelsUS, List[SegObject]], img: Image.Image 30) -> Dict[HipLabelsUS, SegObject]: 31 """ 32 Remove bad segmentation objects from the Hip Objects. 33 34 For example, if there are multiple segmentations for the Illium and Acetabulum, 35 only keep the one that is the most left of the image. 36 37 :param hip_objs: Dict of Hip Objects. 38 :param img: Image object. 39 40 """ 41 42 rejection_reasons = [] 43 44 fem_head_ilium_wrong_way_round = False 45 if len(hip_objs[HipLabelsUS.IlliumAndAcetabulum]) > 1: 46 hip_objs[HipLabelsUS.IlliumAndAcetabulum] = [ 47 sorted( 48 hip_objs[HipLabelsUS.IlliumAndAcetabulum], 49 key=lambda seg_obj: seg_obj.box[0], 50 )[0] 51 ] 52 53 # replace each value with the largest object 54 for k, v in hip_objs.items(): 55 if len(v) != 0: 56 hip_objs[k] = max(v, key=lambda seg_obj: seg_obj.area()) 57 else: 58 hip_objs[k] = SegObject(empty=True) 59 60 fem_head = hip_objs.get(HipLabelsUS.FemoralHead, None) 61 62 illium = hip_objs.get(HipLabelsUS.IlliumAndAcetabulum, None) 63 if illium and illium.box is not None and illium.box[0] > img.shape[1] / 2: 64 # check if the femoral head box is left of the illium box 65 if ( 66 fem_head 67 and fem_head.box is not None 68 and illium.box[0] > fem_head.box[0] 69 ): 70 fem_head_ilium_wrong_way_round = True 71 72 # Femoral Heads should be at least 2.5% 73 expected_min_fem_size = img.shape[0] * img.shape[1] * 0.025 74 if fem_head and fem_head.area() < expected_min_fem_size: 75 hip_objs[HipLabelsUS.FemoralHead] = SegObject(empty=True) 76 rejection_reasons.append("Femoral Head too small") 77 78 return hip_objs, fem_head_ilium_wrong_way_round, rejection_reasons
Remove bad segmentation objects from the Hip Objects.
For example, if there are multiple segmentations for the Illium and Acetabulum, only keep the one that is the most left of the image.
Parameters
- hip_objs: Dict of Hip Objects.
- img: Image object.