Python读取温度矩阵数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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 @   盛夏夜  阅读(179)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2016-03-01 [国嵌攻略][091][TCP网络程序设计]
2016-03-01 [国嵌攻略][090][linux网络编程模型]
2016-03-01 [国嵌攻略][089][网络协议分析]
2016-03-01 [国嵌攻略][088][多线程同步]
点击右上角即可分享
微信分享提示