From 0d96100f69ff6682fdc6b1f87f346184d43ae402 Mon Sep 17 00:00:00 2001 From: tian jiax <446100073@qq.com> Date: Fri, 1 Sep 2023 17:01:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=80=9A=E7=94=A8=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E9=9B=86=EF=BC=8C=E4=BF=AE=E6=94=B9=E5=88=86=E5=9D=97?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E8=BE=B9=E7=95=8C=E4=BB=A5=E5=8F=8A=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=88=86=E5=9D=97=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/algorithm/block/blockprocess.py | 179 ++++++++++++++++++++++++--- tool/algorithm/image/ImageHandle.py | 12 +- 2 files changed, 168 insertions(+), 23 deletions(-) diff --git a/tool/algorithm/block/blockprocess.py b/tool/algorithm/block/blockprocess.py index e9413b88..3fa56fa4 100644 --- a/tool/algorithm/block/blockprocess.py +++ b/tool/algorithm/block/blockprocess.py @@ -220,6 +220,83 @@ class BlockProcess: count += 1 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'): """ :param data_dir: 存放待裁剪的影像文件夹,不用指定到tif文件 @@ -261,6 +338,70 @@ class BlockProcess: count += 1 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'], datetype='float16'): """ @@ -298,30 +439,30 @@ class BlockProcess: img_name = key + '.' + out_type new_out_dir = os.path.join(out_dir,img_name) image_handler.write_img(new_out_dir, proj, geo, temp_label) - count += 1 return True # if __name__ == '__main__': # bp = BlockProcess() - # # cut - # data_dir = r"D:\DATA\testdata1" - # out_dir = r"D:\DATA\testdata1\cut255" - # file_type = ['tif'] - # out_type = 'tif' - # cut_size = 2048 - # - # # bp.cut(data_dir, out_dir, file_type, out_type, cut_size) - # # combine - # data_dir=r"D:\Workspace\SoilMoisture\Temporary\test" - # w= 4626 - # h= 2313 - # out_dir=r"D:\Workspace\SoilMoisture\Temporary\combine" - # out_type='tif' - # file_type=['tif'] - # src_path = r"D:\Workspace\SoilMoisture\Temporary\preprocessed\HH_preprocessed.tif" - # datetype = bp.get_tif_dtype(src_path) - # bp.combine(data_dir, w, h, out_dir, out_type, file_type, datetype) +# # # cut +# data_dir = r"D:\BaiduNetdiskDownload\HF\cut" +# out_dir = r"D:\BaiduNetdiskDownload\HF\cut_out" +# file_type = ['tif'] +# out_type = 'tif' +# cut_size = 512 +# +# bp.cut_new(data_dir, out_dir, file_type, out_type, cut_size) +# # # combine +# # data_dir=r"D:\Workspace\SoilMoisture\Temporary\test" +# w= 5043 +# h= 1239 +# out_dirs=r"D:\BaiduNetdiskDownload\HF\cut_outs" +# # out_type='tif' +# # file_type=['tif'] +# datetype = 'float' +# # src_path = r"D:\Workspace\SoilMoisture\Temporary\preprocessed\HH_preprocessed.tif" +# # datetype = bp.get_tif_dtype(src_path) +# bp.combine_new(out_dir, w, h, out_dirs, out_type, file_type, datetype) # # # 添加地理信息 diff --git a/tool/algorithm/image/ImageHandle.py b/tool/algorithm/image/ImageHandle.py index 1f2efc0b..227b77f9 100644 --- a/tool/algorithm/image/ImageHandle.py +++ b/tool/algorithm/image/ImageHandle.py @@ -423,12 +423,14 @@ class ImageHandler: outband = dataset.GetRasterBand(1) outband.WriteArray(im_data) if no_data != 'null': - outband.SetNoDataValue(no_data) + outband.SetNoDataValue(np.double(no_data)) outband.FlushCache() else: for i in range(im_bands): outband = dataset.GetRasterBand(1 + i) outband.WriteArray(im_data[i]) + if no_data != 'null': + outband.SetNoDataValue(np.double(no_data)) outband.FlushCache() # outRaster.GetRasterBand(i + 1).WriteArray(array[i]) del dataset @@ -624,11 +626,13 @@ class ImageHandler: temp = np.zeros((2, lon_arr.shape[0], lon_arr.shape[1]), dtype=float) temp[0, :, :] = lon_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__': -# 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" -# s = ImageHandler().get_scope_ori_sim(path) +# path = r'D:\BaiduNetdiskDownload\GZ\lon.rdr' +# path2 = r'D:\BaiduNetdiskDownload\GZ\lat.rdr' +# path3 = r'D:\BaiduNetdiskDownload\GZ\lon_lat.tif' +# s = ImageHandler().band_merge(path, path2, path3) # print(s) # pass \ No newline at end of file