热图转换成温度矩阵
對溫度矩陣轉圖片進行補充,之前被問到了,回答時想的簡單了。。。
# -- coding: utf-8 -- import cv2 import color_table import numpy as np# 熱圖轉換成溫度矩陣 # 思路:將圖片讀取出來,根據color_table中的映射關系將找到原始的溫度值 def read_txt(txt_path, height=288, width=384):with open(txt_path, 'r') as f_r:temp = f_r.read().split()data = np.array(temp).reshape((height, width)).astype(np.float)min_tmp = np.min(data)max_tmp = np.max(data)return min_tmp, max_tmpdef png_to_matrix(path, min_tmp, max_tmp, save_path):img = cv2.imread(path)temp = color_table.get_color_table(11)tmp_matrix = []for i in range(len(img)):for j in range(len(img[i])):for x in range(len(temp)):# temp2 = (img[i][j] == temp[x][:3])y, q = img[i][j], np.flipud(temp[x][:3])if list(img[i][j]) == list(np.flipud(temp[x][:3])):temp1 = x / 255 * (max_tmp - min_tmp) + min_tmptmp_matrix.append(temp1)continuenp.reshape(tmp_matrix, (288, 384))tmp_matrix = np.around(tmp_matrix, 2)np.savetxt(save_path, tmp_matrix)if __name__ == '__main__':path = '2.png'min_tmp, max_tmp = read_txt('220107105114_1_value.txt')save_path = 'tmp_matrix.txt'png_to_matrix(path, min_tmp, max_tmp, save_path) a = np.loadtxt('tmp_matrix.txt', delimiter=' ') print(a)純暴力計算,時間復雜度O(N^2)
由于將溫度矩陣轉為熱圖需要將浮點數轉換為整數,故再將熱圖轉換為溫度矩陣時會存在偏差。
對比一下轉換前后的溫度值
總結
- 上一篇: Day25-软件工程基础实验作业
- 下一篇: QQ密码忘记怎么办 教你找回QQ密码