python-3.x - 在 pyqtgraph.ImageView() 上绘制线条

标签 python-3.x pyqt5 numpy-ndarray qwidget

我正在使用 Pyqt5 在三个不同的小部件中绘制一些医学图像(numpy 数组)。现在,我想在图像上绘制一条线(使用 pg.ImageViewer 显示)。有人已经这样做了吗?

谢谢!

最佳答案

您可以使用 .getView() 访问 pg.ImageView 小部件的 View 框。从那里您可以使用 viewbox.AddItem() 添加您喜欢的任何项目。下面是 ImageView 示例的修改版本,它在 ImageView 上绘制了一条线图。

# -*- coding: utf-8 -*-
"""
This example demonstrates the use of ImageView with 3-color image stacks.
ImageView is a high-level widget for displaying and analyzing 2D and 3D data.
ImageView provides:

  1. A zoomable region (ViewBox) for displaying the image
  2. A combination histogram and gradient editor (HistogramLUTItem) for
     controlling the visual appearance of the image
  3. A timeline for selecting the currently displayed frame (for 3D data only).
  4. Tools for very basic analysis of image data (see ROI and Norm buttons)

"""
## Add path to library (just for examples; you do not need this)
import initExample

import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg

# Interpret image data as row-major instead of col-major
pg.setConfigOptions(imageAxisOrder='row-major')

app = pg.mkQApp("ImageView Example")

## Create window with ImageView widget
win = QtGui.QMainWindow()
win.resize(800,800)
imv = pg.ImageView()

imv_v = imv.getView()


win.setCentralWidget(imv)
win.show()
win.setWindowTitle('pyqtgraph example: ImageView')

## Create random 3D data set with time varying signals
dataRed = np.ones((100, 200, 200)) * np.linspace(90, 150, 100)[:, np.newaxis, np.newaxis]
dataRed += pg.gaussianFilter(np.random.normal(size=(200, 200)), (5, 5)) * 100
dataGrn = np.ones((100, 200, 200)) * np.linspace(90, 180, 100)[:, np.newaxis, np.newaxis]
dataGrn += pg.gaussianFilter(np.random.normal(size=(200, 200)), (5, 5)) * 100
dataBlu = np.ones((100, 200, 200)) * np.linspace(180, 90, 100)[:, np.newaxis, np.newaxis]
dataBlu += pg.gaussianFilter(np.random.normal(size=(200, 200)), (5, 5)) * 100

data = np.concatenate(
    (dataRed[:, :, :, np.newaxis], dataGrn[:, :, :, np.newaxis], dataBlu[:, :, :, np.newaxis]), axis=3
)


## Display the data and assign each frame a time value from 1.0 to 3.0
imv.setImage(data, xvals=np.linspace(1., 3., data.shape[0]))

'''ADDED CODE'''
imv_v = imv.getView()
pci = pg.PlotCurveItem(x=[1,50,100,150,200], y=[1,50,100,150,200])
imv_v.addItem(pci)
''''''

## Set a custom color map
colors = [
    (0, 0, 0),
    (45, 5, 61),
    (84, 42, 55),
    (150, 87, 60),
    (208, 171, 141),
    (255, 255, 255)
]
cmap = pg.ColorMap(pos=np.linspace(0.0, 1.0, 6), color=colors)
imv.setColorMap(cmap)

# Start up with an ROI
imv.ui.roiBtn.setChecked(True)
imv.roiClicked()

if __name__ == '__main__':
    pg.exec()

关于python-3.x - 在 pyqtgraph.ImageView() 上绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71342255/

相关文章:

python - 如何在 numpy 中对 cvxopt 矩阵执行运算?

python - 将两个系列合并/压缩到 ndarray 的 ndarray

python - 如何将 1d 数组转换为 3d 数组(将灰度图像转换为 rgb 格式)?

python - 在 Dialogflow 中管理意图

python - 为什么pip3安装包却无法导入呢?

python - "Replace all"之后撤消

python - 在 QGraphicsView 中显示 X 和 Y 坐标线

python - PyQt5 替代 Qtermwidget

Python - 可以暂停程序并发送命令吗?

python - Django预保存信号