86 lines
3.1 KiB
Python
86 lines
3.1 KiB
Python
|
import os
|
|||
|
import glob
|
|||
|
import numpy as np
|
|||
|
import struct
|
|||
|
from PIL import Image
|
|||
|
from tool.algorithm.ml.machineLearning import MachineLeaning as ml
|
|||
|
|
|||
|
|
|||
|
def read_bin_to_img(bin_path):
|
|||
|
"""
|
|||
|
读取bin格式二进制数据,输出为矩阵
|
|||
|
:param bin_path : bin文件的路径,包含.bin,.config
|
|||
|
:return : 矩阵信息
|
|||
|
"""
|
|||
|
(bin_dir, bin_name) = os.path.split(bin_path)
|
|||
|
config_path = os.path.join(bin_dir, 'config.txt')
|
|||
|
config = open(config_path, 'r').read().split('\n', -1)
|
|||
|
rows = int(config[1])
|
|||
|
cols = int(config[4])
|
|||
|
|
|||
|
bin_file = open(bin_path, 'rb') # 打开二进制文件
|
|||
|
size = os.path.getsize(bin_path) # 获得文件大小
|
|||
|
if size < rows * cols * 4:
|
|||
|
raise Exception(
|
|||
|
'bin size less than rows*cols*4! size:',
|
|||
|
size,
|
|||
|
'byte, rows:',
|
|||
|
rows,
|
|||
|
'cols:',
|
|||
|
cols)
|
|||
|
|
|||
|
img = np.zeros([rows, cols], dtype=np.float32)
|
|||
|
for row in range(rows):
|
|||
|
data = bin_file.read(4 * cols) # 每次读取一行的二进制数据
|
|||
|
row_data = struct.unpack('f' * cols, data) # 转为一行float数据
|
|||
|
img[row, :] = row_data
|
|||
|
bin_file.close()
|
|||
|
return img
|
|||
|
|
|||
|
def write_bin_to_tif(out_tif_dir, bin_dir):
|
|||
|
"""
|
|||
|
读取H-A-Alpha分解二进制数据,输出为矩阵格式的字典
|
|||
|
:param out_tif_dir : tif的输出路径
|
|||
|
:param bin_dir : 二进制数据的目录,包含.bin,.config
|
|||
|
:return out_tif_path: 生成tif的路径字典
|
|||
|
"""
|
|||
|
bin_paths = list(glob.glob(os.path.join(bin_dir, '*.bin')))
|
|||
|
out_tif_path = {}
|
|||
|
for in_path in bin_paths:
|
|||
|
name = os.path.split(in_path)[1].split('.')[0]
|
|||
|
out_path = os.path.join(out_tif_dir, name + '.tif')
|
|||
|
out_tif_path.update({name: out_path})
|
|||
|
if os.path.exists(os.path.split(out_path)[0]) is False:
|
|||
|
os.makedirs(os.path.split(out_path)[0])
|
|||
|
img_array = read_bin_to_img(in_path)
|
|||
|
img_array[np.isnan(img_array)] = 0 # 异常值填充为0
|
|||
|
img_array = ml.standardization(img_array) # 数据标准化到[0,1]
|
|||
|
out_image = Image.fromarray(img_array)
|
|||
|
out_image.save(out_path)
|
|||
|
return out_tif_path
|
|||
|
|
|||
|
def write_bin_to_tif_soil(out_tif_dir, bin_dir):
|
|||
|
"""
|
|||
|
读取H-A-Alpha分解二进制数据,输出为矩阵格式的字典
|
|||
|
:param out_tif_dir : tif的输出路径
|
|||
|
:param bin_dir : 二进制数据的目录,包含.bin,.config
|
|||
|
:return out_tif_path: 生成tif的路径字典
|
|||
|
"""
|
|||
|
bin_paths = list(glob.glob(os.path.join(bin_dir, '*.bin')))
|
|||
|
out_tif_path = {}
|
|||
|
for in_path in bin_paths:
|
|||
|
name = os.path.split(in_path)[1].split('.')[0]
|
|||
|
out_path = os.path.join(out_tif_dir, name + '.tif')
|
|||
|
out_tif_path.update({name: out_path})
|
|||
|
if os.path.exists(os.path.split(out_path)[0]) is False:
|
|||
|
os.makedirs(os.path.split(out_path)[0])
|
|||
|
img_array = read_bin_to_img(in_path)
|
|||
|
img_array[np.isnan(img_array)] = 0 # 异常值填充为0
|
|||
|
# img_array = ml.standardization(img_array) # 数据标准化到[0,1]
|
|||
|
out_image = Image.fromarray(img_array)
|
|||
|
out_image.save(out_path)
|
|||
|
return out_tif_path
|
|||
|
|
|||
|
|
|||
|
|