Commit 2f510de2 authored by Luke Campagnola's avatar Luke Campagnola
Browse files

Added PolyLineROI.getArrayRegion

parent db5c303f
...@@ -56,7 +56,7 @@ rois.append(pg.MultiRectROI([[20, 90], [50, 60], [60, 90]], width=5, pen=(2,9))) ...@@ -56,7 +56,7 @@ rois.append(pg.MultiRectROI([[20, 90], [50, 60], [60, 90]], width=5, pen=(2,9)))
rois.append(pg.EllipseROI([60, 10], [30, 20], pen=(3,9))) rois.append(pg.EllipseROI([60, 10], [30, 20], pen=(3,9)))
rois.append(pg.CircleROI([80, 50], [20, 20], pen=(4,9))) rois.append(pg.CircleROI([80, 50], [20, 20], pen=(4,9)))
#rois.append(pg.LineSegmentROI([[110, 50], [20, 20]], pen=(5,9))) #rois.append(pg.LineSegmentROI([[110, 50], [20, 20]], pen=(5,9)))
#rois.append(pg.PolyLineROI([[110, 60], [20, 30], [50, 10]], pen=(6,9))) rois.append(pg.PolyLineROI([[80, 60], [90, 30], [60, 40]], pen=(6,9), closed=True))
def update(roi): def update(roi):
img1b.setImage(roi.getArrayRegion(arr, img1a), levels=(0, arr.max())) img1b.setImage(roi.getArrayRegion(arr, img1a), levels=(0, arr.max()))
......
...@@ -249,7 +249,7 @@ class ImageItem(GraphicsObject): ...@@ -249,7 +249,7 @@ class ImageItem(GraphicsObject):
def render(self): def render(self):
prof = debug.Profiler('ImageItem.render', disabled=True) prof = debug.Profiler('ImageItem.render', disabled=True)
if self.image is None: if self.image is None or self.image.size == 0:
return return
if isinstance(self.lut, collections.Callable): if isinstance(self.lut, collections.Callable):
lut = self.lut(self.image) lut = self.lut(self.image)
...@@ -269,6 +269,8 @@ class ImageItem(GraphicsObject): ...@@ -269,6 +269,8 @@ class ImageItem(GraphicsObject):
return return
if self.qimage is None: if self.qimage is None:
self.render() self.render()
if self.qimage is None:
return
prof.mark('render QImage') prof.mark('render QImage')
if self.paintMode is not None: if self.paintMode is not None:
p.setCompositionMode(self.paintMode) p.setCompositionMode(self.paintMode)
......
...@@ -802,7 +802,11 @@ class ROI(GraphicsObject): ...@@ -802,7 +802,11 @@ class ROI(GraphicsObject):
Also returns the transform which maps the ROI into data coordinates. Also returns the transform which maps the ROI into data coordinates.
If returnSlice is set to False, the function returns a pair of tuples with the values that would have If returnSlice is set to False, the function returns a pair of tuples with the values that would have
been used to generate the slice objects. ((ax0Start, ax0Stop), (ax1Start, ax1Stop))""" been used to generate the slice objects. ((ax0Start, ax0Stop), (ax1Start, ax1Stop))
If the slice can not be computed (usually because the scene/transforms are not properly
constructed yet), then the method returns None.
"""
#print "getArraySlice" #print "getArraySlice"
## Determine shape of array along ROI axes ## Determine shape of array along ROI axes
...@@ -810,7 +814,10 @@ class ROI(GraphicsObject): ...@@ -810,7 +814,10 @@ class ROI(GraphicsObject):
#print " dshape", dShape #print " dshape", dShape
## Determine transform that maps ROI bounding box to image coordinates ## Determine transform that maps ROI bounding box to image coordinates
try:
tr = self.sceneTransform() * fn.invertQTransform(img.sceneTransform()) tr = self.sceneTransform() * fn.invertQTransform(img.sceneTransform())
except np.linalg.linalg.LinAlgError:
return None
## Modify transform to scale from image coords to data coords ## Modify transform to scale from image coords to data coords
#m = QtGui.QTransform() #m = QtGui.QTransform()
...@@ -1737,12 +1744,35 @@ class PolyLineROI(ROI): ...@@ -1737,12 +1744,35 @@ class PolyLineROI(ROI):
def shape(self): def shape(self):
p = QtGui.QPainterPath() p = QtGui.QPainterPath()
if len(self.handles) == 0:
return p
p.moveTo(self.handles[0]['item'].pos()) p.moveTo(self.handles[0]['item'].pos())
for i in range(len(self.handles)): for i in range(len(self.handles)):
p.lineTo(self.handles[i]['item'].pos()) p.lineTo(self.handles[i]['item'].pos())
p.lineTo(self.handles[0]['item'].pos()) p.lineTo(self.handles[0]['item'].pos())
return p return p
def getArrayRegion(self, data, img, axes=(0,1), returnMappedCoords=False, **kwds):
sl = self.getArraySlice(data, img, axes=(0,1))
if sl is None:
return None
sliced = data[sl[0]]
im = QtGui.QImage(sliced.shape[axes[0]], sliced.shape[axes[1]], QtGui.QImage.Format_ARGB32)
im.fill(0x0)
p = QtGui.QPainter(im)
p.setPen(fn.mkPen(None))
p.setBrush(fn.mkBrush('w'))
p.setTransform(self.itemTransform(img)[0])
bounds = self.mapRectToItem(img, self.boundingRect())
p.translate(-bounds.left(), -bounds.top())
p.drawPath(self.shape())
p.end()
mask = fn.imageToArray(im)[:,:,0].astype(float) / 255.
shape = [1] * data.ndim
shape[axes[0]] = sliced.shape[axes[0]]
shape[axes[1]] = sliced.shape[axes[1]]
return sliced * mask
class LineSegmentROI(ROI): class LineSegmentROI(ROI):
""" """
...@@ -1845,8 +1875,8 @@ class SpiralROI(ROI): ...@@ -1845,8 +1875,8 @@ class SpiralROI(ROI):
#for h in self.handles: #for h in self.handles:
#h['pos'] = h['item'].pos()/self.state['size'][0] #h['pos'] = h['item'].pos()/self.state['size'][0]
def stateChanged(self): def stateChanged(self, finish=True):
ROI.stateChanged(self) ROI.stateChanged(self, finish=finish)
if len(self.handles) > 1: if len(self.handles) > 1:
self.path = QtGui.QPainterPath() self.path = QtGui.QPainterPath()
h0 = Point(self.handles[0]['item'].pos()).length() h0 = Point(self.handles[0]['item'].pos()).length()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment