ROI drawer

53 views
Skip to first unread message

yvesweis...@gmail.com

unread,
Jun 20, 2015, 3:22:11 PM6/20/15
to pyqt...@googlegroups.com
Hey,

sorry to ask, I assume the answer to the question is quite simple, but I haven't been making any progress. What I am trying to do is display and image and an elliptical ROI over it and basically piecewise create a mask, on a button click. So that you move your ROI, click the button and this then takes the current ROI position, adds it to a mask array of zeros and then lets you drag the ROI again. What I am having trouble with is adding a button to something based a viewbox. Ideally I don't want to make it into an app, as I'd like to be able to call it as a method of a class and doing this with apps seems to cause my ipython to crash. This might be completely the wrong way of doing it, but here is what I have so far:

""" Initialise Stuff """

cols, rows = meanIm.shape

win = pg.GraphicsWindow()
view = win.addViewBox()
img = pg.ImageItem()
img.setImage(np.fliplr(meanIm.T)) #fliplr and transpose just for display purposes
roi = pg.EllipseROI(pos=[10, 10], size=[20, 20])
btn = pg.ButtonItem()
view.addItem(img)




###___________________________________________________________________________________________________________________
"""ROI stuff  """

m = np.mgrid[:cols,:rows]
possx = m[0,:,:]# make the x pos array
possy = m[1,:,:]# make the y pos array
possx.shape = cols,rows
possy.shape = cols,rows
mask = np.zeros([cols,rows])

roi = pg.EllipseROI(pos=[10, 10], size=[20, 20])
view.addItem(roi)

###___________________________________________________________________________________________________________________
""" Update Function to update the ROI position """

def update():
    
    global pixX,pixY,possx,possy,img, mask
    
    ## let user move ROI, then pull out selected region:
    pixX  = roi.getArrayRegion(possx, img).astype('int')
    pixY =  roi.getArrayRegion(possy, img).astype('int')
    pixx = pixX[np.nonzero(pixX)]
    pixy = pixY[np.nonzero(pixY)]
    mask[pixx,pixy] = 1


timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
#This means you call update every 30ms
timer.start(30) 



P.S. I have also tried working the other way around, starting with a QtGui.QWidget() and then adding fields, but haven't even managed to display the image let along add ROIs. Any advice on even which approach is more feasible would be amazing. Thanks for the help!!! :)

yvesweis...@gmail.com

unread,
Jun 21, 2015, 5:27:08 AM6/21/15
to pyqt...@googlegroups.com
figured it out. Will post a solution when its nice :)
Reply all
Reply to author
Forward
0 new messages