1) First of all, I have a 2D numpy array (the image): type(mat): numpy.ndarray: mat.shape: (256, 256)I can visualize it with pg.image(mat) and it is of course in a gray scale. I am not able to visualize it using a colormap.
2) I use the pg.PolyLineROI tool to draw a region of interest. How can I have a mask of the selected ROI?
For the sake of clarity, with mask I mean a numpy array with the same shape of the image and that has 1 (or True) for all the inside the ROI points and 0 (or False) for all the points outside the ROI.
Hi Luke, thanks a lot for your replay. Great help for the Colormap.
For the other problem, I am still in trouble... I don't know which arguments do I have to pass to the method getArrayRegion.
If I type help(rr.getArrayRegion) - where "rr" is the name of the PolyLineROI - it is wrote that I have to pass the arguments data and img.
What are them? I don't think they are rr methods and I have no clue about it.
Thanks in advance for your patience
data = np.dstack(images)
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]]
ar = sliced.copy()
for i in range(sliced.shape[-1]):
ar[:,:,i] = sliced[:,:,i]*mask
return ar
Hi Luke,I have just setup a viewbox that can handle multiple polyline ROIs. I added the getArrayRegion fuction that you recently posted so I could calculate the average grey scale value.I was getting the following error:File "ROI.py", line 1774, in getArrayRegionreturn sliced * maskValueError: operands could not be broadcast together with shapes (65,93,5) (65,93)This appears to be related to the fact that my data is actually a 3D array of images (5 in this case) created using:data = np.dstack(images)
where images is a list of 2D arrays (one for each image).
Hi Luke,That works. I thought you were heading somewhere with the shape = [1] * data.ndim and following lines.