Python读取温度矩阵数据

import cv2, struct
import numpy as np
import matplotlib.pyplot as plt

class TempMatrix():
    def __init__(self):
        pass

    def read_temp_matrix_img(self, file_path):
        """
        brief
            读取温度矩阵图像
        param  file_path  : 文件路径
        return temp_matrix: 温度矩阵
        """
        # 读取温度矩阵
        with open(file_path, 'rb') as f:
            # 定位文件版本地址
            f.seek(-20, 2)                                      # 定位文件版本地址
            file_version_off = struct.unpack('I', f.read(4))[0] # 解析文件版本偏移

            # 读取温度矩阵宽高
            f.seek(file_version_off + 2)                                  # 定位温度矩阵宽高
            temp_matrix_w, temp_matrix_h = struct.unpack('2H', f.read(4)) # 读取温度矩阵宽高

            # 读取文件温度矩阵
            f.seek(14, 1)                                                                      # 定位温度矩阵数据
            temp_matrix_size = temp_matrix_h * temp_matrix_w                                   # 计算温度矩阵大小
            temp_matrix_lens = temp_matrix_size * struct.calcsize('f')                         # 计算温度矩阵长度
            temp_matrix_data = struct.unpack(f'{temp_matrix_size}f', f.read(temp_matrix_lens)) # 读取文件矩阵数据

            # 转换温度矩阵形状
            temp_matrix = np.reshape(np.array(temp_matrix_data, dtype=np.float32), (temp_matrix_h, temp_matrix_w))

        # 设置温度矩阵
        self.temp_matrix = temp_matrix

        return temp_matrix

    def save_temp_matrix_img(self, save_path):
        """
        brief
            保存温度矩阵图像
        param  save_path: 保存路径
        return none     : 无返回值
        """
        # 转换数据格式
        temp_matrix = self.temp_matrix
        cv2.normalize(temp_matrix, temp_matrix, 0, 255, cv2.NORM_MINMAX)

        # 保存温度矩阵
        cv2.imwrite(save_path, temp_matrix)

    def show_temp_matrix_img(self):
        '''
        brief
            显示温度矩阵图像
        param  none: 无参数值
        return none: 无返回值
        '''
        # 转换数据格式
        temp_matrix = self.temp_matrix
        cv2.normalize(temp_matrix, temp_matrix, 0, 255, cv2.NORM_MINMAX)

        # 显示温度矩阵
        print('Temperature Matrix (h, w):', temp_matrix.shape)
        plt.imshow(temp_matrix, interpolation='None',cmap=plt.cm.hot, origin='upper')
        plt.colorbar()
        plt.xticks(())
        plt.yticks(())
        plt.show()

    def read_temp_matrix_raw(self, file_path, temp_matrix_w, temp_matrix_h):
        """
        brief
            读取温度矩阵数据
        param  file_path    : 文件路径
        param  temp_matrix_w: 矩阵宽度
        param  temp_matrix_h: 矩阵高度
        return temp_matrix  : 温度矩阵
        """
        # 读取温度矩阵
        temp_matrix = np.fromfile(file_path, dtype=np.float32)
        temp_matrix.shape = temp_matrix_h, temp_matrix_w

        # 设置温度矩阵
        self.temp_matrix = temp_matrix

        return temp_matrix
    
    def save_temp_matrix_raw(self, save_path):
        """
        brief
            保存温度矩阵数据
        param  save_path: 保存路径
        return none     : 无返回值
        """
        # 保存温度矩阵
        self.temp_matrix.tofile(save_path)

    def read_temp_matrix_txt(self, file_path):
        """
        brief
            读取温度矩阵文件
        param  file_path  : 文件路径
        return temp_matrix: 温度矩阵
        """
        # 读取温度矩阵
        self.temp_matrix = np.loadtxt(file_path, delimiter=',')

        return self.temp_matrix

    def save_temp_matrix_txt(self, save_path):
        """
        brief
            保存温度矩阵文件
        param  save_path: 保存路径
        return none     : 无返回值
        """
        # 保存温度矩阵
        np.savetxt(save_path, self.temp_matrix, delimiter=',', fmt='%f')

if __name__ == '__main__':
    # 创建温度矩阵实例
    # file_path = 'HeatMap_2023-02-03_09-38-11_HeatMap'
    # file_path = 'HeatMap_2023-02-17_15-36-58_HeatMap'
    file_path = 'HeatMap_2023-02-17_15-46-23_HeatMap'
    temp_matrix = TempMatrix()

    # 读取温度矩阵图像
    temp_matrix.read_temp_matrix_img('./data/img/' + file_path + '.jpg')

    # 保存温度矩阵图像
    temp_matrix.save_temp_matrix_img('./output/temp_matrix.jpg')

    # 读取温度矩阵数据
    temp_matrix.read_temp_matrix_raw('./data/raw/' + file_path + '.raw', 256, 192)

    # 保存温度矩阵数据
    temp_matrix.save_temp_matrix_raw('./output/temp_matrix.raw')

    # 读取温度矩阵文本
    temp_matrix.read_temp_matrix_txt('./data/txt/' + file_path + '.txt')

    # 保存温度矩阵文本
    temp_matrix.save_temp_matrix_txt('./output/temp_matrix.txt')

    # 显示温度矩阵图像
    temp_matrix.show_temp_matrix_img()

 

参考地址:

https://blog.csdn.net/zsh501736479/article/details/117196324
https://www.zhihu.com/question/63686736?sort=created
https://www.zhangshilong.cn/work/110410.html
https://www.cnpython.com/qa/112252
https://www.jb51.cc/python/3568815.html
https://www.freesion.com/article/6196525797/
https://www.zkxjob.com/45940
https://blog.csdn.net/qq_42183962/article/details/121930356
https://blog.csdn.net/weixin_61057398/article/details/128314637
https://blog.csdn.net/weixin_42663139/article/details/124218934
https://www.xrvps.com/46269.html
https://www.jianshu.com/p/ebb041a4e104
https://blog.csdn.net/sinat_26472165/article/details/85342766
https://zhuanlan.zhihu.com/p/461880996
https://blog.csdn.net/weixin_39657978/article/details/120747469
https://www.cnblogs.com/ice-daigua/archive/2012/11/16/2772674.html
https://www.zhiu.cn/56147.html
posted @ 2023-03-01 13:38  盛夏夜  阅读(159)  评论(0编辑  收藏  举报