📜  使用 PyQtGraph 的图像分析工具

📅  最后修改于: 2022-05-13 01:55:47.844000             🧑  作者: Mango

使用 PyQtGraph 的图像分析工具

在本文中,我们将看到如何使用Python中的 PyQtGraph 模块执行常见的图像分析。 PyQtGraph 是一个用于Python的图形和用户界面库,它提供了设计和科学应用程序中通常需要的功能。它的主要目标是提供用于显示数据(绘图、视频等)的快速交互式图形,其次是提供有助于快速应用程序开发的工具(例如,Qt Designer 中使用的属性树)。

为了安装 PyQtGraph,我们使用下面给出的命令。

pip install pyqtgraph

图像分析是从图像中提取有意义的信息;主要是通过数字图像处理技术从数字图像中提取。图像分析任务可以像读取条形码标签一样简单,也可以像从面部识别一个人一样复杂。

为此,我们必须执行以下操作。

  1. 导入所需的库,如 pyqtgraph、pyqt5 和 numpy。
  2. 使用 pyqt5 创建一个主窗口类。
  3. 创建一个图形窗口以添加显示图像分析所需的小部件。
  4. 创建两个绘图区域并将图像项添加到其中,并将 roi 对象添加到第一个绘图区域。
  5. 创建一个 isocurve 对象并将其添加到图像项中。
  6. 为图像创建数据并将其添加到图像项中。
  7. 当区域发生变化时,将更新方法连接到 roi 对象,在更新方法中获取区域并将其设置为第二个绘图区域。
  8. 创建一个鼠标移动事件,并根据鼠标位置为标题设置位置、像素值。
  9. 将此图形窗口添加到带有任何其他小部件的主窗口布局。

下面是实现

Python3
# importing Qt widgets
from PyQt5.QtWidgets import *
 
# importing system
import sys
 
# importing numpy as np
import numpy as np
 
# importing pyqtgraph as pg
import pyqtgraph as pg
from PyQt5.QtGui import *
from PyQt5.QtCore import *
 
class Window(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("PyQtGraph")
 
        # setting geometry
        self.setGeometry(100, 100, 900, 550)
 
        # icon
        icon = QIcon("skin.png")
 
        # setting icon to the window
        self.setWindowIcon(icon)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
    # method for components
    def UiComponents(self):
 
        # creating a widget object
        widget = QWidget()
 
        # text
        text = "Image Analysis"
 
        # creating a label
        label = QLabel(text)
 
        # setting minimum width
        label.setMinimumWidth(130)
 
        # making label do word wrap
        label.setWordWrap(True)
 
        # creating a graphic layout widget
 
        win = pg.GraphicsLayoutWidget()
 
        # plot area (ViewBox + axes) for displaying the image
        p1 = win.addPlot(title="")
 
        # item for displaying image data
        img = pg.ImageItem()
 
        # adding image to the plot area
        p1.addItem(img)
 
        # Custom ROI for selecting an image region
        roi = pg.ROI([-10, 14], [5, 5])
        roi.addScaleHandle([0.5, 1], [0.5, 0.5])
        roi.addScaleHandle([0, 0.5], [0.5, 0.5])
 
        # adding roi to the plot area
        p1.addItem(roi)
 
        # setting z value to roi
        # making sure ROI is drawn above image
        roi.setZValue(10)
 
        # creating a Isocurve drawing on the image
        iso = pg.IsocurveItem(level=1.2, pen='r')
 
        # setting parent as image
        iso.setParentItem(img)
 
        # setting z axis value of isocurve
        iso.setZValue(5)
 
        # Contrast/color control
        hist = pg.HistogramLUTItem()
 
        # setting image to the control
        hist.setImageItem(img)
 
        # adding control widget to the plot window
        win.addItem(hist)
 
        # creating draggable line for setting isocurve level
        isoLine = pg.InfiniteLine(angle=0, movable=True, pen='r')
        hist.vb.addItem(isoLine)
 
        # making user interaction a little easier
        hist.vb.setMouseEnabled(y=False)
        isoLine.setValue(0.8)
 
        # bring iso line above contrast controls
        isoLine.setZValue(1000)
 
        # going to next row of graphic window
        win.nextRow()
 
        # another plot area for displaying ROI data
        p2 = win.addPlot(colspan=2)
 
        # setting maximum height of plot area
        p2.setMaximumHeight(250)
 
        # generating image data
        data = np.random.normal(size=(200, 100))
        data[20:80, 20:80] += 2.
 
        # setting gaussian filter to the data
        data = pg.gaussianFilter(data, (3, 3))
        data += np.random.normal(size=(200, 100)) * 0.1
 
        # setting data to the image
        img.setImage(data)
 
        # setting level
        hist.setLevels(data.min(), data.max())
 
        # build isocurves from smoothed data
        iso.setData(pg.gaussianFilter(data, (2, 2)))
 
        # set position and scale of image
        img.scale(0.2, 0.2)
        img.translate(-50, 0)
 
        # zoom to fit image
        p1.autoRange()
 
        # method for updating the plot
        def updatePlot():
 
            # getting the selected region by the roi
            selected = roi.getArrayRegion(data, img)
 
            # plot the selected region
            p2.plot(selected.mean(axis=0), clear=True)
 
        # connecting the update plot method
        # it get called when the region is changed
        roi.sigRegionChanged.connect(updatePlot)
 
        # call the update plot method
        updatePlot()
 
        # method for updating the isocurve
        def updateIsocurve():
            # setting iso level
            iso.setLevel(isoLine.value())
 
        isoLine.sigDragged.connect(updateIsocurve)
 
        # method for image hover event
        def imageHoverEvent(event):
 
            # showing the position, pixel, and value under the mouse cursor
            # if cursor is not on the plot area
            if event.isExit():
                # set title as blank
                p1.setTitle("")
                return
 
            # getting cursor position
            pos = event.pos()
            i, j = pos.y(), pos.x()
 
            # pixel values
            i = int(np.clip(i, 0, data.shape[0] - 1))
            j = int(np.clip(j, 0, data.shape[1] - 1))
 
            # value of point
            val = data[i, j]
            ppos = img.mapToParent(pos)
            x, y = ppos.x(), ppos.y()
 
            # setting plot title data
            p1.setTitle(
                "pos: (%0.1f, %0.1f)  pixel: (%d, %d)  value: %g" % (x, y, i, j, val))
 
        # Monkey-patch the image to use our custom hover function.
        img.hoverEvent = imageHoverEvent
 
        # Creating a grid layout
        layout = QGridLayout()
 
        # minimum width value of the label
        label.setMinimumWidth(130)
 
        # setting this layout to the widget
        widget.setLayout(layout)
 
        # adding label in the layout
        layout.addWidget(label, 1, 0)
 
        # plot window goes on right side, spanning 3 rows
        layout.addWidget(win, 0, 1, 3, 1)
 
        # setting this widget as central widget of the main widow
        self.setCentralWidget(widget)
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())


输出: