237 lines
7.8 KiB
Python
237 lines
7.8 KiB
Python
|
# -*- coding: UTF-8 -*-
|
|||
|
"""
|
|||
|
@Project:__init__.py
|
|||
|
@File:AHVToPolsarpro.py
|
|||
|
@Function:全极化影像转成polsarpro格式T3数据
|
|||
|
@Contact:
|
|||
|
@Author:SHJ
|
|||
|
@Date:2021/9/18 16:44
|
|||
|
@Version:1.0.0
|
|||
|
"""
|
|||
|
import os
|
|||
|
import numpy as np
|
|||
|
import glob
|
|||
|
import struct
|
|||
|
from tool.algorithm.image.ImageHandle import ImageHandler
|
|||
|
|
|||
|
|
|||
|
class AHVToPolsarpro:
|
|||
|
"""
|
|||
|
全极化影像转换为bin格式T3矩阵,支持polsarpro处理
|
|||
|
"""
|
|||
|
def __init__(self):
|
|||
|
pass
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def __ahv_to_s2(ahv_dir):
|
|||
|
"""
|
|||
|
全极化影像转S2矩阵
|
|||
|
:param ahv_dir: 全极化影像文件夹路径
|
|||
|
:return: 极化散射矩阵S2
|
|||
|
"""
|
|||
|
in_tif_paths = list(glob.glob(os.path.join(ahv_dir, '*.tif')))
|
|||
|
|
|||
|
if in_tif_paths == []:
|
|||
|
in_tif_paths = list(glob.glob(os.path.join(ahv_dir, '*.tiff')))
|
|||
|
|
|||
|
flag_list = [0, 0, 0, 0]
|
|||
|
for in_tif_path in in_tif_paths:
|
|||
|
|
|||
|
# 读取原始SAR影像
|
|||
|
proj, geotrans, data = ImageHandler.read_img(in_tif_path)
|
|||
|
|
|||
|
# 获取极化类型
|
|||
|
if 'HH' in os.path.basename(in_tif_path):
|
|||
|
data_real = data[0, :, :]
|
|||
|
data_imag = data[1, :, :]
|
|||
|
s11 = data_real + 1j * data_imag
|
|||
|
flag_list[0] = 1
|
|||
|
elif 'HV' in os.path.basename(in_tif_path):
|
|||
|
data_real = data[0, :, :]
|
|||
|
data_imag = data[1, :, :]
|
|||
|
s12 = data_real + 1j * data_imag
|
|||
|
flag_list[1] = 1
|
|||
|
elif 'VH' in os.path.basename(in_tif_path):
|
|||
|
data_real = data[0, :, :]
|
|||
|
data_imag = data[1, :, :]
|
|||
|
s21 = data_real + 1j * data_imag
|
|||
|
flag_list[2] = 1
|
|||
|
elif 'VV' in os.path.basename(in_tif_path):
|
|||
|
data_real = data[0, :, :]
|
|||
|
data_imag = data[1, :, :]
|
|||
|
s22 = data_real + 1j * data_imag
|
|||
|
flag_list[3] = 1
|
|||
|
else:
|
|||
|
continue
|
|||
|
if not flag_list == [1, 1, 1, 1]:
|
|||
|
raise Exception('tif of HH or HV or VH or VV is not in path :%s', ahv_dir)
|
|||
|
return s11, s12, s21, s22
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def __s2_to_t3(s11, s12, s21, s22):
|
|||
|
"""
|
|||
|
S2矩阵转T3矩阵
|
|||
|
:param s11: HH极化数据
|
|||
|
:param s12: HV极化数据
|
|||
|
:param s21: VH极化数据
|
|||
|
:param s22: VV极化数据
|
|||
|
:return: 极化相干矩阵T3
|
|||
|
"""
|
|||
|
HH = s11
|
|||
|
HV = s12
|
|||
|
VH = s21
|
|||
|
VV = s22
|
|||
|
|
|||
|
t11 = (np.abs(HH + VV)) ** 2 / 2
|
|||
|
t12 = (HH + VV) * np.conj(HH - VV) / 2
|
|||
|
t13 = (HH + VV) * np.conj(HV + VH)
|
|||
|
|
|||
|
t21 = (HH - VV) * np.conj(HH + VV) / 2
|
|||
|
t22 = np.abs(HH - VV) ** 2 / 2
|
|||
|
t23 = (HH - VV) * np.conj(HV + VH)
|
|||
|
|
|||
|
t31 = (HV + VH) * np.conj(HH + VV)
|
|||
|
t32 = (HV + VH) * np.conj(HH - VV)
|
|||
|
t33 = 2 * np.abs(HV + VH) ** 2
|
|||
|
return t11, t12, t13, t21, t22, t23, t31, t32, t33
|
|||
|
|
|||
|
def __t3_to_polsarpro_t3(self, out_dir, t11, t12, t13, t22, t23, t33):
|
|||
|
"""
|
|||
|
T3矩阵转bin格式,支持 polsarpro处理
|
|||
|
:param out_dir: 输出的文件夹路径
|
|||
|
:param t11:
|
|||
|
:param t12:
|
|||
|
:param t13:
|
|||
|
:param t22:
|
|||
|
:param t23:
|
|||
|
:param t33:
|
|||
|
:return: bin格式矩阵T3和头文件
|
|||
|
"""
|
|||
|
if not os.path.exists(out_dir):
|
|||
|
os.makedirs(out_dir)
|
|||
|
|
|||
|
rows = t11.shape[0]
|
|||
|
cols = t11.shape[1]
|
|||
|
bins_dict = {'T11.bin': t11, 'T12_real.bin': t12.real, 'T12_imag.bin': t12.imag,
|
|||
|
'T13_real.bin': t13.real, 'T13_imag.bin': t13.imag,'T22.bin': t22,
|
|||
|
'T23_real.bin': t23.real, 'T23_imag.bin': t23.imag, 'T33.bin': t33}
|
|||
|
|
|||
|
for name, data in bins_dict.items():
|
|||
|
bin_path = os.path.join(out_dir, name)
|
|||
|
self.__write_img_bin(data, bin_path)
|
|||
|
out_hdr_path = bin_path+'.hdr'
|
|||
|
self.__write_bin_hdr(out_hdr_path, bin_path, rows, cols)
|
|||
|
|
|||
|
self.__write_config_file(out_dir, rows, cols)
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def __write_img_bin(im, file_path):
|
|||
|
"""
|
|||
|
写入影像到bin文件中,保存为float32类型
|
|||
|
:param im : 影像矩阵数据,暂支持单通道影像数据
|
|||
|
:param file_path: bin文件的完整路径
|
|||
|
"""
|
|||
|
with open(file_path, 'wb') as f:
|
|||
|
rows =im.shape[0]
|
|||
|
cols = im.shape[1]
|
|||
|
for row in range(rows):
|
|||
|
im_bin = struct.pack("f" * cols, *np.reshape(im[row, :], (cols, 1), order='F'))
|
|||
|
f.write(im_bin)
|
|||
|
# # if 是否为复数形式
|
|||
|
# #方案1:
|
|||
|
# for row in range(rows):
|
|||
|
# im_bin = struct.pack("f"*cols, *np.reshape(im[row, :].real, (cols, 1), order='F'))
|
|||
|
# f.write(im_bin)
|
|||
|
# for row in range(rows):
|
|||
|
# im_bin = struct.pack("f" * cols, *np.reshape(im[row, :].imag, (cols, 1), order='F'))
|
|||
|
# f.write(im_bin)
|
|||
|
# #方案2
|
|||
|
# for row in range(rows):
|
|||
|
# im_bin = struct.pack("f" * cols, *np.reshape(im[row, :].real, (cols, 1), order='F'))
|
|||
|
# f.write(im_bin)
|
|||
|
# im_bin = struct.pack("f" * cols, *np.reshape(im[row, :].imag, (cols, 1), order='F'))
|
|||
|
# f.write(im_bin)
|
|||
|
# #else: 非复数
|
|||
|
# for row in range(rows):
|
|||
|
# im_bin = struct.pack("f" * cols, *np.reshape(im[row, :], (cols, 1), order='F'))
|
|||
|
# f.write(im_bin)
|
|||
|
f.close()
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def __write_bin_hdr(out_hdr_path, bin_path, rows, cols):
|
|||
|
"""
|
|||
|
写入影像的头文件
|
|||
|
:param out_hdr_path : 头文件的路径
|
|||
|
:param bin_path: bin文件的路径
|
|||
|
:param rows: 影像的行数
|
|||
|
:param cols: 影像的列数
|
|||
|
"""
|
|||
|
h1 = 'Envi'
|
|||
|
h2 = 'description = {'
|
|||
|
h3 = 'File Imported into ENVI. }'
|
|||
|
h4 = 'samples = ' + str(cols) # 列
|
|||
|
h5 = 'lines = ' + str(rows) # 行
|
|||
|
h6 = 'bands = 1 ' # 波段数
|
|||
|
h7 = 'header offset = 0'
|
|||
|
h8 = 'file type = ENVI Standard'
|
|||
|
h9 = 'data type = 4' # 数据格式
|
|||
|
h10 = 'interleave = bsq' # 存储格式
|
|||
|
h11 = 'sensor type = Unknown'
|
|||
|
h12 = 'byte order = 0'
|
|||
|
h13 = 'band names = {'
|
|||
|
h14 = bin_path + '}'
|
|||
|
h = [h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12, h13, h14]
|
|||
|
doc = open(out_hdr_path, 'w')
|
|||
|
for i in range(0, 14):
|
|||
|
print(h[i], end='', file=doc)
|
|||
|
print('\n', end='', file=doc)
|
|||
|
doc.close()
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def __write_config_file(out_config_dir, rows, cols):
|
|||
|
"""
|
|||
|
写入polsarpro配置文件
|
|||
|
:param out_config_dir : 配置文件路径
|
|||
|
:param rows: 影像的行数
|
|||
|
:param cols: 影像的列数
|
|||
|
"""
|
|||
|
h1 = 'Nrow'
|
|||
|
h2 = str(rows)
|
|||
|
h3 = '---------'
|
|||
|
h4 = 'Ncol'
|
|||
|
h5 = str(cols)
|
|||
|
h6 = '---------'
|
|||
|
h7 = 'PolarCase'
|
|||
|
h8 = 'monostatic'
|
|||
|
h9 = '---------'
|
|||
|
h10 = 'PolarType'
|
|||
|
h11 = 'full'
|
|||
|
h = [h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11]
|
|||
|
|
|||
|
out_config_path = os.path.join(out_config_dir, 'config.txt')
|
|||
|
doc = open(out_config_path, 'w')
|
|||
|
for i in range(0, 11):
|
|||
|
print(h[i], end='', file=doc)
|
|||
|
print('\n', end='', file=doc)
|
|||
|
doc.close()
|
|||
|
|
|||
|
def ahv_to_polsarpro_t3(self, out_file_dir, in_ahv_dir):
|
|||
|
|
|||
|
s11, s12, s21, s22 = self.__ahv_to_s2(in_ahv_dir)
|
|||
|
|
|||
|
t11, t12, t13, t21, t22, t23, t31, t32, t33 = self.__s2_to_t3(s11, s12, s21, s22)
|
|||
|
|
|||
|
self.__t3_to_polsarpro_t3(out_file_dir, t11, t12, t13, t22, t23, t33)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
# atp = AHVToPolsarpro()
|
|||
|
# ahv_path = 'D:\\DATA\\GAOFEN3\\2-GF3_MYN_WAV_020086_E107.2_N27.6_20200603_L1A_AHV_L10004843087\\'
|
|||
|
# # ahv_path = 'D:\\DATA\\GAOFEN3\\2598957_Paris\\'
|
|||
|
# out_file_path = 'D:\\bintest0923\\'
|
|||
|
# atp.ahv_to_polsarpro_t3(out_file_path, ahv_path)
|
|||
|
|
|||
|
print("done")
|