v2.2.1add:能够通过光谱bin1空间bin1的定标文件生成光谱bin2空间bin1的定标文件

This commit is contained in:
tangchao0503
2023-07-28 11:20:31 +08:00
parent 32ccb25a0e
commit 0936506e37
5 changed files with 619 additions and 256 deletions

View File

@ -14,6 +14,7 @@ from osgeo import gdal
from scipy.interpolate import interp1d
import spectral
import time
import copy
from PyQt5.QtWidgets import QMainWindow, QFileDialog, QApplication
from PyQt5.QtCore import Qt
@ -25,6 +26,7 @@ class EnterWindow(QMainWindow, Ui_MainWindow):
self.setupUi(self)
self.radiance_calibration_object = RadianceCalibration()
self.changeBinForCorning_object = ChangeBinForCorning()
# self.setWindowState(Qt.WindowMaximized) # 初始化时就最大化窗口
@ -32,6 +34,9 @@ class EnterWindow(QMainWindow, Ui_MainWindow):
self.dn_bt.clicked.connect(self.select_dn)
self.dark_bt.clicked.connect(self.select_dark)
self.out_file_bt.clicked.connect(self.select_out_file)
self.calfile_bin1_bt.clicked.connect(self.select_calfile_changebin)
self.out_calfile_bt.clicked.connect(self.select_out_file_changebin)
self.operate_bt.clicked.connect(self.operate)
self.dark_checkBox.stateChanged.connect(self.disabledDark)
@ -41,6 +46,14 @@ class EnterWindow(QMainWindow, Ui_MainWindow):
self.tmp_dark_file_path = None # 用于保存当前路径,下次打开就默认在此路径下
self.tmp_out_file_path = None # 用于保存当前路径,下次打开就默认在此路径下
self.spatialbin_comboBox.addItem("1")
# self.spatialbin_comboBox.addItem("2")
# self.spectral_comboBox.addItem("1")
self.spectral_comboBox.addItem("2")
self.change_bin_operate_bt.clicked.connect(self.operate_changebin)
def select_rad(self):
if self.tmp_rad_file_path == None:
rad_file_path = QFileDialog.getOpenFileName(self, '选择asd辐亮度文件', os.path.dirname(__file__))[0]
@ -93,12 +106,29 @@ class EnterWindow(QMainWindow, Ui_MainWindow):
self.radiance_calibration_object.out_file_path = out_file_path
self.out_file_le.setText(out_file_path)
def select_calfile_changebin(self):
tmp = QFileDialog.getExistingDirectory(self, 'bin1定标文件夹', os.path.dirname(__file__))
self.changeBinForCorning_object.calfile_path = tmp
self.calfile_bin1_le.setText(tmp)
def select_out_file_changebin(self):
tmp = QFileDialog.getExistingDirectory(self, '选择输出路径', os.path.dirname(__file__))
self.changeBinForCorning_object.out_file_path = tmp
self.out_calfile_changebin_le.setText(tmp)
def operate(self):
if(self.dark_checkBox.isChecked()):
self.radiance_calibration_object.operate()
else:
self.radiance_calibration_object.operate_without_dark()
def operate_changebin(self):
self.changeBinForCorning_object.spatialbin = int(self.spatialbin_comboBox.currentText())
self.changeBinForCorning_object.spectralbin = int(self.spectral_comboBox.currentText())
self.changeBinForCorning_object.changebin()
def disabledDark(self, isEnable):
self.dark_le.setEnabled(isEnable)
self.dark_bt.setEnabled(isEnable)
@ -284,6 +314,115 @@ class RadianceCalibration():
traceback.print_exc()
class ChangeBinForCorning():
def __init__(self):
self.calfile_path = None
self.out_file_path = None
self.spatialbin = None
self.spectralbin = None
def get_hdr_filename(self, file_path):
return os.path.splitext(file_path)[0] + ".hdr"
def get_hdr_filename_with_bip(self, file_path):
return os.path.splitext(file_path)[0] + ".bip.hdr"
def write_img(self, dst_filename, data):
format = "ENVI"
driver = gdal.GetDriverByName(format)
RasterXSize = data.shape[2] # 遥感影像的sample列数
RasterYSize = data.shape[1] # 遥感影像的line行数
band = data.shape[0]
# driver.Create()函数中RasterXSize代表影像的sample列数RasterYSize代表影像的line行数
dst_ds = driver.Create(dst_filename, RasterXSize, RasterYSize, band, gdal.GDT_Float64,
options=["INTERLEAVE=BIP"])
for i in range(band):
dst_ds.GetRasterBand(i + 1).WriteArray(data[i, :, :]) # gdal的band从1开始所以dst_ds.GetRasterBand(i+1)
dst_ds = None
def write_fields_to_hdrfile(self, fields, hdr_file):
header_tmp = spectral.envi.read_envi_header(hdr_file)
fields['generated time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
with open(hdr_file, "a", encoding='utf-8') as f:
for key in fields.keys():
if key in header_tmp or key == "description":
continue
if type(fields[key]) == list:
f.write(key + " = {" + ", ".join(fields[key]) + "}\n")
else:
f.write(key + " = " + fields[key] + "\n")
def process_hdr(self, hdr_file_path, envi_header_dict):
self.write_fields_to_hdrfile(envi_header_dict, self.get_hdr_filename(hdr_file_path))
shutil.copyfile(self.get_hdr_filename(hdr_file_path), self.get_hdr_filename_with_bip(hdr_file_path))
os.remove(self.get_hdr_filename(hdr_file_path))
def changebin(self):
gain_name_in = os.path.join(self.calfile_path, "gain.bip")
offset_name_in = os.path.join(self.calfile_path, "offset.bip")
img_proj, img_geotrans, img_data = ImageReaderWriter.read_img(gain_name_in)
img_proj1, img_geotrans1, img_dark = ImageReaderWriter.read_img(offset_name_in)
in_hdr_dict = spectral.envi.read_envi_header(self.get_hdr_filename_with_bip(gain_name_in))
if in_hdr_dict["spectral binning"] != "1" and in_hdr_dict["sample binning"] != "1":
print("输入定标文件不是bin1。")
return
if self.spatialbin == 1 and self.spectralbin == 1:
print("Bin1 is not need to convert.")
return
if self.spatialbin == 1 and self.spectralbin == 2:
samples = int(in_hdr_dict["samples"])
if int(in_hdr_dict["bands"]) % 2 == 0:
bands = int(int(in_hdr_dict["bands"]) / 2)
else:
print("Bands is not divisible by 2.")
return
out_hdr_dict = copy.deepcopy(in_hdr_dict)
out_hdr_dict["bands"] = str(bands)
out_hdr_dict["spectral binning"] = str(2)
wave = []
gain = np.empty((bands, 1, samples))
dark = np.empty((bands, 1, samples))
for i in range(gain.shape[0]):
gain[i, :, :] = (img_data[2 * i, :, :] + img_data[2 * i + 1, :, :]) / 2
dark[i, :, :] = (img_dark[2 * i, :, :] + img_dark[2 * i + 1, :, :]) / 2
tmp = (float(out_hdr_dict["wavelength"][2 * i]) + float(out_hdr_dict["wavelength"][2 * i + 1])) / 2
wave.append(str(round(tmp, 3)))
out_hdr_dict["wavelength"] = wave
gain_name = os.path.join(self.out_file_path, "gain.bip")
offset_name = os.path.join(self.out_file_path, "offset.bip")
self.write_img(gain_name, gain)
self.process_hdr(gain_name, out_hdr_dict)
self.write_img(offset_name, dark)
self.process_hdr(offset_name, out_hdr_dict)
if self.spatialbin == 2 and self.spectralbin == 2:
samples = int(in_hdr_dict["samples"])
if int(in_hdr_dict["bands"]) % 2 == 0:
bands = int(int(in_hdr_dict["bands"]) / 2)
else:
print("Bands is not divisible by 2.")
return
# # 空间bin
# gain = np.empty((300, 1, 688))
# for i in range(gain.shape[2]):
# if i <= 683:
# gain[:, :, i] = img_data[:, :, i*2] # 丢弃
# else:
# gain[:, :, i] = img_data[:, :, 683] # 重复
if __name__ == '__main__':
app = QApplication(sys.argv)