使用 PyQt5 和 PyQtGraph,我在图像顶部显示了一个圆形散点图。这两个项目都在 QGraphicsView 中,它是从 ImageView 对象提升的。当可能生成数百个特定点时,我正在尝试找到一种简单的方法来批量选择特定点。
我发现了一个 CircleROI 对象,它是 pyqtgraph 的 ROI 的子类。创建圆圈后,我希望有某种方法来引用其中“选定”的点......从那里导出数据很简单。但我不知道如何实现这一点。
这是我目前正在尝试的:
self.roi = pg.CircleROI([500, 500], [100, 100], pen=(4,9), movable=True, resizable=True)
self.roi.setAcceptedMouseButtons(QtCore.Qt.MouseButton.RightButton)
self.roi.sigClicked.connect(self.select_points)
self.graphicsView.view.addItem(self.roi)
def select_points(self):
#self.dataPlot is a pg.ScatterPlotItem
selected = self.roi.getArrayRegion(self.dataPlot.points, self.graphicsView.imageItem, axes=(0))
print(*selected, sep = ", ")
我收到此错误:
selected = self.roi.getArrayRegion(self.dataPlot.points, self.graphicsView.imageItem, axes=(0))
File "C:\runnable\soft\Python37\lib\site-packages\pyqtgraph\graphicsItems\ROI.py", line 1891, in getArrayRegion
returnMappedCoords, **kwds)
File "C:\runnable\soft\Python37\lib\site-packages\pyqtgraph\graphicsItems\ROI.py", line 1193, in getArrayRegion
rgn = fn.affineSlice(data, shape=shape, vectors=vectors, origin=origin, axes=axes, **kwds)
File "C:\runnable\soft\Python37\lib\site-packages\pyqtgraph\functions.py", line 810, in affineSlice
x = affineSliceCoords(shape, origin, vectors, axes)
File "C:\runnable\soft\Python37\lib\site-packages\pyqtgraph\functions.py", line 740, in affineSliceCoords
if len(origin) != len(axes):
TypeError: object of type 'int' has no len()
如何修复此错误?如果这是错误的方法,我该怎么办?
由于低代表,我无法发表评论。
来自文档:
axes: (length-2 tuple) 指定data中的轴 对应于 img 的 (x, y) 轴。如果 图像的轴顺序设置为 'row-major',那么轴被指定为 (y, x) 顺序。
我不确定,但我怀疑这与您在期望 2 时在 axis 参数中给出单个项目有关。如果您查看回溯并检查模块中的那段代码,您可以看到它试图将原点的长度与轴的数量(轴元组的长度)相匹配,但它试图获取现在是一个 int,因为这就是你给它的。
这是那段源代码:
def affineSliceCoords(shape, origin, vectors, axes):
"""Return the array of coordinates used to sample data arrays in affineSlice().
"""
# sanity check
if len(shape) != len(vectors):
raise Exception("shape and vectors must have same length.")
if len(origin) != len(axes):
raise Exception("origin and axes must have same length.")
for v in vectors:
if len(v) != len(axes):
raise Exception("each vector must be same length as axes.")
编辑: 我还认为第一个参数(给出数组/数据)应该是对方法的调用,而不是引用,即添加括号。或者它是一个保存点数组的属性?
我希望这有帮助,或者你已经明白了。