Commit 0759123d5d69d55174d6a970b55c49c15dc0a949

Authored by tfmoraes
1 parent 4838dfee

ENH: Improvements in mask threshold, like use of buffers, better use of numpy.arrays and some fixes.

Showing 1 changed file with 61 additions and 56 deletions   Show diff stats
invesalius/data/slice_.py
@@ -184,6 +184,7 @@ class Slice(object): @@ -184,6 +184,7 @@ class Slice(object):
184 self.CreateMask(name=mask_name, threshold_range=thresh, colour =colour) 184 self.CreateMask(name=mask_name, threshold_range=thresh, colour =colour)
185 self.SetMaskColour(self.current_mask.index, self.current_mask.colour) 185 self.SetMaskColour(self.current_mask.index, self.current_mask.colour)
186 self.SelectCurrentMask(self.current_mask.index) 186 self.SelectCurrentMask(self.current_mask.index)
  187 + ps.Publisher().sendMessage('Reload actual slice')
187 188
188 def __select_current_mask(self, pubsub_evt): 189 def __select_current_mask(self, pubsub_evt):
189 mask_index = pubsub_evt.data 190 mask_index = pubsub_evt.data
@@ -259,12 +260,15 @@ class Slice(object): @@ -259,12 +260,15 @@ class Slice(object):
259 #--------------------------------------------------------------------------- 260 #---------------------------------------------------------------------------
260 261
261 def GetSlices(self, orientation, slice_number): 262 def GetSlices(self, orientation, slice_number):
  263 + print "Getting slice", self.buffer_slices[orientation][0], slice_number
262 if self.buffer_slices[orientation][0] == slice_number: 264 if self.buffer_slices[orientation][0] == slice_number:
263 - print "From buffer"  
264 image = self.buffer_slices[orientation][1] 265 image = self.buffer_slices[orientation][1]
265 - n_mask = self.buffer_slices[orientation][2]  
266 - mask = iu.to_vtk(n_mask, self.spacing, slice_number, orientation)  
267 - final_image = self.do_blend(image, self.do_colour_mask(mask)) 266 + if self.current_mask and self.current_mask.is_shown:
  267 + n_mask = self.buffer_slices[orientation][2]
  268 + mask = iu.to_vtk(n_mask, self.spacing, slice_number, orientation)
  269 + final_image = self.do_blend(image, self.do_colour_mask(mask))
  270 + else:
  271 + final_image = image
268 else: 272 else:
269 n_image = self.GetImageSlice(orientation, slice_number) 273 n_image = self.GetImageSlice(orientation, slice_number)
270 image = iu.to_vtk(n_image, self.spacing, slice_number, orientation) 274 image = iu.to_vtk(n_image, self.spacing, slice_number, orientation)
@@ -285,39 +289,49 @@ class Slice(object): @@ -285,39 +289,49 @@ class Slice(object):
285 return final_image 289 return final_image
286 290
287 def GetImageSlice(self, orientation, slice_number): 291 def GetImageSlice(self, orientation, slice_number):
288 - if orientation == 'AXIAL':  
289 - n_image = numpy.array(self.matrix[slice_number])  
290 - elif orientation == 'CORONAL':  
291 - n_image = numpy.array(self.matrix[..., slice_number, ...])  
292 - elif orientation == 'SAGITAL':  
293 - n_image = numpy.array(self.matrix[..., ..., slice_number]) 292 + if self.buffer_slices[orientation] == slice_number:
  293 + n_image = self.buffer_slices[orientation][3]
  294 + else:
  295 + if orientation == 'AXIAL':
  296 + n_image = numpy.array(self.matrix[slice_number])
  297 + elif orientation == 'CORONAL':
  298 + n_image = numpy.array(self.matrix[..., slice_number, ...])
  299 + elif orientation == 'SAGITAL':
  300 + n_image = numpy.array(self.matrix[..., ..., slice_number])
294 return n_image 301 return n_image
295 302
296 def GetMaskSlice(self, orientation, slice_number): 303 def GetMaskSlice(self, orientation, slice_number):
297 - slice_number += 1 304 + """
  305 + It gets the from actual mask the given slice from given orientation
  306 + """
  307 +
  308 + # It's necessary because the first position for each dimension from
  309 + # mask matrix is used as flags to control if the mask in the
  310 + # slice_number position has been generated.
  311 + n = slice_number + 1
298 if orientation == 'AXIAL': 312 if orientation == 'AXIAL':
299 - if self.current_mask.matrix[slice_number, 0, 0] == 0:  
300 - self.current_mask.matrix[slice_number, 1:, 1:] = \ 313 + if self.current_mask.matrix[n, 0, 0] == 0:
  314 + self.current_mask.matrix[n, 1:, 1:] = \
301 self.do_threshold_to_a_slice(self.GetImageSlice(orientation, 315 self.do_threshold_to_a_slice(self.GetImageSlice(orientation,
302 slice_number)) 316 slice_number))
303 - self.current_mask.matrix[slice_number, 0, 0] = 1  
304 - n_mask = numpy.array(self.current_mask.matrix[slice_number]) 317 + self.current_mask.matrix[n, 0, 0] = 1
  318 + n_mask = numpy.array(self.current_mask.matrix[n, 1:, 1:])
305 319
306 elif orientation == 'CORONAL': 320 elif orientation == 'CORONAL':
307 - if self.current_mask.matrix[0, slice_number, 0] == 0:  
308 - self.current_mask.matrix[1:, slice_number, 1:] = \ 321 + if self.current_mask.matrix[0, n, 0] == 0:
  322 + self.current_mask.matrix[1:, n, 1:] = \
309 self.do_threshold_to_a_slice(self.GetImageSlice(orientation, 323 self.do_threshold_to_a_slice(self.GetImageSlice(orientation,
310 slice_number)) 324 slice_number))
311 - self.current_mask.matrix[0, slice_number, 0] = 1  
312 - n_mask = numpy.array(self.current_mask.matrix[..., slice_number, ...]) 325 + self.current_mask.matrix[0, n, 0] = 1
  326 + n_mask = numpy.array(self.current_mask.matrix[1:, n, 1:])
313 327
314 elif orientation == 'SAGITAL': 328 elif orientation == 'SAGITAL':
315 - if self.current_mask.matrix[0, 0, slice_number] == 0:  
316 - self.current_mask.matrix[1:, 1:, slice_number] = \ 329 + if self.current_mask.matrix[0, 0, n] == 0:
  330 + self.current_mask.matrix[1:, 1:, n] = \
317 self.do_threshold_to_a_slice(self.GetImageSlice(orientation, 331 self.do_threshold_to_a_slice(self.GetImageSlice(orientation,
318 slice_number)) 332 slice_number))
319 - self.current_mask.matrix[0, 0, slice_number] = 1  
320 - n_mask = numpy.array(self.current_mask.matrix[..., ..., slice_number]) 333 + self.current_mask.matrix[0, 0, n] = 1
  334 + n_mask = numpy.array(self.current_mask.matrix[1:, 1:, n])
321 return n_mask 335 return n_mask
322 336
323 def GetNumberOfSlices(self, orientation): 337 def GetNumberOfSlices(self, orientation):
@@ -401,9 +415,8 @@ class Slice(object): @@ -401,9 +415,8 @@ class Slice(object):
401 else: 415 else:
402 print "Only one slice" 416 print "Only one slice"
403 slice_ = self.buffer_slices[orientation][3] 417 slice_ = self.buffer_slices[orientation][3]
404 - m = numpy.zeros(slice_.shape, self.current_mask.matrix.dtype)  
405 - m[numpy.logical_and(slice_ >= thresh_min,slice_ <= thresh_max)] = 255  
406 - self.buffer_slices[orientation][2] = m 418 + self.buffer_slices[orientation][2][:] = 0
  419 + self.buffer_slices[orientation][2][numpy.logical_and(slice_ >= thresh_min,slice_ <= thresh_max)] = 255
407 420
408 # Update viewer 421 # Update viewer
409 #ps.Publisher().sendMessage('Update slice viewer') 422 #ps.Publisher().sendMessage('Update slice viewer')
@@ -425,12 +438,10 @@ class Slice(object): @@ -425,12 +438,10 @@ class Slice(object):
425 proj = Project() 438 proj = Project()
426 proj.mask_dict[index].is_shown = value 439 proj.mask_dict[index].is_shown = value
427 if (index == self.current_mask.index): 440 if (index == self.current_mask.index):
428 - if value:  
429 - self.blend_filter.SetOpacity(1, self.current_mask.opacity)  
430 - else:  
431 - self.blend_filter.SetOpacity(1, 0)  
432 - self.blend_filter.Update()  
433 - ps.Publisher().sendMessage('Update slice viewer') 441 + self.buffer_slices = {"AXIAL": [-1, None, None],
  442 + "CORONAL": [-1,None, None],
  443 + "SAGITAL": [-1, None, None]}
  444 + ps.Publisher().sendMessage('Reload actual slice')
434 #--------------------------------------------------------------------------- 445 #---------------------------------------------------------------------------
435 def ErasePixel(self, position): 446 def ErasePixel(self, position):
436 "Delete pixel, based on x, y and z position coordinates." 447 "Delete pixel, based on x, y and z position coordinates."
@@ -478,33 +489,27 @@ class Slice(object): @@ -478,33 +489,27 @@ class Slice(object):
478 #if index != self.current_mask.index: 489 #if index != self.current_mask.index:
479 print "SelectCurrentMask" 490 print "SelectCurrentMask"
480 print "index:", index 491 print "index:", index
481 - if self.current_mask and self.blend_filter and index > -1:  
482 - proj = Project()  
483 - future_mask = proj.GetMask(index)  
484 - future_mask.is_shown = True  
485 - self.current_mask = future_mask  
486 -  
487 - colour = future_mask.colour  
488 - #index = future_mask.index  
489 - print index  
490 - self.SetMaskColour(index, colour, update=False) 492 + proj = Project()
  493 + future_mask = proj.GetMask(index)
  494 + future_mask.is_shown = True
  495 + self.current_mask = future_mask
491 496
492 - imagedata = future_mask.imagedata  
493 - self.img_colours_mask.SetInput(imagedata) 497 + colour = future_mask.colour
  498 + #index = future_mask.index
  499 + print index
  500 + self.SetMaskColour(index, colour, update=False)
494 501
495 - if self.current_mask.is_shown:  
496 - self.blend_filter.SetOpacity(1, self.current_mask.opacity)  
497 - else:  
498 - self.blend_filter.SetOpacity(1, 0)  
499 - self.blend_filter.Update() 502 + self.buffer_slices = {"AXIAL": [-1, None, None],
  503 + "CORONAL": [-1,None, None],
  504 + "SAGITAL": [-1, None, None]}
500 505
501 - ps.Publisher().sendMessage('Set mask threshold in notebook',  
502 - (index,  
503 - self.current_mask.threshold_range))  
504 - ps.Publisher().sendMessage('Set threshold values in gradient',  
505 - self.current_mask.threshold_range)  
506 - ps.Publisher().sendMessage('Select mask name in combo', index)  
507 - ps.Publisher().sendMessage('Update slice viewer') 506 + ps.Publisher().sendMessage('Set mask threshold in notebook',
  507 + (index,
  508 + self.current_mask.threshold_range))
  509 + ps.Publisher().sendMessage('Set threshold values in gradient',
  510 + self.current_mask.threshold_range)
  511 + ps.Publisher().sendMessage('Select mask name in combo', index)
  512 + ps.Publisher().sendMessage('Update slice viewer')
508 #--------------------------------------------------------------------------- 513 #---------------------------------------------------------------------------
509 514
510 def CreateSurfaceFromIndex(self, pubsub_evt): 515 def CreateSurfaceFromIndex(self, pubsub_evt):