Commit f6fab930746412c08db88c49f27914f1af42cdf2

Authored by Thiago Franco de Moraes
1 parent c0352ef3

created a separated function

invesalius/data/styles.py
... ... @@ -2052,88 +2052,43 @@ class FloodFillSegmentInteractorStyle(DefaultInteractorStyle):
2052 2052 mask = self.viewer.slice_.buffer_slices[self.orientation].mask.copy()
2053 2053 image = self.viewer.slice_.buffer_slices[self.orientation].image
2054 2054  
2055   - if self.config.method == 'threshold':
2056   - v = image[y, x]
2057   - t0 = self.config.t0
2058   - t1 = self.config.t1
2059   -
2060   - elif self.config.method == 'dynamic1':
2061   - if self.config.use_ww_wl:
2062   - print "Using WW&WL"
2063   - ww = self.viewer.slice_.window_width
2064   - wl = self.viewer.slice_.window_level
2065   - image = get_LUT_value_255(image, ww, wl)
2066   -
2067   - v = image[y, x]
2068   -
2069   - t0 = v - self.config.dev_min
2070   - t1 = v + self.config.dev_max
2071   -
2072   - else:
  2055 + if self.config.method == 'confidence':
2073 2056 dy, dx = image.shape
2074 2057 image = image.reshape((1, dy, dx))
2075 2058 mask = mask.reshape((1, dy, dx))
2076   - if self.config.use_ww_wl:
2077   - print "Using WW&WL"
2078   - ww = self.viewer.slice_.window_width
2079   - wl = self.viewer.slice_.window_level
2080   - image = get_LUT_value_255(image, ww, wl)
2081   - bool_mask = np.zeros_like(mask, dtype='bool')
2082   - out_mask = np.zeros_like(mask)
2083   - for i in xrange(int(x-1), int(x+2)):
2084   - if i < 0 or i >= bool_mask.shape[2]:
2085   - continue
2086   - for j in xrange(int(y-1), int(y+2)):
2087   - if j < 0 or j >= bool_mask.shape[1]:
2088   - continue
2089   - bool_mask[0, j, i] = True
2090   -
2091   -
2092   - bstruct = np.array(generate_binary_structure(2, CON2D[self.config.con_2d]), dtype='uint8')
2093   - bstruct = bstruct.reshape((1, 3, 3))
2094   -
2095   - for i in xrange(3):
2096   - var = np.std(image[bool_mask])
2097   - mean = np.mean(image[bool_mask])
2098   -
2099   - t0 = mean - var * 2.5
2100   - t1 = mean + var * 2.5
2101   - print var, mean, t0, t1
2102   -
2103   - floodfill.floodfill_threshold(image, [[x, y, 0]], t0, t1, 1, bstruct, out_mask)
2104   -
2105   - bool_mask[out_mask == 1] = True
2106   -
2107   - mask[out_mask.astype('bool')] = self.config.fill_value
  2059 + out_mask = self.do_rg_confidence((x, y, 0), image, mask)
  2060 + else:
  2061 + if self.config.method == 'threshold':
  2062 + v = image[y, x]
  2063 + t0 = self.config.t0
  2064 + t1 = self.config.t1
2108 2065  
2109   - index = self.viewer.slice_.buffer_slices[self.orientation].index
2110   - b_mask = self.viewer.slice_.buffer_slices[self.orientation].mask
2111   - vol_mask = self.viewer.slice_.current_mask.matrix[1:, 1:, 1:]
  2066 + elif self.config.method == 'dynamic':
  2067 + if self.config.use_ww_wl:
  2068 + print "Using WW&WL"
  2069 + ww = self.viewer.slice_.window_width
  2070 + wl = self.viewer.slice_.window_level
  2071 + image = get_LUT_value_255(image, ww, wl)
2112 2072  
2113   - if self.orientation == 'AXIAL':
2114   - vol_mask[index, :, :] = mask
2115   - elif self.orientation == 'CORONAL':
2116   - vol_mask[:, index, :] = mask
2117   - elif self.orientation == 'SAGITAL':
2118   - vol_mask[:, :, index] = mask
  2073 + v = image[y, x]
2119 2074  
2120   - self.viewer.slice_.current_mask.save_history(index, self.orientation, mask, b_mask)
  2075 + t0 = v - self.config.dev_min
  2076 + t1 = v + self.config.dev_max
2121 2077  
2122   - return
2123 2078  
2124   - if image[y, x] < t0 or image[y, x] > t1:
2125   - return
  2079 + if image[y, x] < t0 or image[y, x] > t1:
  2080 + return
2126 2081  
2127   - dy, dx = image.shape
2128   - image = image.reshape((1, dy, dx))
2129   - mask = mask.reshape((1, dy, dx))
  2082 + dy, dx = image.shape
  2083 + image = image.reshape((1, dy, dx))
  2084 + mask = mask.reshape((1, dy, dx))
2130 2085  
2131   - out_mask = np.zeros_like(mask)
  2086 + out_mask = np.zeros_like(mask)
2132 2087  
2133   - bstruct = np.array(generate_binary_structure(2, CON2D[self.config.con_2d]), dtype='uint8')
2134   - bstruct = bstruct.reshape((1, 3, 3))
  2088 + bstruct = np.array(generate_binary_structure(2, CON2D[self.config.con_2d]), dtype='uint8')
  2089 + bstruct = bstruct.reshape((1, 3, 3))
2135 2090  
2136   - floodfill.floodfill_threshold(image, [[x, y, 0]], t0, t1, 1, bstruct, out_mask)
  2091 + floodfill.floodfill_threshold(image, [[x, y, 0]], t0, t1, 1, bstruct, out_mask)
2137 2092  
2138 2093 mask[out_mask.astype('bool')] = self.config.fill_value
2139 2094  
... ... @@ -2199,6 +2154,41 @@ class FloodFillSegmentInteractorStyle(DefaultInteractorStyle):
2199 2154  
2200 2155 self.viewer.slice_.current_mask.save_history(0, 'VOLUME', self.viewer.slice_.current_mask.matrix.copy(), cp_mask)
2201 2156  
  2157 + def do_rg_confidence(self, p, image, mask):
  2158 + x, y, z = p
  2159 + if self.config.use_ww_wl:
  2160 + print "Using WW&WL"
  2161 + ww = self.viewer.slice_.window_width
  2162 + wl = self.viewer.slice_.window_level
  2163 + image = get_LUT_value_255(image, ww, wl)
  2164 + bool_mask = np.zeros_like(mask, dtype='bool')
  2165 + out_mask = np.zeros_like(mask)
  2166 + for i in xrange(int(x-1), int(x+2)):
  2167 + if i < 0 or i >= bool_mask.shape[2]:
  2168 + continue
  2169 + for j in xrange(int(y-1), int(y+2)):
  2170 + if j < 0 or j >= bool_mask.shape[1]:
  2171 + continue
  2172 + bool_mask[0, j, i] = True
  2173 +
  2174 +
  2175 + bstruct = np.array(generate_binary_structure(2, CON2D[self.config.con_2d]), dtype='uint8')
  2176 + bstruct = bstruct.reshape((1, 3, 3))
  2177 +
  2178 + for i in xrange(3):
  2179 + var = np.std(image[bool_mask])
  2180 + mean = np.mean(image[bool_mask])
  2181 +
  2182 + t0 = mean - var * 2.5
  2183 + t1 = mean + var * 2.5
  2184 + print var, mean, t0, t1
  2185 +
  2186 + floodfill.floodfill_threshold(image, [[x, y, 0]], t0, t1, 1, bstruct, out_mask)
  2187 +
  2188 + bool_mask[out_mask == 1] = True
  2189 +
  2190 + return out_mask
  2191 +
2202 2192 def get_style(style):
2203 2193 STYLES = {
2204 2194 const.STATE_DEFAULT: DefaultInteractorStyle,
... ...
invesalius/gui/dialogs.py
... ... @@ -2273,13 +2273,14 @@ class FFillSegmentationOptionsDialog(wx.Dialog):
2273 2273 else:
2274 2274 self.panel3dcon.conect3D_6.SetValue(1)
2275 2275  
2276   - self.cmb_method = wx.ComboBox(self, -1, choices=(_(u"Dynamic"), _(u"Threshold")), style=wx.CB_READONLY)
  2276 + self.cmb_method = wx.ComboBox(self, -1, choices=(_(u"Dynamic"), _(u"Threshold"), _(u"Confidence")), style=wx.CB_READONLY)
2277 2277  
2278 2278 if self.config.method == 'dynamic':
2279 2279 self.cmb_method.SetSelection(0)
2280   - else:
  2280 + elif self.config.method == 'threshold':
2281 2281 self.cmb_method.SetSelection(1)
2282   - self.config.method = 'threshold'
  2282 + elif self.config.method == 'confidence':
  2283 + self.cmb_method.SetSelection(2)
2283 2284  
2284 2285 self.panel_ffill_threshold = PanelFFillThreshold(self, self.config, -1, style=border_style|wx.TAB_TRAVERSAL)
2285 2286 self.panel_ffill_threshold.SetMinSize((250, -1))
... ... @@ -2313,6 +2314,10 @@ class FFillSegmentationOptionsDialog(wx.Dialog):
2313 2314 self.cmb_method.SetSelection(0)
2314 2315 self.panel_ffill_dynamic.Show()
2315 2316 sizer.Add(self.panel_ffill_dynamic, (11, 0), (1, 6), flag=wx.LEFT|wx.RIGHT|wx.EXPAND, border=7)
  2317 + elif self.config.method == 'confidence':
  2318 + self.cmb_method.SetSelection(2)
  2319 + self.panel_ffill_dynamic.Show()
  2320 + sizer.Add(self.panel_ffill_dynamic, (11, 0), (1, 6), flag=wx.LEFT|wx.RIGHT|wx.EXPAND, border=7)
2316 2321 else:
2317 2322 self.cmb_method.SetSelection(1)
2318 2323 self.panel_ffill_threshold.Show()
... ... @@ -2363,6 +2368,11 @@ class FFillSegmentationOptionsDialog(wx.Dialog):
2363 2368 self.panel_ffill_threshold.Hide()
2364 2369 self.panel_ffill_dynamic.Show()
2365 2370 self.GetSizer().Replace(self.panel_ffill_threshold, self.panel_ffill_dynamic)
  2371 + elif self.cmb_method.GetSelection() == 2:
  2372 + self.config.method = 'confidence'
  2373 + self.panel_ffill_threshold.Hide()
  2374 + self.panel_ffill_dynamic.Show()
  2375 + self.GetSizer().Replace(self.panel_ffill_threshold, self.panel_ffill_dynamic)
2366 2376 else:
2367 2377 self.config.method = 'threshold'
2368 2378 self.panel_ffill_dynamic.Hide()
... ...