python调用摄像头动态更新三维灰度分布图

我想python调用摄像头动态更新三维灰度分布图,但发现在绘制的时候,前面绘制的图像不会被擦掉,而是重叠在一起,这要怎么改呢?

from DataDisplayUI import Ui_MainWindow
from PyQt5.QtWidgets import QApplication,QMainWindow,QGridLayout
from PyQt5.QtCore import QTimer
import sys,time
import cv2
import numpy as np
import cv2 as cv

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.lines import Line2D
import matplotlib
import matplotlib.cbook as cbook

class Figure_Canvas(FigureCanvas):
    def __init__(self,parent=None,width=3.9,height=2.7,dpi=100):
        self.fig=Figure(figsize=(width,height),dpi=50)    #创建画布
        super(Figure_Canvas,self).__init__(self.fig)       #
        self.ax=self.fig.add_subplot()                 #创建子图

class ImgDisp(QMainWindow,Ui_MainWindow):
    def __init__(self,parent=None):
        super(ImgDisp,self).__init__(parent)
        self.setupUi(self)
        self.Init_Widgets()
        self.video_start()
        self.timer=QTimer()    #QTimer定时器
        self.timer.start(1000)    #timer方法,开始执行,1代表循环周期
        self.timer.timeout.connect(self.SurfUpdate)  #连接到槽UpdateImgs,循环函数体
    def Init_Widgets(self):
        self.PrepareSamples()
        self.PrepareSurfaceCanvas()
    def PrepareSamples(self):
        self.x = np.arange(-4, 4, 0.02)
        self.y = np.arange(-4, 4, 0.02)
        self.X, self.Y = np.meshgrid(self.x, self.y)
        self.z = np.sin(self.x)
        self.R = np.sqrt(self.X ** 2 + self.Y ** 2)
        self.Z = np.sin(self.R)
    def PrepareSurfaceCanvas(self):
        self.SurfFigure = Figure_Canvas()
        self.SurfFigureLayout = QGridLayout(self.SurfaceDisplayGB)
        self.SurfFigureLayout.addWidget(self.SurfFigure)
        self.SurfFigure.ax.remove()
        self.ax3d = self.SurfFigure.fig.gca(projection='3d')
    def video_start(self):
        self.cap = cv2.VideoCapture(0)  # 打开摄像头,0代表的是设备id,如果有多个摄像头,可以设置其他数值
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)  # 调节视频分辨率
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
        self.cap.set(cv2.CAP_PROP_SATURATION, 10)  # 调节曝光
    def SurfUpdate(self):
        ret, frame = self.cap.read()  # 读取摄像头,它能返回两个参数,第一个参数是bool型的ret,其值为True或False,代表有没有读到图片;第二个参数是frame,是当前截取一帧的图片
        img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # 转灰度图
        img= cv2.GaussianBlur(img, (5, 5), 0) #高斯滤波
        img_date = np.array(img)  # 每一帧存入循环数组
        # 准备数据
        sp = img_date.shape
        h = int(sp[0])
        w = int(sp[1])
        X = np.arange(0, w, 1)
        Y = np.arange(0, h, 1)
        X, Y = np.meshgrid(X, Y)
        Z = img_date
        self.Surf = self.ax3d.plot_surface(X, Y, Z, cmap='rainbow')
        self.SurfFigure.draw()

if __name__=='__main__':
    app=QApplication(sys.argv)  #创建一个应用程序对象
    ui=ImgDisp()
    ui.show()
    sys.exit(app.exec_())   #退出
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问