Create RectROI centered on mouse click position

34 views
Skip to first unread message

Maxwell Grady

unread,
Feb 8, 2017, 2:54:15 PM2/8/17
to pyqtgraph

I am having trouble with the coordinates used to create a RectROI centered on the position of a mouse click.

For example if I have an image displayed:

import pyqtgraph as pg
import numpy as np

pw = pg.PlotWidget()
img = pg.ImageItem(np.random.normal(size=(400,400)))
pw.addItem(img)

then I can get us position of a user click by hooking into img.sigMouseClicked

img.sigMouseClicked.connect(drawRect)

def drawRect(pos):
   # img coordinates
   imc = img.mapFromScene(pos)
   width = 10
   height = 10

   rect = pg.RectROI(pos=, size=(width, height))
   pw.addItem(rect)


I am unsure of how to format the position of the rectangle such that the center is the coordinates of the mouse click (in image coordinates). The documentation suggests that the pos argument should be the coordinates of the lower left corner of the rectangle. However, do these coordinates need to be given in the scene coordinate system rather than the image coordinate system?

Would the correct transformation be as follows?

    pg.RectROI(pos=img.mapToScene(imc.x() - width/2, imc.y() + height/2), size=(width, height))
Reply all
Reply to author
Forward
0 new messages