Files
HPI/library/matplotlib_display_image_spectral.py
tangchao0503 98cf134cca 第一次提交
1、hpi的可用代码;
2、修复了多次点击曝光后,福亮度数据错误的问题;
3、定标方式为大的蓝菲积分球的标准能量曲线,而不是基于asd的能量曲线;
2022-09-06 22:54:14 +08:00

162 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import matplotlib
matplotlib.use("Qt5Agg") # 声明使用QT5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import traceback
import time, sys
from functions import percentile_stretching
from PyQt5 import QtWidgets, QtCore, uic
import pyqtgraph as pg
from random import randint
class ArgsError(Exception):
pass
# 画图类,用于:画出采集到的光谱;调焦(影响模式调焦)
class MatplotlibSpectralViewer(FigureCanvas):
def __init__(self, xlabel=None, ylabel=None, width=5, height=4, dpi=100):
# 第一步创建一个创建Figure
self.fig = Figure(figsize=(width, height), dpi=dpi)
# 第二步在父类中激活Figure窗口
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
super(MatplotlibSpectralViewer, self).__init__(self.fig) # 此句必不可少,否则不能显示图形
# 第三步创建一个子图用于绘制图形用111表示子图编号如matlab的subplot(1,1,1)
self.axes = self.fig.add_subplot(1, 1, 1)
self.axes.set_xlabel('Wavelength (nm)')
self.axes.set_ylabel('reflectance')
self.xlabel = xlabel
self.ylabel = ylabel
self._plotref = None # 这里代表曲线
self.axes.set_ylim(0, 1.2)
# 第四步:就是画图,【可以在此类中画,也可以在其它类中画】
def plot_wrap(self, *args):
if self.xlabel is not None:
self.axes.set_xlabel(self.xlabel)
if self.ylabel is not None:
self.axes.set_ylabel(self.ylabel)
try:
if self._plotref == None:
if len(args) == 0:
raise ArgsError('传入了0个参数本函数只能传入1/2个参数')
elif len(args) == 1:
self.axes.cla()
# self.axes.set_ylim(0, 1.2)
self._plotref = self.axes.plot(list(range(len(args[0]))), args[0])[0]
self.draw()
elif len(args) == 2:
self.axes.cla()
# self.axes.set_ylim(0, 1.2)
self._plotref = self.axes.plot(args[0], args[1])[0]
self.draw()
elif len(args) > 2:
raise ArgsError('传入了大于2个参数本函数只能传入1/2个参数')
elif self._plotref is not None:
if len(args) == 0:
raise ArgsError('传入了0个参数本函数只能传入1/2个参数')
elif len(args) == 1:
self._plotref.set_data(list(range(len(args[0]))), args[0])
# 更新显示区域
self.axes.set_xlim(min(list(range(len(args[0])))), max(list(range(len(args[0])))))
self.axes.set_ylim(min(args[0]), max(args[0]))
# self.axes.set_ylim(0, 1.2)
# 绘制图像
self.draw()
elif len(args) == 2:
self._plotref.set_data(args[0], args[1])
# 更新显示区域
self.axes.set_xlim(min(args[0]), max(args[0]))
self.axes.set_ylim(min(args[1]), max(args[1]))
# self.axes.set_ylim(0, 1.2)
# 绘制图像
self.draw()
elif len(args) > 2:
raise ArgsError('传入了大于2个参数本函数只能传入1/2个参数')
except Exception:
traceback.print_exc()
# 画图类,用于:画出采集到的图像;显示帧流(光谱模式对准光纤)
class MatplotlibImageViewer(FigureCanvas):
def __init__(self, width=5, height=4, dpi=100):
# 第一步创建一个创建Figure
self.fig = Figure(figsize=(width, height), dpi=dpi)
# 第二步在父类中激活Figure窗口
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
super(MatplotlibImageViewer, self).__init__(self.fig) # 此句必不可少,否则不能显示图形
# 第三步创建一个子图用于绘制图形用111表示子图编号如matlab的subplot(1,1,1)
self.axes = self.fig.add_subplot(1, 1, 1)
self._plotref = None # 这里代表图像
# 第四步:就是画图,【可以在此类中画,也可以在其它类中画】
def setImage(self, image, lowPercentile=0, highPercentile=100):
self.axes.set_xticks([])
self.axes.set_yticks([])
if self._plotref == None:
self._plotref = self.axes.imshow(image)
self.draw()
elif self._plotref is not None:
self._plotref.set_data(percentile_stretching(image, lowPercentile, highPercentile))
self.draw()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
# self.graphWidget = QtSpectralViewer()
self.graphWidget = MatplotlibSpectralViewer(xlabel='Wavelength (nm)', ylabel='reflectance')
self.setCentralWidget(self.graphWidget)
self.x = list(range(100)) # 100 time points
self.y = [randint(0, 100) for _ in range(100)] # 100 data points
# self.graphWidget.setBackground('w')
pen = pg.mkPen(color=(255, 0, 0))
self.graphWidget.plot_wrap(self.x, self.y)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update_plot_data)
self.timer.start(10)
def update_plot_data(self):
self.x = self.x[1:] # Remove the first y element.
self.x.append(self.x[-1] + 1) # Add a new value 1 higher than the last.
self.y = self.y[1:] # Remove the first
self.y.append(randint(0, 100)) # Add a new random value.
# self.data_line.setData(self.x, self.y) # Update the data.
self.graphWidget.plot_wrap(self.x, self.y)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())