#HeaderInfo
#type=ROITool
#name=Threshold Segmentation
#version=0.0.1
#author=adminmblackledge
#EndHeaderInfo
import matplotlib.pyplot as pl
from matplotlib.widgets import Button, Slider
import numpy as np
class threshold_segmentation(object):
def __init__(self):
self.threshold = 0
self.viewer_controller = None
def _update(self, thresh_value):
# Update the displayed matplotlib window
cur_image = self.viewer_controller.curDCM().image
self.ax_image.cla()
self.ax_image.set_xticks([]), self.ax_image.set_yticks([]) # Remove the labels on the axes
self.ax_image.imshow(cur_image.T, cmap = 'gray', interpolation = 'None')
thresh_min = self.slider_min.val
thresh_max = self.slider_max.val
mask = np.logical_and(cur_image < thresh_max, cur_image > thresh_min)
self.ax_image.contour(mask.T, [0.5], colors = ['r'])
# Widget events
def _compute_pushed(self, evt):
# Compute ROIs for all images in the viewer controller
thresh_min = self.slider_min.val
thresh_max = self.slider_max.val
curIdx = self.viewer_controller.movieIdx # The current movie index
pix_list = self.viewer_controller.pixList(curIdx)
for i in range(len(pix_list)):
pix = pix_list[i]
mask = np.logical_and(pix.image < thresh_max, pix.image > thresh_min)
roi = osirix.ROI(itype = 'tPlain', buffer = mask, DCMPix = pix)
roi.name = "Threshold Segmentation"
roi.color = (255, 0, 255) # Purple Color
roi.opacity = 0.5
self.viewer_controller.setROI(roi, position = i, movieIdx = curIdx)
pl.close(self.fig)
stopRuntimeLoop()
def _cancel_pushed(self, evt):
pl.close(self.fig)
stopRuntimeLoop() # Required for installed pyOsiriX plugins
def _close_event(self, evt):
# Get here when user closes the window
stopRuntimeLoop() # Required for installed pyOsiriX plugins
def display(self):
# The current viewer controller
self.viewer_controller = osirix.frontmostViewer()
# Get the currently displayed image - to extract image statistics
cur_image = self.viewer_controller.curDCM().image
im_min = np.min(cur_image)
im_max = np.max(cur_image)
# Begin display of the matploblib window
self.fig = pl.figure(figsize = (5, 5))
# To display the current image
self.ax_image = self.fig.add_axes([0.15, 0.35, 0.7, 0.55])
self.ax_image.set_xticks([]), self.ax_image.set_yticks([])
# Min/max threshold sliders
ax_slider = self.fig.add_axes([0.25, 0.25, 0.6, 0.05])
self.slider_min = Slider(ax_slider, label="Min Thresh.",\
valmin=im_min, valmax=im_max, valinit=im_min, valfmt="%.2f")
self.slider_min.on_changed(self._update)
ax_slider = self.fig.add_axes([0.25, 0.15, 0.6, 0.05])
self.slider_max = Slider(ax_slider, label="Max Thresh.",\
valmin=im_min, valmax=im_max, valinit=im_max, valfmt="%.2f")
self.slider_max.on_changed(self._update)
# Compute/Cancel buttons
ax_button = self.fig.add_axes([0.2, 0.05, 0.25, 0.05])
self.button_compute = Button(ax_button, label = 'Compute')
self.button_compute.on_clicked(self._compute_pushed)
ax_button = self.fig.add_axes([0.55, 0.05, 0.25, 0.05])
self.button_cancel = Button(ax_button, label = 'Cancel')
self.button_cancel.on_clicked(self._cancel_pushed)
self.fig.canvas.mpl_connect('close_event', self._close_event)
# Update the matplotlib window display
self._update(None)
pl.show()
startRuntimeLoop() # Required for installed pyOsiriX plugins to keep the event loop going.
threshold = threshold_segmentation()
threshold.display()