首页 > 编程语言 >Python读取温度矩阵数据

Python读取温度矩阵数据

时间:2023-03-01 13:44:24浏览次数:40  
标签:读取 temp Python 矩阵 file path 温度 matrix

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

标签:读取,temp,Python,矩阵,file,path,温度,matrix
From: https://www.cnblogs.com/d442130165/p/17167884.html

相关文章

  • 爬虫代码中Python中random模块的方法整理
    1、random产生0~1之间的随机小数。2、randrange在前两个参数范围内产生一个数字。不包括第二个参数,第三个参数是步长。3、randint在两个参数之间产生一个数字,左右两个参数都......
  • 爬虫代码中Python中time模块的方法整理
    1、时间戳time.time当前时间。2、time.sleep程序暂停三秒钟。3、time.ctime当前时间。年月日时分秒。4、time.localtime()将时间戳转换成元组。显示当前时间的详细信息。tim......
  • Python解析器如何在爬虫代码里搜索模块位置
    1、先找到当前目录。2、如果不在当前目录中,Python将在shell变量PYTHONPATH下搜索每个目录。3、如果找不到,Python会查看默认路径。在UNIX下,默认路径一般为/user/local/lib/py......
  • 利用python操作数据库MySQL
    一、python操作MySQL的库(包)1.PythonDatabasAPI   Python操作数据库的标准接口为PythonDatabasAPISpecificationV2.0,其定义了在python中操作数据库的方法......
  • 利用python将MySQL数据导出到excel中
    涉及到的相关库:   pymysql、   xlwt库函数(将MySQL一个数据表导出到excel文件的一个表)文件exportexcel.py内容:defexport_to_excel(worksheet,cursor......
  • Python - Numpy 学习笔记
     #python-Numpylearningimportnumpyasnp#---Numpy学习笔记---(第四章)---#切片,浅拷贝a=np.arange(10)print(a)b=a[2:7]print(b)a[5]=1000print(a)p......
  • sql ytd 附python 实现方式
    ytd释义YTD分析属于同比分析类,其特点在于对比汇总值,即从年初第一日值一直至今的值累加。作用在于分析企业中长期的经营绩效。做法假定:有一张销量明细表date仓库s......
  • Python批量读取HDF多波段栅格数据并绘制像元直方图
      本文介绍基于Python语言gdal模块,实现多波段HDF栅格图像文件的读取、处理与像元值可视化(直方图绘制)等操作。  另外,基于gdal等模块读取.tif格式栅格图层文件的方法可......
  • 力扣---2373. 矩阵中的局部最大值
    给你一个大小为nxn的整数矩阵grid。生成一个大小为 (n-2)x(n-2)的整数矩阵 maxLocal,并满足:maxLocal[i][j]等于grid中以i+1行和j+1列为......
  • Python-tomorrow应用于UI自动化的简单使用
    一、简介tomorrow属于第三方的一个模块,使用threads方法作为装饰器去修饰一个普通的函数,使其可以达到并发效果。使用起来非常简单。 二、安装pipinstalltomorrow......