更新通用工具集,修改分块计算边界以及合并分块模块
parent
e6468adc8a
commit
0d96100f69
|
@ -220,6 +220,83 @@ class BlockProcess:
|
||||||
count += 1
|
count += 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def cut_new(self, in_dir, out_dir, file_type=['tif', 'tiff'], out_type='tif', out_size=2048):
|
||||||
|
"""
|
||||||
|
:param in_dir:存放待裁剪的影像文件夹,不用指定到tif文件
|
||||||
|
:param out_dir:存放裁剪结果的影像文件夹
|
||||||
|
:param file_type:待裁剪的影像文件类型tif、tiff、bmp、jpg、png等等
|
||||||
|
:param out_type:裁剪结果影像文件类型
|
||||||
|
:param out_size:裁剪尺寸,裁剪为n*n的方形
|
||||||
|
:return: True or Flase
|
||||||
|
20230831修改 ----tjx
|
||||||
|
"""
|
||||||
|
if not os.path.exists(out_dir):
|
||||||
|
os.makedirs(out_dir)
|
||||||
|
data_dir_list, _ = self.get_file_names(in_dir, file_type)
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
for each_dir in data_dir_list:
|
||||||
|
|
||||||
|
name_suffix = os.path.basename(each_dir)
|
||||||
|
img_name = os.path.splitext(name_suffix)[0]
|
||||||
|
|
||||||
|
# gdal读取方法
|
||||||
|
image = self.__get_band_array(each_dir, 1)
|
||||||
|
|
||||||
|
block_x = int(np.ceil(image.shape[1] / out_size))
|
||||||
|
block_y = int(np.ceil(image.shape[0] / out_size)) # todo 修改分块
|
||||||
|
for i in range(block_y):
|
||||||
|
for j in range(block_x):
|
||||||
|
start_x = j * out_size
|
||||||
|
start_y = i * out_size
|
||||||
|
end_x = image.shape[1] if (j + 1) * out_size > image.shape[1] else (j + 1) * out_size
|
||||||
|
end_y = image.shape[0] if (i + 1) * out_size > image.shape[0] else (i + 1) * out_size
|
||||||
|
|
||||||
|
out_dir_images = os.path.join(out_dir, img_name + '_' + str(start_x) + '_' + str(end_x) + '_' + str(start_y) + '_' + str(
|
||||||
|
end_y) + '.' + out_type)
|
||||||
|
# print(out_dir_images)
|
||||||
|
|
||||||
|
# cut_factor_row = int(np.ceil(image.shape[0] / out_size))
|
||||||
|
# cut_factor_clo = int(np.ceil(image.shape[1] / out_size))
|
||||||
|
# for i in range(cut_factor_row):
|
||||||
|
# for j in range(cut_factor_clo):
|
||||||
|
#
|
||||||
|
# if i == cut_factor_row - 1:
|
||||||
|
# i = image.shape[0] / out_size - 1
|
||||||
|
# else:
|
||||||
|
# pass
|
||||||
|
#
|
||||||
|
# if j == cut_factor_clo - 1:
|
||||||
|
# j = image.shape[1] / out_size - 1
|
||||||
|
# else:
|
||||||
|
# pass
|
||||||
|
#
|
||||||
|
# start_x = int(np.rint(i * out_size))
|
||||||
|
# start_y = int(np.rint(j * out_size))
|
||||||
|
# end_x = int(np.rint((i + 1) * out_size))
|
||||||
|
# end_y = int(np.rint((j + 1) * out_size))
|
||||||
|
# out_dir_images = os.path.join(out_dir, img_name + '_' + str(start_x) + '_' + str(end_x) + '_' + str(start_y) + '_' + str(
|
||||||
|
# end_y) + '.' + out_type)
|
||||||
|
# + '/' + img_name \
|
||||||
|
# + '_' + str(start_x) + '_' + str(end_x) + '_' + str(start_y) + '_' + str(
|
||||||
|
# end_y) + '.' + out_type
|
||||||
|
|
||||||
|
# temp_image = image[start_x:end_x, start_y:end_y]
|
||||||
|
# out_image = Image.fromarray(temp_data)
|
||||||
|
# out_image = Image.fromarray(temp_image)
|
||||||
|
# out_image.save(out_dir_images)
|
||||||
|
|
||||||
|
data = ImageHandler.get_data(each_dir)
|
||||||
|
if ImageHandler.get_bands(each_dir) > 1:
|
||||||
|
# temp_data = data[:,start_x:end_x, start_y:end_y]
|
||||||
|
temp_data = data[:,start_y:end_y, start_x:end_x]
|
||||||
|
else:
|
||||||
|
# temp_data = data[start_x:end_x, start_y:end_y]
|
||||||
|
temp_data = data[start_y:end_y, start_x:end_x]
|
||||||
|
ImageHandler.write_img(out_dir_images, '', [0, 0, 0, 0, 0, 0], temp_data)
|
||||||
|
count += 1
|
||||||
|
return True
|
||||||
|
|
||||||
def combine(self, data_dir, w, h, out_dir, out_type='tif', file_type=['tif', 'tiff'], datetype='float16'):
|
def combine(self, data_dir, w, h, out_dir, out_type='tif', file_type=['tif', 'tiff'], datetype='float16'):
|
||||||
"""
|
"""
|
||||||
:param data_dir: 存放待裁剪的影像文件夹,不用指定到tif文件
|
:param data_dir: 存放待裁剪的影像文件夹,不用指定到tif文件
|
||||||
|
@ -261,6 +338,70 @@ class BlockProcess:
|
||||||
count += 1
|
count += 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
# todo 20230901 修改分块同步修改合并代码
|
||||||
|
def combine_new(self, data_dir, w, h, out_dir, out_type='tif', file_type=['tif', 'tiff'], datetype='float16'):
|
||||||
|
"""
|
||||||
|
:param data_dir: 存放待裁剪的影像文件夹,不用指定到tif文件
|
||||||
|
:param w 拼接影像的宽度,
|
||||||
|
:param h 拼接影像的高度
|
||||||
|
:param out_dir: 存放裁剪结果的影像文件夹
|
||||||
|
:param out_type: 裁剪结果影像文件类型
|
||||||
|
:param file_type: 待裁剪的影像文件类型
|
||||||
|
:param datetype:数据类型 int8,int16,float16,float32 等
|
||||||
|
:return: True or Flase
|
||||||
|
"""
|
||||||
|
if not os.path.exists(out_dir):
|
||||||
|
os.makedirs(out_dir)
|
||||||
|
img_dir, img_name = self.get_file_names(data_dir, file_type)
|
||||||
|
|
||||||
|
dir_dict = self.get_same_img(img_dir, img_name)
|
||||||
|
count = 0
|
||||||
|
for key in dir_dict.keys():
|
||||||
|
dir_list = dir_dict[key]
|
||||||
|
bands = ImageHandler.get_bands(dir_list[0])
|
||||||
|
if bands > 1:
|
||||||
|
temp_label = np.zeros(shape=(bands, h, w), dtype=datetype)
|
||||||
|
for item in dir_list:
|
||||||
|
name_split = item.split('_')
|
||||||
|
x_start = int(name_split[-4])
|
||||||
|
x_end = int(name_split[-3])
|
||||||
|
y_start = int(name_split[-2])
|
||||||
|
y_end = int(name_split[-1].split('.')[0])
|
||||||
|
# img = Image.open(item)
|
||||||
|
img = ImageHandler.get_band_array(item, 1)
|
||||||
|
img = np.array(img)
|
||||||
|
|
||||||
|
temp_label[:, y_start:y_end, x_start:x_end] = img
|
||||||
|
|
||||||
|
img_name = key + '.' + out_type
|
||||||
|
new_out_dir = os.path.join(out_dir, img_name)
|
||||||
|
ImageHandler.write_img(new_out_dir, '', [0, 0, 0, 0, 0, 0], temp_label)
|
||||||
|
# label = Image.fromarray(temp_label)
|
||||||
|
# label.save(new_out_dir)
|
||||||
|
count += 1
|
||||||
|
else:
|
||||||
|
temp_label = np.zeros(shape=(h, w), dtype=datetype)
|
||||||
|
for item in dir_list:
|
||||||
|
name_split = item.split('_')
|
||||||
|
x_start = int(name_split[-4])
|
||||||
|
x_end = int(name_split[-3])
|
||||||
|
y_start = int(name_split[-2])
|
||||||
|
y_end = int(name_split[-1].split('.')[0])
|
||||||
|
# img = Image.open(item)
|
||||||
|
img = ImageHandler.get_band_array(item, 1)
|
||||||
|
img = np.array(img)
|
||||||
|
|
||||||
|
temp_label[y_start:y_end, x_start:x_end] = img
|
||||||
|
|
||||||
|
img_name = key + '.' + out_type
|
||||||
|
new_out_dir = os.path.join(out_dir, img_name)
|
||||||
|
ImageHandler.write_img(new_out_dir, '', [0, 0, 0, 0, 0, 0], temp_label)
|
||||||
|
# label = Image.fromarray(temp_label)
|
||||||
|
# label.save(new_out_dir)
|
||||||
|
|
||||||
|
count += 1
|
||||||
|
return True
|
||||||
|
|
||||||
def combine_Tif(self, data_dir, w, h, out_dir, proj, geo, out_type='tif', file_type=['tif', 'tiff'],
|
def combine_Tif(self, data_dir, w, h, out_dir, proj, geo, out_type='tif', file_type=['tif', 'tiff'],
|
||||||
datetype='float16'):
|
datetype='float16'):
|
||||||
"""
|
"""
|
||||||
|
@ -298,30 +439,30 @@ class BlockProcess:
|
||||||
img_name = key + '.' + out_type
|
img_name = key + '.' + out_type
|
||||||
new_out_dir = os.path.join(out_dir,img_name)
|
new_out_dir = os.path.join(out_dir,img_name)
|
||||||
image_handler.write_img(new_out_dir, proj, geo, temp_label)
|
image_handler.write_img(new_out_dir, proj, geo, temp_label)
|
||||||
|
|
||||||
count += 1
|
count += 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# if __name__ == '__main__':
|
# if __name__ == '__main__':
|
||||||
# bp = BlockProcess()
|
# bp = BlockProcess()
|
||||||
# # cut
|
# # # cut
|
||||||
# data_dir = r"D:\DATA\testdata1"
|
# data_dir = r"D:\BaiduNetdiskDownload\HF\cut"
|
||||||
# out_dir = r"D:\DATA\testdata1\cut255"
|
# out_dir = r"D:\BaiduNetdiskDownload\HF\cut_out"
|
||||||
# file_type = ['tif']
|
# file_type = ['tif']
|
||||||
# out_type = 'tif'
|
# out_type = 'tif'
|
||||||
# cut_size = 2048
|
# cut_size = 512
|
||||||
#
|
#
|
||||||
# # bp.cut(data_dir, out_dir, file_type, out_type, cut_size)
|
# bp.cut_new(data_dir, out_dir, file_type, out_type, cut_size)
|
||||||
# # combine
|
# # # combine
|
||||||
# data_dir=r"D:\Workspace\SoilMoisture\Temporary\test"
|
# # data_dir=r"D:\Workspace\SoilMoisture\Temporary\test"
|
||||||
# w= 4626
|
# w= 5043
|
||||||
# h= 2313
|
# h= 1239
|
||||||
# out_dir=r"D:\Workspace\SoilMoisture\Temporary\combine"
|
# out_dirs=r"D:\BaiduNetdiskDownload\HF\cut_outs"
|
||||||
# out_type='tif'
|
# # out_type='tif'
|
||||||
# file_type=['tif']
|
# # file_type=['tif']
|
||||||
# src_path = r"D:\Workspace\SoilMoisture\Temporary\preprocessed\HH_preprocessed.tif"
|
# datetype = 'float'
|
||||||
# datetype = bp.get_tif_dtype(src_path)
|
# # src_path = r"D:\Workspace\SoilMoisture\Temporary\preprocessed\HH_preprocessed.tif"
|
||||||
# bp.combine(data_dir, w, h, out_dir, out_type, file_type, datetype)
|
# # datetype = bp.get_tif_dtype(src_path)
|
||||||
|
# bp.combine_new(out_dir, w, h, out_dirs, out_type, file_type, datetype)
|
||||||
|
|
||||||
#
|
#
|
||||||
# # 添加地理信息
|
# # 添加地理信息
|
||||||
|
|
|
@ -423,12 +423,14 @@ class ImageHandler:
|
||||||
outband = dataset.GetRasterBand(1)
|
outband = dataset.GetRasterBand(1)
|
||||||
outband.WriteArray(im_data)
|
outband.WriteArray(im_data)
|
||||||
if no_data != 'null':
|
if no_data != 'null':
|
||||||
outband.SetNoDataValue(no_data)
|
outband.SetNoDataValue(np.double(no_data))
|
||||||
outband.FlushCache()
|
outband.FlushCache()
|
||||||
else:
|
else:
|
||||||
for i in range(im_bands):
|
for i in range(im_bands):
|
||||||
outband = dataset.GetRasterBand(1 + i)
|
outband = dataset.GetRasterBand(1 + i)
|
||||||
outband.WriteArray(im_data[i])
|
outband.WriteArray(im_data[i])
|
||||||
|
if no_data != 'null':
|
||||||
|
outband.SetNoDataValue(np.double(no_data))
|
||||||
outband.FlushCache()
|
outband.FlushCache()
|
||||||
# outRaster.GetRasterBand(i + 1).WriteArray(array[i])
|
# outRaster.GetRasterBand(i + 1).WriteArray(array[i])
|
||||||
del dataset
|
del dataset
|
||||||
|
@ -624,11 +626,13 @@ class ImageHandler:
|
||||||
temp = np.zeros((2, lon_arr.shape[0], lon_arr.shape[1]), dtype=float)
|
temp = np.zeros((2, lon_arr.shape[0], lon_arr.shape[1]), dtype=float)
|
||||||
temp[0, :, :] = lon_arr
|
temp[0, :, :] = lon_arr
|
||||||
temp[1, :, :] = lat_arr
|
temp[1, :, :] = lat_arr
|
||||||
self.write_img(ori_sim, '', [0.0, 1.0, 0.0, 0.0, 0.0, 1.0], temp)
|
self.write_img(ori_sim, '', [0.0, 1.0, 0.0, 0.0, 0.0, 1.0], temp, '0')
|
||||||
|
|
||||||
|
|
||||||
# if __name__ == '__main__':
|
# if __name__ == '__main__':
|
||||||
# path = r"I:\MicroWorkspace\product\C-SAR\Ortho\Output\GF3B_MYC_QPSI_003581_E120.6_N31.3_20220729_L1A_AHV_L10000073024_RPC\RPC_ori_sim.tif"
|
# path = r'D:\BaiduNetdiskDownload\GZ\lon.rdr'
|
||||||
# s = ImageHandler().get_scope_ori_sim(path)
|
# path2 = r'D:\BaiduNetdiskDownload\GZ\lat.rdr'
|
||||||
|
# path3 = r'D:\BaiduNetdiskDownload\GZ\lon_lat.tif'
|
||||||
|
# s = ImageHandler().band_merge(path, path2, path3)
|
||||||
# print(s)
|
# print(s)
|
||||||
# pass
|
# pass
|
Loading…
Reference in New Issue