-
Notifications
You must be signed in to change notification settings - Fork 0
/
psii_segmentation.py
executable file
·151 lines (114 loc) · 4.12 KB
/
psii_segmentation.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
"""
Author : Emmanuel Gonzalez
**Notes: This code was developed by collaborators at the USDA
(see references). It was modified to allow for it's
integration in a scalable data processing pipeline.**
References: Jacob Long, Matthew Herritt, Alison Thompson
Date : 2020-08-23
Purpose: Segment PS2 images given a set of thresholds
"""
import argparse
import os
import sys
import numpy as np
import glob
from collections import Counter, defaultdict
import pandas as pd
from osgeo import gdal, ogr, osr
import json
from datetime import datetime
import multiprocessing
startTime = datetime.now()
# --------------------------------------------------
def get_args():
"""Get command-line arguments"""
parser = argparse.ArgumentParser(
description='PS2 image segmentation',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('dirs',
metavar='dirs',
nargs='+',
type=str,
help='Directories containing TIF images')
parser.add_argument('-o',
'--outdir',
help='Output directory',
metavar='str',
type=str,
default='psii_segmentation_out')
return parser.parse_args()
# --------------------------------------------------
def apply_threshold(img_path):
plot = img_path.split('/')[-2]
g_img = gdal.Open(img_path)
a_img = g_img.GetRasterBand(1).ReadAsArray()
all_pixels = []
for row in a_img:
all_pixels.extend(row)
all_pixels = np.asarray(all_pixels)
thresholds = {
"t1": (0, 7 ),
"t2": (8, 10 ),
"t3": (11, 14 ),
"t4": (15, 19 ),
"t5": (20, 255 ),
}
unique, counts = np.unique(all_pixels, return_counts=True)
pixel_counts = dict(zip(unique, counts))
threshold_counts = Counter()
averages_dict = {}
for threshold_name, threshold in thresholds.items():
average_dict = {}
for pixel_value, count in pixel_counts.items():
if threshold[0] <= pixel_value <=threshold[1]:
threshold_counts[threshold_name] += count
average_dict[pixel_value] = count
num = 0
total = 0
for pixel_value, count in average_dict.items():
num += pixel_value * count
total += count
try:
averages_dict[threshold_name] = num/total
except:
averages_dict[threshold_name] = 0
output_list = []
for key, threshold in thresholds.items():
output_list.append({
'Label': os.path.basename(img_path),
'Area': threshold_counts[key],
'Mean': averages_dict[key],
'Min': threshold[0],
'Max': threshold[1],
'Plot': plot
})
return output_list
def process_set(directory):
args = get_args()
startTime_ind = datetime.now()
image_paths = glob.glob(f'{directory}/*.tif')
print(image_paths)
basename = os.path.splitext(os.path.basename(image_paths[0]))[0].split('_')[0]
plot = '_'.join(str(image_paths[0].split('/')[-2]).split(' '))
if not os.path.isdir(os.path.join(args.outdir, plot)):
os.makedirs(os.path.join(args.outdir, plot))
out_path = os.path.join(args.outdir, plot, basename + '_segmentation.csv')
image_dicts = []
for ip in image_paths:
image_dicts.extend(apply_threshold(ip))
df = pd.DataFrame(image_dicts)
df.to_csv(out_path)
print(f'Created {out_path}\nProcessing time: {datetime.now() - startTime_ind}\n')
# --------------------------------------------------
def main():
"""Segment images here"""
args = get_args()
if not os.path.isdir(args.outdir):
os.makedirs(args.outdir)
with multiprocessing.Pool(multiprocessing.cpu_count()) as p:
p.map(process_set, args.dirs)
print(f'Done.\nEntire processing time: {datetime.now() - startTime}')
# --------------------------------------------------
if __name__ == '__main__':
main()