mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-17 11:32:45 +08:00
Merge pull request #810 from jni/viewer-returns-overlays
WIP: Viewer returns overlays
This commit is contained in:
@@ -235,3 +235,23 @@ class Plugin(QtGui.QDialog):
|
||||
"""Remove artists that are connected to the image viewer."""
|
||||
for a in self.artists:
|
||||
a.remove()
|
||||
|
||||
def output(self):
|
||||
"""Return the plugin's representation and data.
|
||||
|
||||
Returns
|
||||
-------
|
||||
image : array, same shape as ``self.image_viewer.image``, or None
|
||||
The filtered image.
|
||||
data : None
|
||||
Any data associated with the plugin.
|
||||
|
||||
Notes
|
||||
-----
|
||||
Derived classes should override this method to return a tuple
|
||||
containing an *overlay* of the same shape of the image, and a
|
||||
*data* object. Either of these is optional: return ``None`` if
|
||||
you don't want to return a value.
|
||||
"""
|
||||
return (self.image_viewer.image, None)
|
||||
|
||||
|
||||
@@ -26,10 +26,12 @@ class ColorHistogram(PlotPlugin):
|
||||
L, a, b = self.lab_image.T
|
||||
left, right = -100, 100
|
||||
ab_extents = [left, right, right, left]
|
||||
self.mask = np.ones(L.shape, bool)
|
||||
bins = np.arange(left, right)
|
||||
hist, x_edges, y_edges = np.histogram2d(a.flatten(), b.flatten(), bins,
|
||||
normed=True)
|
||||
|
||||
hist, x_edges, y_edges = np.histogram2d(a.flatten(), b.flatten(),
|
||||
bins, normed=True)
|
||||
self.data = {'bins': bins, 'hist': hist, 'edges': (x_edges, y_edges),
|
||||
'extents': (left, right, left, right)}
|
||||
# Clip bin heights that dominate a-b histogram
|
||||
max_val = pct_total_area(hist, percentile=self.max_pct)
|
||||
hist = exposure.rescale_intensity(hist, in_range=(0, max_val))
|
||||
@@ -46,15 +48,36 @@ class ColorHistogram(PlotPlugin):
|
||||
|
||||
def ab_selected(self, extents):
|
||||
x0, x1, y0, y1 = extents
|
||||
self.data['extents'] = extents
|
||||
|
||||
lab_masked = self.lab_image.copy()
|
||||
L, a, b = lab_masked.T
|
||||
|
||||
mask = ((a > y0) & (a < y1)) & ((b > x0) & (b < x1))
|
||||
lab_masked[..., 1:][~mask.T] = 0
|
||||
self.mask = ((a > y0) & (a < y1)) & ((b > x0) & (b < x1))
|
||||
lab_masked[..., 1:][~self.mask.T] = 0
|
||||
|
||||
self.image_viewer.image = color.lab2rgb(lab_masked)
|
||||
|
||||
def output(self):
|
||||
"""Return the image mask and the histogram data.
|
||||
|
||||
Returns
|
||||
-------
|
||||
mask : array of bool, same shape as image
|
||||
The selected pixels.
|
||||
data : dict
|
||||
The data describing the histogram and the selected region.
|
||||
Keys:
|
||||
- 'bins' : array of float, the bin boundaries for both
|
||||
`a` and `b` channels.
|
||||
- 'hist' : 2D array of float, the normalized histogram.
|
||||
- 'edges' : tuple of array of float, the bin edges
|
||||
along each dimension
|
||||
- 'extents' : tuple of float, the left and right and
|
||||
top and bottom of the selected region.
|
||||
"""
|
||||
return (self.mask, self.data)
|
||||
|
||||
|
||||
def pct_total_area(image, percentile=0.80):
|
||||
"""Return threshold value based on percentage of total area.
|
||||
@@ -65,5 +88,3 @@ def pct_total_area(image, percentile=0.80):
|
||||
sorted_pixels = np.sort(image.flat)
|
||||
return sorted_pixels[idx]
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -108,3 +108,15 @@ class OverlayPlugin(Plugin):
|
||||
# clear overlay from ImageViewer on close
|
||||
self.overlay = None
|
||||
super(OverlayPlugin, self).closeEvent(event)
|
||||
|
||||
def output(self):
|
||||
"""Return the overlaid image.
|
||||
|
||||
Returns
|
||||
-------
|
||||
overlay : array, same shape as image
|
||||
The overlay currently displayed.
|
||||
data : None
|
||||
"""
|
||||
return (self.overlay, None)
|
||||
|
||||
|
||||
@@ -228,6 +228,7 @@ class ImageViewer(QtGui.QMainWindow):
|
||||
self._show()
|
||||
if main_window:
|
||||
utils.start_qtapp()
|
||||
return [p.output() for p in self.plugins]
|
||||
|
||||
def redraw(self):
|
||||
self.canvas.draw_idle()
|
||||
|
||||
Reference in New Issue
Block a user