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

2024-05-03 09:45:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图限制一个视图框可以放大/缩小的范围以及可以移动的范围。
我知道我必须使用setLimits(),并且我已经阅读了这里的文档
https://pyqtgraph.readthedocs.io/en/latest/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setLimits

虽然平移限制是不言而喻的,但我无法真正理解缩放限制是如何工作的。
度量单位是多少?是像素吗?百分比

A screenshot of the docs is handy

我已经用这些值达到了一个可用点,但不明白为什么它在困扰我

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_()

Tags: thefromimageimportviewitemcv2pyqtgraph
1条回答
网友
1楼 · 发布于 2024-05-03 09:45:36

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

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

长答案: 在类ViewBox中,方法setLimits调用方法updateViewRange,该方法在给定纵横比约束的情况下,更新视图范围以尽可能接近目标视图范围。在updateViewRange方法中,有一个部分循环通过两个轴,并将最大视图范围设置为最大视图范围(最大缩放限制)和上下限的绝对差值(即最大最小值,平移限制的差值)中的较小值(如果未给出缩放限制,则将其设置为平移限制的差值)。由于两个限制可以互换,因此它们应具有相同的测量单位。 只有通过检查源代码,才能看到如果给定了最大范围,则最大范围不能大于边界。此信息应添加到文档中。 注意:放大到限制时,实际上是将视图范围设置为缩放限制的最小范围

示例:这里我将使用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_()

相关问题 更多 >