首页 > 解决方案 > 有没有办法锁定 pyqtgraph ROI ScaleRotateHandle 的方面?

问题描述

我正在使用 pyqtgraph ViewBox 来处理带有 ImageItem 类的图像,并使用 ROI 类来标记图像的重要部分。我正在处理的图像要求我使用具有给定 x 和 y 像素大小的 setAspectLocked。

问题是,如果我在 ViewBox 上设置纵横比锁定,则 ROI 比例旋转手柄在旋转时会被拉伸。有没有办法锁定它?或者有没有办法只在 ImageItem 类而不是整个 ViewBox 上应用 setAspectLocked?

我的问题有一个修改后的示例代码。尝试旋转右侧的矩形。

# -*- coding: utf-8 -*-
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np

pg.setConfigOptions(imageAxisOrder='row-major')


## create GUI
app = QtGui.QApplication([])
w = pg.GraphicsLayoutWidget(show=True, size=(1000,800), border=True)
w.setWindowTitle('pyqtgraph example: ROI Examples')

w3 = w.addLayout(row=1, col=0)
v3 = w3.addViewBox(row=1, col=0)
v3.setAspectLocked(True,1.8/18)

r3a = pg.ROI([0,0], [10,10])
v3.addItem(r3a)
## handles scaling horizontally around center
r3a.addScaleHandle([1, 0.5], [0.5, 0.5])
r3a.addScaleHandle([0, 0.5], [0.5, 0.5])

## handles scaling vertically from opposite edge
r3a.addScaleHandle([0.5, 0], [0.5, 1])
r3a.addScaleHandle([0.5, 1], [0.5, 0])

## handles scaling both vertically and horizontally
r3a.addScaleHandle([1, 1], [0, 0])
r3a.addScaleHandle([0, 0], [1, 1])

r3b = pg.ROI([20,0], [10,10])
v3.addItem(r3b)
## handles rotating around center
r3b.addRotateHandle([1, 1], [0.5, 0.5])
r3b.addRotateHandle([0, 0], [0.5, 0.5])

## handles rotating around opposite corner
r3b.addRotateHandle([1, 0], [0, 1])
r3b.addRotateHandle([0, 1], [1, 0])

## handles rotating/scaling around center
r3b.addScaleRotateHandle([0, 0.5], [0.5, 0.5])
r3b.addScaleRotateHandle([1, 0.5], [0.5, 0.5])

v3.disableAutoRange('xy')
v3.autoRange()

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

标签: pythonpyqt5pyqtgraphroi

解决方案


推荐阅读