-
Notifications
You must be signed in to change notification settings - Fork 0
/
crop_sar.py
58 lines (43 loc) · 1.65 KB
/
crop_sar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Project: sarcnn
Author: khalil MEFTAH
Date: 2021-12-20
SARCNN: Deep Neural Convolutional Network for SAR Images Despeckling model testing implementation
"""
# imports
from pathlib import Path
import argparse
import numpy as np
import os
# arguments parsing
parser = argparse.ArgumentParser()
parser.add_argument('--inputs_dir', type = str, help='input directory')
parser.add_argument('--outputs_dir', type = str, help='output directory')
parser.add_argument('--patch_size', type = int, help='patch size')
parser.add_argument('--stride', type = int, help='stride')
parser.add_argument('--step', type = int, help='step')
args = parser.parse_args()
pat_size = args.patch_size
stride = args.stride
step = args.step
outputs_dir = Path(args.outputs_dir)
outputs_dir.mkdir(parents = True, exist_ok = True)
for dirpath, dirnames, filenames in os.walk(args.inputs_dir):
structure = os.path.join(outputs_dir, dirpath[len(args.inputs_dir):])
if not os.path.isdir(structure):
os.mkdir(structure)
path = Path(dirpath)
image_files = [*path.glob('*.npy')]
print(f"number of data {len(image_files)} in {dirpath}")
count = 0
for f in image_files:
img = np.load(f)
H, W = img.shape
# make patches from the image
for x in range(0 + step, H - pat_size, stride):
for y in range(0 + step, W - pat_size, stride):
patch = img[x:x + pat_size, y:y + pat_size]
patch_name = f"{f.stem}_{count}.npy"
np.save(Path(structure) / patch_name, patch)
count += 1
print(f"{count} patches are saved")