一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
 1 sitk_ seg_ img = sitk . ReadImage( m )
 2 bbox =get_ bbox_ from mask(sitk_ seg_ _img)
 3 
 4 def get_bbox_from_mask(bin_mask):
 5     # One is : sitk.LabelShapeStatisticsImageFilter()
 6     #          Input: bin_mask
 7     #          Output: bbox<x,y,z, length, width, height>
 8     #                  here, x y z --> (R, A, S) when direction=(1,1,1)
 9     #          Note: for GetArrayFromImage , it is <z, y, x>
10     # Front idx = 1, background idx = 0
11     label_filter = sitk.LabelShapeStatisticsImageFilter()
12     label_filter.Execute(bin_mask)
13     bbox = label_filter.GetBoundingBox(1)
14     return bbox
15 
16 
17 def get_bbox_from_volme_and_mask(sitk_seg_img):
18     # Another is : sitk.LabelStatisticsImageFilter()
19     #          Input: <raw_img ,bin_mask>
20     #          Output: bbox = <x_min, x_max, y_min, y_max, z_min, z_max>
21     #          Note: for GetArrayFromImage , it is <z, y, x>
22     # Front idx = 1, background idx = 0
23     label_filter = sitk.LabelStatisticsImageFilter()
24     label_filter.Execute(sitk_seg_img, sitk_seg_img)
25     bbox = label_filter.GetBoundingBox(1)
26     return bbox
 1  def get_bbox_from_mask(self, bin_mask):
 2     # One is : sitk.LabelShapeStatisticsImageFilter()
 3     #          Input: bin_mask
 4     #          Output: bbox<x,y,z, length, width, height>
 5     #                  here, x y z --> (R, A, S) when direction=(1,1,1)
 6     #          Note: for GetArrayFromImage , it is <z, y, x>
 7     # Front idx = 1, background idx = 0
 8     label_filter = sitk.LabelShapeStatisticsImageFilter()
 9     label_filter.Execute(bin_mask)
10     bbox = label_filter.GetBoundingBox(1)
11     return bbox
12 
13   def landmarkCanal(self):
14     canalBoneNode = slicer.util.getFirstNodeByClassByName("vtkMRMLLabelMapVolumeNode", self._canal_seg)
15     segbounds = np.zeros([6])
16     canalBoneNode.GetBounds(segbounds)
17     print("segbounds \n", segbounds)
18     itkImagelabel = sitkUtils.PullVolumeFromSlicer(canalBoneNode)
19     itkImagelabel = sitk.Cast(itkImagelabel, sitk.sitkUInt8)
20     bbox = self.get_bbox_from_mask(itkImagelabel)
21     print("bbox: \n", bbox)

 

posted on 2023-07-13 14:54  一杯清酒邀明月  阅读(68)  评论(0编辑  收藏  举报