Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQT用户可编辑多边形_Python_Pyqt_Pyqt5_Qgraphicsview_Qgraphicsscene - Fatal编程技术网

Python PyQT用户可编辑多边形

Python PyQT用户可编辑多边形,python,pyqt,pyqt5,qgraphicsview,qgraphicsscene,Python,Pyqt,Pyqt5,Qgraphicsview,Qgraphicsscene,我正在使用QPainter和QPolygon类在图像上绘制多边形。我需要用户能够通过拖动多边形中的点来编辑这些多边形。QPolygon和QPainter类没有任何信号,因此我无法通过它们触发事件 我已经意识到,如果我继续使用这种方法,那么每次移动一个点时都必须渲染图像,这可能看起来很糟糕,而且一点也不平滑 有人知道我怎么能把这些点放在图片上而不是在图片上画它们吗 如果您使用的是QGraphicsView和QGraphicscene,最好使用这些项目,不要使用QPaint,因为您的任务只是绘制,而

我正在使用QPainter和QPolygon类在图像上绘制多边形。我需要用户能够通过拖动多边形中的点来编辑这些多边形。QPolygon和QPainter类没有任何信号,因此我无法通过它们触发事件

我已经意识到,如果我继续使用这种方法,那么每次移动一个点时都必须渲染图像,这可能看起来很糟糕,而且一点也不平滑


有人知道我怎么能把这些点放在图片上而不是在图片上画它们吗

如果您使用的是QGraphicsView和QGraphicscene,最好使用这些项目,不要使用QPaint,因为您的任务只是绘制,而不处理其他类型的事件。在下面的示例中,我显示了一个自定义QGraphicsPolygonItem来绘制多边形,并在每个顶点中显示另一个项目,其任务是移动和修改多边形的点。在我的示例中,您可以加载图像,也可以创建多边形,您必须按“多边形”菜单,然后通过单击添加它,若要表示不想添加更多元素,必须按ESC键,然后可以通过拖动或从项目中心拖动多边形来移动每个顶点。最后,如果需要缩放和缩放,请按Ctrl++和Ctrl+-

从枚举导入枚举
从functools导入部分
从PyQt5导入QtWidgets、QtGui、QtCore
类GripItem(qtwidts.QGraphicsPathItem):
circle=QtGui.QPainterPath()
圆加法器(QtCore.QRectF(-10,-10,20,20))
square=QtGui.QPainterPath()
square.addRect(QtCore.QRectF(-15,-15,30,30))
定义初始化(自我、注释项、索引):
超级(GripItem,self)。\uuuu init\uuuuu()
self.m\u annotation\u item=annotation\u item
self.m_index=索引
self.setPath(GripItem.circle)
self.setBrush(QtGui.QColor(“绿色”))
self.setPen(QtGui.QPen(QtGui.QColor(“绿色”),2))
self.setFlag(qtwidts.QGraphicsItem.ItemIsSelectable,True)
self.setFlag(qtwidts.QGraphicsItem.ItemIsMovable,True)
self.setFlag(qtwidts.QGraphicsItem.itemssendsgeometrychanges,True)
self.setAcceptHoverEvents(True)
自我设置值(11)
self.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
def hoverEnterEvent(自我,事件):
self.setPath(GripItem.square)
self.setBrush(QtGui.QColor(“红色”))
超级(GripItem,self)。悬停事件(event)
def hoverLeaveEvent(自我,事件):
self.setPath(GripItem.circle)
self.setBrush(QtGui.QColor(“绿色”))
超级(GripItem,self)。悬停事件(event)
def mouseReleaseEvent(自身,事件):
自选(假)
超级(GripItem,self).鼠标事件(event)
def itemChange(自身、更改、值):
如果更改==QtWidgets.QGraphicsItem.ItemPositionChange和self.isEnabled():
self.m\u注释\u项.movePoint(self.m\u索引,值)
返回super(GripItem,self).itemChange(change,value)
类多边形注释(QtWidgets.QGraphicsPolygonItem):
def uuu init uuu(self,parent=None):
超级(多边形注释,自我)。\uuuu初始化\uuuuu(父级)
self.m_点=[]
自我设置值(10)
self.setPen(QtGui.QPen(QtGui.QColor(“绿色”),2))
self.setAcceptHoverEvents(True)
self.setFlag(qtwidts.QGraphicsItem.ItemIsSelectable,True)
self.setFlag(qtwidts.QGraphicsItem.ItemIsMovable,True)
self.setFlag(qtwidts.QGraphicsItem.itemssendsgeometrychanges,True)
self.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.m_项目=[]
def点数(自身):
返回len(self.m_项目)
def添加点(自身,p):
self.m_points.append(p)
self.setPolygon(QtGui.QPolygonF(self.m_点))
项目=GripItem(自我,透镜(自我m_点)-1)
self.scene().addItem(项目)
self.m_items.append(item)
项目.设置位置(p)
def removeLastPoint(自):
如果self.m_点:
self.m_points.pop()
self.setPolygon(QtGui.QPolygonF(self.m_点))
it=self.m_items.pop()
self.scene().removietem(it)
删除它
def移动点(自身、i、p):

如果0如果您使用QGraphicsView和QGraphicscene,则最好使用这些项目,不要使用QPaint,因为您的任务只是绘制,而不处理其他类型的事件。在下面的示例中,我显示了一个自定义QGraphicsPolygonItem来绘制多边形,并在每个顶点中显示另一个项目,其任务是移动和修改多边形的点。在我的示例中,您可以加载图像,也可以创建多边形,您必须按“多边形”菜单,然后通过单击添加它,若要表示不想添加更多元素,必须按ESC键,然后可以通过拖动或从项目中心拖动多边形来移动每个顶点。最后,如果需要缩放和缩放,请按Ctrl++和Ctrl+-

从枚举导入枚举
从functools导入部分
从PyQt5导入QtWidgets、QtGui、QtCore
类GripItem(qtwidts.QGraphicsPathItem):
circle=QtGui.QPainterPath()
圆加法器(QtCore.QRectF(-10,-10,20,20))
square=QtGui.QPainterPath()
square.addRect(QtCore.QRectF(-15,-15,30,30))
定义初始化(自我、注释项、索引):
超级(GripItem,self)。\uuuu init\uuuuu()
self.m\u annotation\u item=annotation\u item
self.m_index=索引
self.setPath(GripItem.circle)
self.setBrush(QtGui.QColor(“绿色”))
self.setPen(QtGui.QPen(QtGui.QColor(“绿色”),2))
self.setFlag(qtwidts.QGraphicsItem.ItemIsSelectable,True)
self.setFlag(qtwidts.QGraphicsItem.ItemIsMovable,True)
self.setFlag(qtwidts.QGraphicsItem.itemssendsgeometrychanges,True)
self.setAcceptHoverEvents(True)
自我设置值(11)
self.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
def hoverEnterEvent(自我,事件):
from enum import Enum
from functools import partial
from PyQt5 import QtWidgets, QtGui, QtCore


class GripItem(QtWidgets.QGraphicsPathItem):
    circle = QtGui.QPainterPath()
    circle.addEllipse(QtCore.QRectF(-10, -10, 20, 20))
    square = QtGui.QPainterPath()
    square.addRect(QtCore.QRectF(-15, -15, 30, 30))

    def __init__(self, annotation_item, index):
        super(GripItem, self).__init__()
        self.m_annotation_item = annotation_item
        self.m_index = index

        self.setPath(GripItem.circle)
        self.setBrush(QtGui.QColor("green"))
        self.setPen(QtGui.QPen(QtGui.QColor("green"), 2))
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, True)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges, True)
        self.setAcceptHoverEvents(True)
        self.setZValue(11)
        self.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))

    def hoverEnterEvent(self, event):
        self.setPath(GripItem.square)
        self.setBrush(QtGui.QColor("red"))
        super(GripItem, self).hoverEnterEvent(event)

    def hoverLeaveEvent(self, event):
        self.setPath(GripItem.circle)
        self.setBrush(QtGui.QColor("green"))
        super(GripItem, self).hoverLeaveEvent(event)

    def mouseReleaseEvent(self, event):
        self.setSelected(False)
        super(GripItem, self).mouseReleaseEvent(event)

    def itemChange(self, change, value):
        if change == QtWidgets.QGraphicsItem.ItemPositionChange and self.isEnabled():
            self.m_annotation_item.movePoint(self.m_index, value)
        return super(GripItem, self).itemChange(change, value)


class PolygonAnnotation(QtWidgets.QGraphicsPolygonItem):
    def __init__(self, parent=None):
        super(PolygonAnnotation, self).__init__(parent)
        self.m_points = []
        self.setZValue(10)
        self.setPen(QtGui.QPen(QtGui.QColor("green"), 2))
        self.setAcceptHoverEvents(True)

        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, True)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges, True)

        self.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))

        self.m_items = []

    def number_of_points(self):
        return len(self.m_items)

    def addPoint(self, p):
        self.m_points.append(p)
        self.setPolygon(QtGui.QPolygonF(self.m_points))
        item = GripItem(self, len(self.m_points) - 1)
        self.scene().addItem(item)
        self.m_items.append(item)
        item.setPos(p)

    def removeLastPoint(self):
        if self.m_points:
            self.m_points.pop()
            self.setPolygon(QtGui.QPolygonF(self.m_points))
            it = self.m_items.pop()
            self.scene().removeItem(it)
            del it

    def movePoint(self, i, p):
        if 0 <= i < len(self.m_points):
            self.m_points[i] = self.mapFromScene(p)
            self.setPolygon(QtGui.QPolygonF(self.m_points))

    def move_item(self, index, pos):
        if 0 <= index < len(self.m_items):
            item = self.m_items[index]
            item.setEnabled(False)
            item.setPos(pos)
            item.setEnabled(True)

    def itemChange(self, change, value):
        if change == QtWidgets.QGraphicsItem.ItemPositionHasChanged:
            for i, point in enumerate(self.m_points):
                self.move_item(i, self.mapToScene(point))
        return super(PolygonAnnotation, self).itemChange(change, value)

    def hoverEnterEvent(self, event):
        self.setBrush(QtGui.QColor(255, 0, 0, 100))
        super(PolygonAnnotation, self).hoverEnterEvent(event)

    def hoverLeaveEvent(self, event):
        self.setBrush(QtGui.QBrush(QtCore.Qt.NoBrush))
        super(PolygonAnnotation, self).hoverLeaveEvent(event)


class Instructions(Enum):
    No_Instruction = 0
    Polygon_Instruction = 1


class AnnotationScene(QtWidgets.QGraphicsScene):
    def __init__(self, parent=None):
        super(AnnotationScene, self).__init__(parent)
        self.image_item = QtWidgets.QGraphicsPixmapItem()
        self.image_item.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
        self.addItem(self.image_item)
        self.current_instruction = Instructions.No_Instruction

    def load_image(self, filename):
        self.image_item.setPixmap(QtGui.QPixmap(filename))
        self.setSceneRect(self.image_item.boundingRect())

    def setCurrentInstruction(self, instruction):
        self.current_instruction = instruction
        self.polygon_item = PolygonAnnotation()
        self.addItem(self.polygon_item)

    def mousePressEvent(self, event):
        if self.current_instruction == Instructions.Polygon_Instruction:
            self.polygon_item.removeLastPoint()
            self.polygon_item.addPoint(event.scenePos())
            # movable element
            self.polygon_item.addPoint(event.scenePos())
        super(AnnotationScene, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if self.current_instruction == Instructions.Polygon_Instruction:
            self.polygon_item.movePoint(self.polygon_item.number_of_points()-1, event.scenePos())
        super(AnnotationScene, self).mouseMoveEvent(event)


class AnnotationView(QtWidgets.QGraphicsView):
    factor = 2.0

    def __init__(self, parent=None):
        super(AnnotationView, self).__init__(parent)
        self.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform)
        self.setMouseTracking(True)
        QtWidgets.QShortcut(QtGui.QKeySequence.ZoomIn, self, activated=self.zoomIn)
        QtWidgets.QShortcut(QtGui.QKeySequence.ZoomOut, self, activated=self.zoomOut)

    @QtCore.pyqtSlot()
    def zoomIn(self):
        self.zoom(AnnotationView.factor)

    @QtCore.pyqtSlot()
    def zoomOut(self):
        self.zoom(1 / AnnotationView.factor)

    def zoom(self, f):
        self.scale(f, f)
        if self.scene() is not None:
            self.centerOn(self.scene().image_item)


class AnnotationWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(AnnotationWindow, self).__init__(parent)
        self.m_view = AnnotationView()
        self.m_scene = AnnotationScene(self)
        self.m_view.setScene(self.m_scene)

        self.setCentralWidget(self.m_view)
        self.create_menus()

        QtWidgets.QShortcut(QtCore.Qt.Key_Escape, self, activated=partial(self.m_scene.setCurrentInstruction, Instructions.No_Instruction))

    def create_menus(self):
        menu_file = self.menuBar().addMenu("File")
        load_image_action = menu_file.addAction("&Load Image")
        load_image_action.triggered.connect(self.load_image)

        menu_instructions = self.menuBar().addMenu("Intructions")
        polygon_action = menu_instructions.addAction("Polygon")
        polygon_action.triggered.connect(partial(self.m_scene.setCurrentInstruction, Instructions.Polygon_Instruction))

    @QtCore.pyqtSlot()
    def load_image(self):
        filename, _ = QtWidgets.QFileDialog.getOpenFileName(self, 
            "Open Image",
            QtCore.QStandardPaths.writableLocation(QtCore.QStandardPaths.PicturesLocation), #QtCore.QDir.currentPath(), 
            "Image Files (*.png *.jpg *.bmp)")
        if filename:
            self.m_scene.load_image(filename)
            self.m_view.fitInView(self.m_scene.image_item, QtCore.Qt.KeepAspectRatio)
            self.m_view.centerOn(self.m_scene.image_item)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = AnnotationWindow()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())