From f6fab930746412c08db88c49f27914f1af42cdf2 Mon Sep 17 00:00:00 2001 From: Thiago Franco de Moraes Date: Fri, 30 Sep 2016 16:19:54 -0300 Subject: [PATCH] created a separated function --- invesalius/data/styles.py | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------- invesalius/gui/dialogs.py | 16 +++++++++++++--- 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/invesalius/data/styles.py b/invesalius/data/styles.py index 47d8b77..67339db 100644 --- a/invesalius/data/styles.py +++ b/invesalius/data/styles.py @@ -2052,88 +2052,43 @@ class FloodFillSegmentInteractorStyle(DefaultInteractorStyle): mask = self.viewer.slice_.buffer_slices[self.orientation].mask.copy() image = self.viewer.slice_.buffer_slices[self.orientation].image - if self.config.method == 'threshold': - v = image[y, x] - t0 = self.config.t0 - t1 = self.config.t1 - - elif self.config.method == 'dynamic1': - if self.config.use_ww_wl: - print "Using WW&WL" - ww = self.viewer.slice_.window_width - wl = self.viewer.slice_.window_level - image = get_LUT_value_255(image, ww, wl) - - v = image[y, x] - - t0 = v - self.config.dev_min - t1 = v + self.config.dev_max - - else: + if self.config.method == 'confidence': dy, dx = image.shape image = image.reshape((1, dy, dx)) mask = mask.reshape((1, dy, dx)) - if self.config.use_ww_wl: - print "Using WW&WL" - ww = self.viewer.slice_.window_width - wl = self.viewer.slice_.window_level - image = get_LUT_value_255(image, ww, wl) - bool_mask = np.zeros_like(mask, dtype='bool') - out_mask = np.zeros_like(mask) - for i in xrange(int(x-1), int(x+2)): - if i < 0 or i >= bool_mask.shape[2]: - continue - for j in xrange(int(y-1), int(y+2)): - if j < 0 or j >= bool_mask.shape[1]: - continue - bool_mask[0, j, i] = True - - - bstruct = np.array(generate_binary_structure(2, CON2D[self.config.con_2d]), dtype='uint8') - bstruct = bstruct.reshape((1, 3, 3)) - - for i in xrange(3): - var = np.std(image[bool_mask]) - mean = np.mean(image[bool_mask]) - - t0 = mean - var * 2.5 - t1 = mean + var * 2.5 - print var, mean, t0, t1 - - floodfill.floodfill_threshold(image, [[x, y, 0]], t0, t1, 1, bstruct, out_mask) - - bool_mask[out_mask == 1] = True - - mask[out_mask.astype('bool')] = self.config.fill_value + out_mask = self.do_rg_confidence((x, y, 0), image, mask) + else: + if self.config.method == 'threshold': + v = image[y, x] + t0 = self.config.t0 + t1 = self.config.t1 - index = self.viewer.slice_.buffer_slices[self.orientation].index - b_mask = self.viewer.slice_.buffer_slices[self.orientation].mask - vol_mask = self.viewer.slice_.current_mask.matrix[1:, 1:, 1:] + elif self.config.method == 'dynamic': + if self.config.use_ww_wl: + print "Using WW&WL" + ww = self.viewer.slice_.window_width + wl = self.viewer.slice_.window_level + image = get_LUT_value_255(image, ww, wl) - if self.orientation == 'AXIAL': - vol_mask[index, :, :] = mask - elif self.orientation == 'CORONAL': - vol_mask[:, index, :] = mask - elif self.orientation == 'SAGITAL': - vol_mask[:, :, index] = mask + v = image[y, x] - self.viewer.slice_.current_mask.save_history(index, self.orientation, mask, b_mask) + t0 = v - self.config.dev_min + t1 = v + self.config.dev_max - return - if image[y, x] < t0 or image[y, x] > t1: - return + if image[y, x] < t0 or image[y, x] > t1: + return - dy, dx = image.shape - image = image.reshape((1, dy, dx)) - mask = mask.reshape((1, dy, dx)) + dy, dx = image.shape + image = image.reshape((1, dy, dx)) + mask = mask.reshape((1, dy, dx)) - out_mask = np.zeros_like(mask) + out_mask = np.zeros_like(mask) - bstruct = np.array(generate_binary_structure(2, CON2D[self.config.con_2d]), dtype='uint8') - bstruct = bstruct.reshape((1, 3, 3)) + bstruct = np.array(generate_binary_structure(2, CON2D[self.config.con_2d]), dtype='uint8') + bstruct = bstruct.reshape((1, 3, 3)) - floodfill.floodfill_threshold(image, [[x, y, 0]], t0, t1, 1, bstruct, out_mask) + floodfill.floodfill_threshold(image, [[x, y, 0]], t0, t1, 1, bstruct, out_mask) mask[out_mask.astype('bool')] = self.config.fill_value @@ -2199,6 +2154,41 @@ class FloodFillSegmentInteractorStyle(DefaultInteractorStyle): self.viewer.slice_.current_mask.save_history(0, 'VOLUME', self.viewer.slice_.current_mask.matrix.copy(), cp_mask) + def do_rg_confidence(self, p, image, mask): + x, y, z = p + if self.config.use_ww_wl: + print "Using WW&WL" + ww = self.viewer.slice_.window_width + wl = self.viewer.slice_.window_level + image = get_LUT_value_255(image, ww, wl) + bool_mask = np.zeros_like(mask, dtype='bool') + out_mask = np.zeros_like(mask) + for i in xrange(int(x-1), int(x+2)): + if i < 0 or i >= bool_mask.shape[2]: + continue + for j in xrange(int(y-1), int(y+2)): + if j < 0 or j >= bool_mask.shape[1]: + continue + bool_mask[0, j, i] = True + + + bstruct = np.array(generate_binary_structure(2, CON2D[self.config.con_2d]), dtype='uint8') + bstruct = bstruct.reshape((1, 3, 3)) + + for i in xrange(3): + var = np.std(image[bool_mask]) + mean = np.mean(image[bool_mask]) + + t0 = mean - var * 2.5 + t1 = mean + var * 2.5 + print var, mean, t0, t1 + + floodfill.floodfill_threshold(image, [[x, y, 0]], t0, t1, 1, bstruct, out_mask) + + bool_mask[out_mask == 1] = True + + return out_mask + def get_style(style): STYLES = { const.STATE_DEFAULT: DefaultInteractorStyle, diff --git a/invesalius/gui/dialogs.py b/invesalius/gui/dialogs.py index bc4db77..c29c6d8 100644 --- a/invesalius/gui/dialogs.py +++ b/invesalius/gui/dialogs.py @@ -2273,13 +2273,14 @@ class FFillSegmentationOptionsDialog(wx.Dialog): else: self.panel3dcon.conect3D_6.SetValue(1) - self.cmb_method = wx.ComboBox(self, -1, choices=(_(u"Dynamic"), _(u"Threshold")), style=wx.CB_READONLY) + self.cmb_method = wx.ComboBox(self, -1, choices=(_(u"Dynamic"), _(u"Threshold"), _(u"Confidence")), style=wx.CB_READONLY) if self.config.method == 'dynamic': self.cmb_method.SetSelection(0) - else: + elif self.config.method == 'threshold': self.cmb_method.SetSelection(1) - self.config.method = 'threshold' + elif self.config.method == 'confidence': + self.cmb_method.SetSelection(2) self.panel_ffill_threshold = PanelFFillThreshold(self, self.config, -1, style=border_style|wx.TAB_TRAVERSAL) self.panel_ffill_threshold.SetMinSize((250, -1)) @@ -2313,6 +2314,10 @@ class FFillSegmentationOptionsDialog(wx.Dialog): self.cmb_method.SetSelection(0) self.panel_ffill_dynamic.Show() sizer.Add(self.panel_ffill_dynamic, (11, 0), (1, 6), flag=wx.LEFT|wx.RIGHT|wx.EXPAND, border=7) + elif self.config.method == 'confidence': + self.cmb_method.SetSelection(2) + self.panel_ffill_dynamic.Show() + sizer.Add(self.panel_ffill_dynamic, (11, 0), (1, 6), flag=wx.LEFT|wx.RIGHT|wx.EXPAND, border=7) else: self.cmb_method.SetSelection(1) self.panel_ffill_threshold.Show() @@ -2363,6 +2368,11 @@ class FFillSegmentationOptionsDialog(wx.Dialog): self.panel_ffill_threshold.Hide() self.panel_ffill_dynamic.Show() self.GetSizer().Replace(self.panel_ffill_threshold, self.panel_ffill_dynamic) + elif self.cmb_method.GetSelection() == 2: + self.config.method = 'confidence' + self.panel_ffill_threshold.Hide() + self.panel_ffill_dynamic.Show() + self.GetSizer().Replace(self.panel_ffill_threshold, self.panel_ffill_dynamic) else: self.config.method = 'threshold' self.panel_ffill_dynamic.Hide() -- libgit2 0.21.2