split.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import os.path as osp
  16. import argparse
  17. from math import ceil
  18. import paddlers
  19. from tqdm import tqdm
  20. from utils import Raster, save_geotiff, time_it
  21. def _calc_window_tf(geot, loc):
  22. x, hr, r1, y, r2, vr = geot
  23. nx, ny = loc
  24. return (x + nx * hr, hr, r1, y + ny * vr, r2, vr)
  25. @time_it
  26. def split_data(image_path, mask_path, block_size, save_dir):
  27. if not osp.exists(save_dir):
  28. os.makedirs(save_dir)
  29. os.makedirs(osp.join(save_dir, "images"))
  30. if mask_path is not None:
  31. os.makedirs(osp.join(save_dir, "masks"))
  32. image_name, image_ext = image_path.replace("\\",
  33. "/").split("/")[-1].split(".")
  34. image = Raster(image_path)
  35. mask = Raster(mask_path) if mask_path is not None else None
  36. if mask is not None and (image.width != mask.width or
  37. image.height != mask.height):
  38. raise ValueError("image's shape must equal mask's shape.")
  39. rows = ceil(image.height / block_size)
  40. cols = ceil(image.width / block_size)
  41. total_number = int(rows * cols)
  42. with tqdm(total=total_number) as pbar:
  43. for r in range(rows):
  44. for c in range(cols):
  45. loc_start = (c * block_size, r * block_size)
  46. image_title = image.getArray(loc_start,
  47. (block_size, block_size))
  48. image_save_path = osp.join(save_dir, "images", (
  49. image_name + "_" + str(r) + "_" + str(c) + "." + image_ext))
  50. window_geotf = _calc_window_tf(image.geot, loc_start)
  51. save_geotiff(image_title, image_save_path, image.proj,
  52. window_geotf)
  53. if mask is not None:
  54. mask_title = mask.getArray(loc_start,
  55. (block_size, block_size))
  56. mask_save_path = osp.join(save_dir, "masks",
  57. (image_name + "_" + str(r) + "_" +
  58. str(c) + "." + image_ext))
  59. save_geotiff(mask_title, mask_save_path, image.proj,
  60. window_geotf)
  61. pbar.update(1)
  62. if __name__ == "__main__":
  63. parser = argparse.ArgumentParser(description="input parameters")
  64. parser.add_argument("--image_path", type=str, required=True, \
  65. help="Path of input image.")
  66. parser.add_argument("--mask_path", type=str, default=None, \
  67. help="Path of input labels.")
  68. parser.add_argument("--block_size", type=int, default=512, \
  69. help="Size of image block. Default value is 512.")
  70. parser.add_argument("--save_dir", type=str, default="dataset", \
  71. help="Directory to save the results. Default value is 'dataset'.")
  72. args = parser.parse_args()
  73. split_data(args.image_path, args.mask_path, args.block_size, args.save_dir)