#HeaderInfo
#type=ROITool
#name=Inflate ROIs
#version=0.0.1
#author=adminmblackledge
#EndHeaderInfo
import matplotlib.pyplot as pl
from matplotlib.widgets import Button, Slider
import numpy as np
class roi_inflater(object):
def _inflate_roi(self, roi, inflation):
# Inflate the input roi by a specified amount
# Params:
# =======
# roi: ROI
# The roi to inflate
# inflation: float
# A positive value
if not roi.type == 'tPencil': # Currently only works for this ROI type (free draw)
return 0
else:
points = roi.points
com = np.mean(points, axis = 0)
diff = points - com
new_diff = diff * inflation
new_points = new_diff + com
roi.points = new_points
def _inflate_selected_rois(self, inflation):
# Inflate the selected rois in the current browser by a specified amount
# Params:
# =======
# inflation: float
# A positive value
vc = osirix.frontmostViewer()
rois = vc.selectedROIs()
for roi in rois:
self._inflate_roi(roi, inflation)
vc.needsDisplayUpdate()
# Widget events
def _inflate_pushed(self, evt):
self._inflate_selected_rois(self.slider.val)
def _close_event(self, evt):
#stopRuntimeLoop() # Uncomment for installed pyOsiriX plugins
pass
def display(self):
# Begin display of the matploblib window
self.fig = pl.figure(figsize = (4, 2))
ax_slider = self.fig.add_axes([0.2, 0.05, 0.6, 0.15])
self.slider = Slider(ax_slider, label="Inflation", valmin=0.001, valmax=2, valinit=1, valfmt="%.2f")
ax_button = self.fig.add_axes([0.2, 0.3, 0.6, 0.4])
self.button = Button(ax_button, label = 'Inflate')
self.button.on_clicked(self._inflate_pushed)
self.fig.canvas.mpl_connect('close_event', self._close_event)
ax_text = self.fig.add_axes([0.2, 0.85, 0.6, 0.1])
ax_text.axis('off')
ax_text.text(0.5, 0.0, "Inflate selected ROIs...", verticalalignment = 'bottom', horizontalalignment = 'center')
pl.show()
#startRuntimeLoop() # Uncomment for installed pyOsiriX plugins to keep the event loop going.
roi = roi_inflater()
roi.display()