ViewBox.setLimits() 的缩放参数如何工作?

How do the scaling paramters of ViewBox.setLimits() work?

我正在尝试限制 ViewBox 可以缩放多少 in/out 以及它可以移动多少。
我知道我必须使用 setLimits() 并且我已经阅读了此处的文档
https://pyqtgraph.readthedocs.io/en/latest/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setLimits

虽然平移限制很明显,但我无法真正理解缩放限制的工作原理。
计量单位是什么?是像素吗?百分比?

我已经达到了这些值的可用点,但不明白为什么会困扰我!

view.setLimits(xMin=-image.shape[0]*0.05, xMax=image.shape[0]*1.05,
               minXRange=100, maxXRange=2000,
               yMin=-image.shape[1]*0.05, yMax=image.shape[1]*1.05,
               minYRange=100, maxYRange=2000)

我认为这是一个比其他任何问题都更理论化的问题,但如果您想尝试一些代码,请看这里

# import the necessary packages
from pyqtgraph.graphicsItems.ImageItem import ImageItem
from pyqtgraph.graphicsItems.LinearRegionItem import LinearRegionItem
import requests
import numpy as np
import cv2
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui


image = cv2.imread('aggraffatura.jpg') # Change the picture here!
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)

app = QtGui.QApplication([])

## Create window with GraphicsView widget
w = pg.GraphicsView()
w.show()
w.resize(image.shape[0]/2, image.shape[1]/2) # Depending on the picture you may not need to resize
w.setWindowTitle('Test')

view = pg.ViewBox()
view.setLimits(xMin=-image.shape[0]*0.05, xMax=image.shape[0]*1.05,
               minXRange=100, maxXRange=2000,
               yMin=-image.shape[1]*0.05, yMax=image.shape[1]*1.05,
               minYRange=100, maxYRange=2000)
w.setCentralItem(view)

## lock the aspect ratio
view.setAspectLocked(True)

## Add image item
item = ImageItem(image)
view.addItem(item)

## Add line item
line = LinearRegionItem()
view.addItem(line)

def mouseClicked(evt):
    pos = evt[0]
    print(pos)

proxyClicked = pg.SignalProxy(w.scene().sigMouseClicked, rateLimit=60, slot=mouseClicked)


## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

What's the unit of measure? Is it pixels? Percentage?

简答:缩放限制的度量单位与平移限制相同。

长答案: 在 class ViewBox 中,方法 setLimits 调用方法 updateViewRange,更新视图范围以匹配给定宽高比限制,目标视图范围尽可能接近。在 updateViewRange 方法中,有一段循环遍历两个轴并将最大视图范围设置为最大视图范围(最大缩放限制)和下限和上限的绝对差值中的较小者边界(即最大-最小值,平移限制的差异)(如果未给出缩放限制,则它将设置为平移限制的差异)。由于这两个限制可以互换,因此它们应该具有相同的度量单位。 只有通过检查源代码才能看到最大范围不能大于边界(如果给定的话)。这条信息应该添加到文档中。 注意:当您放大到限制时,您实际上是将视图范围设置为缩放限制的 minRange。

示例:这里我将使用op的示例来说明这个概念。 Download this image and rename it to '500x500' to test the example.。在开始时,您应该看到视图范围设置为 maxRange(400px),这是绿色圆圈的直径。通过放大,您应该看到视图范围永远不会小于直径为 100px 的红色圆圈。平移限制设置为图像的形状,即 500 X 500px。

# import the necessary packages
from pyqtgraph.graphicsItems.ImageItem import ImageItem
from pyqtgraph.graphicsItems.LinearRegionItem import LinearRegionItem
import requests
import numpy as np
import cv2
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui

# Name the image to 500x500
image = cv2.imread('500x500.jpg') # Change the picture here!
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)

app = QtGui.QApplication([])

## Create window with GraphicsView widget
w = pg.GraphicsView()
w.show()
w.resize(image.shape[0], image.shape[1]) # Depending on the picture you may not need to resize
w.setWindowTitle('Test')

view = pg.ViewBox()
view.setLimits(xMin=0, xMax=image.shape[0],
               minXRange=100, maxXRange=400,
               yMin=0, yMax=image.shape[1],
               minYRange=100, maxYRange=400)
w.setCentralItem(view)

## lock the aspect ratio
view.setAspectLocked(True)

## Add image item
item = ImageItem(image)
view.addItem(item)

## Add line item
line = LinearRegionItem()
view.addItem(line)

def mouseClicked(evt):
    pos = evt[0]
    print(pos)

proxyClicked = pg.SignalProxy(w.scene().sigMouseClicked, rateLimit=60, slot=mouseClicked)


## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()