-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVaihingen_img_preprocessing.py
More file actions
68 lines (52 loc) · 2.1 KB
/
Vaihingen_img_preprocessing.py
File metadata and controls
68 lines (52 loc) · 2.1 KB
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
59
60
61
62
63
64
65
66
67
68
"""
This script has been used for preprocessing remote sensing data, in order to use it with neural networks.
Crops patches of same size in a set of images and in the corresponding ground truth images.
Original images and ground truth are attributed with the same file names.
"""
from PIL import Image
from pylab import array, imshow, show
import sys, os
# Parameters...
# Paths to data.
path_to_img = "D:/Desktop/Vaihingen_original/gt/"
# Define patch size for image cropping.
patch_width = 768
patch_height = 768
# Read all data files names for loop.
input_filenames = [f for f in os.listdir(path_to_img) if f.endswith('.tif')]
def main():
# Set an index that will increase after each input image has been processed.
index = 0
print(input_filenames)
# Loop, for each input image, divide it into patches, and save them on disk.
for input_filename in input_filenames:
# Open input image
img = Image.open(path_to_img+input_filename)
# Get input image dimensions
sizeX = img.size[0]
sizeY = img.size[1]
# Compute total number of rows and columns that the image will be divided into.
num_of_columns = sizeX/patch_width
num_of_rows = sizeY/patch_height
# Loop, goes through the image and crop the patches.
for row in range(int(num_of_rows)):
for column in range(int(num_of_columns)):
coords = (
column * patch_width,
row * patch_height,
(column + 1) * patch_width,
(row + 1) * patch_height
)
patch = img.crop(coords)
# Save patch to desired path, with unique ID.
patch.save("D:/Desktop/gt_cropped/"
+ input_filename + "_"
#+ str(index)
+ str(row)
+ str(column)
#+ "_L"
+ ".tif" )
# Increases for each input image.
index += 1
if __name__ == "__main__":
main()