【问题标题】:pyqtgraph image point selectionpyqtgraph 图像点选择
【发布时间】:2021-12-20 00:41:49
【问题描述】:

我正在尝试为我的实验室制作一个用于手动图像配准的工具——用户可以在其中选择两个不同图像上的一些点来对齐它们。我在 matplotlib 中制作了这个,但是放大/缩小太慢了(我认为因为我们正在对齐的图像非常高分辨率)。在 pyqtgraph 中有没有好的方法来做到这一点?我只需要能够并排选择两个图像图上的点并显示点选择的位置。

目前我在ImageViews 中有图像,我尝试用imv.scene.sigMouseClicked.connect(mouse_click) 来做,但是在mouse_click(evt) evt.pos()、evt.scenePos() 和evt.screenPos() 中都给出了图像中没有的坐标坐标。我还尝试使用无 ROI 手柄进行点选择(因为我可以从中获得正确的坐标),但您似乎无法为手柄着色,这并不是我想知道的完全交易破坏者如果有更好的选择。有没有更好的方法来做到这一点?

【问题讨论】:

    标签: python pyqtgraph


    【解决方案1】:

    您的问题不清楚您希望程序如何匹配点,这里我提供一个简单的解决方案,让您 (1) 显示图像。 (2) 给图像添加点。

    基本思路是使用pg.GraphicsLayoutWidget,然后添加pg.ImageItem和pg.ScatterPlotItem,每次鼠标点击都会在ScatterPlotItem中添加一个点。代码:

    import sys
    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout
    import pyqtgraph as pg
    import cv2 
    
    pg.setConfigOption('background', 'w')
    pg.setConfigOption('foreground', 'k')
    
    class ImagePlot(pg.GraphicsLayoutWidget):
    
        def __init__(self):
            super(ImagePlot, self).__init__()
            self.p1 = pg.PlotItem() 
            self.addItem(self.p1)
            self.p1.vb.invertY(True) # Images need inverted Y axis
    
            # Use ScatterPlotItem to draw points
            self.scatterItem = pg.ScatterPlotItem(
                size=10, 
                pen=pg.mkPen(None), 
                brush=pg.mkBrush(255, 0, 0),
                hoverable=True,
                hoverBrush=pg.mkBrush(0, 255, 255)
            )
            self.scatterItem.setZValue(2) # Ensure scatterPlotItem is always at top
            self.points = [] # Record Points
    
            self.p1.addItem(self.scatterItem)
    
    
        def setImage(self, image_path, size):
            
            self.p1.clear()
            self.p1.addItem(self.scatterItem)
            # pg.ImageItem.__init__ method takes input as an image array
            # I use opencv to load image, you can replace with other packages
            image = cv2.imread(image_path, 1)
            # resize image to some fixed size
            image = cv2.resize(image, size)
    
            self.image_item = pg.ImageItem(image)
            self.image_item.setOpts(axisOrder='row-major')
            self.p1.addItem(self.image_item)
    
    
        def mousePressEvent(self, event):
    
            point = self.p1.vb.mapSceneToView(event.pos()) # get the point clicked
            # Get pixel position of the mouse click
            x, y = int(point.x()), int(point.y())
    
            self.points.append([x, y])
            self.scatterItem.setPoints(pos=self.points)
            super().mousePressEvent(event)
    
        
    
    if __name__ == "__main__":
    
        QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
        app = QApplication([])
        win = QMainWindow()
    
        central_win = QWidget()
        layout = QHBoxLayout()
        central_win.setLayout(layout)
        win.setCentralWidget(central_win)
    
        image_plot1 = ImagePlot()
        image_plot2 = ImagePlot()
        layout.addWidget(image_plot1)
        layout.addWidget(image_plot2)
    
        image_plot1.setImage('/home/think/image1.png', (310, 200))
        image_plot2.setImage('/home/think/image2.jpeg', (310, 200))
        # You can access points by accessing image_plot1.points
        win.show()
    
        if (sys.flags.interactive != 1) or not hasattr(Qt.QtCore, "PYQT_VERSION"):
            QApplication.instance().exec_()
    

    结果如下:

    【讨论】:

    • 嘿哇,谢谢你的回复!是的,我对匹配含糊不清,因为我已经弄清楚了如何在我以前的 matplotlib 版本中进行所有转换(我应该明确说明这一点)。非常感谢这个,它超级有用!我没有意识到你可以同时使用这样的散点图和图像(男孩,我有很多东西要学)
    猜你喜欢
    • 2016-06-25
    • 2018-11-16
    • 2013-05-17
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2010-10-06
    相关资源
    最近更新 更多