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. from tqdm import tqdm
  19. from utils import Raster, save_geotiff, time_it
  20. def _calc_window_tf(geot, loc):
  21. x, hr, r1, y, r2, vr = geot
  22. nx, ny = loc
  23. return (x + nx * hr, hr, r1, y + ny * vr, r2, vr)
  24. @time_it
  25. def split_data(image_path, mask_path, block_size, save_folder):
  26. if not osp.exists(save_folder):
  27. os.makedirs(save_folder)
  28. os.makedirs(osp.join(save_folder, "images"))
  29. if mask_path is not None:
  30. os.makedirs(osp.join(save_folder, "masks"))
  31. image_name, image_ext = image_path.replace("\\",
  32. "/").split("/")[-1].split(".")
  33. image = Raster(image_path)
  34. mask = Raster(mask_path) if mask_path is not None else None
  35. if mask is not None and (image.width != mask.width or
  36. image.height != mask.height):
  37. raise ValueError("image's shape must equal mask's shape.")
  38. rows = ceil(image.height / block_size)
  39. cols = ceil(image.width / block_size)
  40. total_number = int(rows * cols)
  41. with tqdm(total=total_number) as pbar:
  42. for r in range(rows):
  43. for c in range(cols):
  44. loc_start = (c * block_size, r * block_size)
  45. image_title = image.getArray(loc_start,
  46. (block_size, block_size))
  47. image_save_path = osp.join(save_folder, "images", (
  48. image_name + "_" + str(r) + "_" + str(c) + "." + image_ext))
  49. window_geotf = _calc_window_tf(image.geot, loc_start)
  50. save_geotiff(image_title, image_save_path, image.proj,
  51. window_geotf)
  52. if mask is not None:
  53. mask_title = mask.getArray(loc_start,
  54. (block_size, block_size))
  55. mask_save_path = osp.join(save_folder, "masks",
  56. (image_name + "_" + str(r) + "_" +
  57. str(c) + "." + image_ext))
  58. save_geotiff(mask_title, mask_save_path, image.proj,
  59. window_geotf)
  60. pbar.update(1)
  61. parser = argparse.ArgumentParser(description="input parameters")
  62. parser.add_argument("--image_path", type=str, required=True, \
  63. help="The path of big image data.")
  64. parser.add_argument("--mask_path", type=str, default=None, \
  65. help="The path of big image label data.")
  66. parser.add_argument("--block_size", type=int, default=512, \
  67. help="The size of image block, `512` is the default.")
  68. parser.add_argument("--save_folder", type=str, default="dataset", \
  69. help="The folder path to save the results, `dataset` is the default.")
  70. if __name__ == "__main__":
  71. args = parser.parse_args()
  72. split_data(args.image_path, args.mask_path, args.block_size,
  73. args.save_folder)