matplotlib修补程序在实时热图像上消失

2024-06-28 11:45:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个RPi设备,它应该能够查看热图像,然后一个功能是放置一个矩形来突出显示图像上最热的点。为此,我使用的是MLX90640红外摄像机

我试图使用matplotlib面片功能将高亮矩形放置在图像上,但我遇到了一个问题,即矩形将出现一小段时间,然后消失。我相信当图像“刷新”到新的帧时可能需要做些什么,但是我不知道如何解决这个问题

下面是代码,我已经评论过了!!!在应该处理补丁的行上。谢谢


def liveImage():
    i2c = busio.I2C(board.SCL, board.SDA, frequency=400000)                 # setup I2C
    mlx = adafruit_mlx90640.MLX90640(i2c)                                   # begin MLX90640 with I2C comm
    mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_8_HZ           # set refresh rate
    mlx_shape = (24,32)                                                     # mlx90640 shape

    mlx_interp_val = 10                                                     # interpolate # on each dimension
    mlx_interp_shape = (mlx_shape[0]*mlx_interp_val,
                        mlx_shape[1]*mlx_interp_val)                        # new shape

    fig = plt.figure(figsize=(12,9))                                        # start figure
    ax = fig.add_subplot(111)                                               # add subplot
    fig.subplots_adjust(0.05,0.05,0.95,0.95)                                # get rid of unnecessary padding
    therm1 = ax.imshow(np.zeros(mlx_interp_shape),interpolation='none',
                       cmap=plt.cm.bwr,vmin=25,vmax=45)                     # preemptive image
    cbar = fig.colorbar(therm1)                                             # setup colorbar
    cbar.set_label('Temperature [$^{\circ}$C]',fontsize=14)                 # colorbar label

    fig.canvas.draw()                                                       # draw figure to copy background
    ax_background = fig.canvas.copy_from_bbox(ax.bbox)                      # copy background

    rect = patches.Rectangle((0,0),50,50,fc='None',ec='r')                  # !!!define highlight rectange patch!!!
    ax.add_patch(rect)                                                      # !!!init insert rectangle onto image!!!
    
    fig.show()                                                              # show the figure before blitting
    frame = np.zeros(mlx_shape[0]*mlx_shape[1])                             # 768 pts
            

    def plotUpdate():
        
        fig.canvas.restore_region(ax_background)                            # restore background
        mlx.getFrame(frame)                                                 # read mlx90640
        data_array1 = np.fliplr(np.reshape(frame,mlx_shape))                # reshape, flip data
        data_array = np.flipud(data_array1)                                 # flip data
        data_array = ndimage.zoom(data_array,mlx_interp_val)                # interpolate
        therm1.set_array(data_array)                                        # set data
        therm1.set_clim(vmin=np.min(data_array),vmax=np.max(data_array))    # set bounds
        cbar.on_mappable_changed(therm1)                                    # update colorbar range
        
        ax.draw_artist(therm1)                                              # draw new thermal image
        fig.canvas.blit(ax.bbox)                                            # draw background
        fig.canvas.flush_events()                                           # show the new image             

        highlightPoint = np.where(data_array==np.amax(data_array))          # !!!gets array coords of areas to be highlighted!!!
        highlightCoords = list(zip(highlightPoint[0],highlightPoint[1]))    # !!!formats highlightPoint into (x,y)!!!
        for coords in highlightCoords:                                      # !!!
                rect.set_xy(coords)                                         # !!!

        return
    
    
                    
    t_array = []
    while True:
        
        try:
            plotUpdate() # update plot
        except:
            continue


Tags: 图像datanpfigaxarraycanvasbackground