-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseg_funcs.py
63 lines (40 loc) · 1.72 KB
/
seg_funcs.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
import numpy as np
def sort_xy(x, y, ang_shift=0):
'https://stackoverflow.com/questions/58377015/counterclockwise-sorting-of-x-y-data'
x0 = np.mean(x)
y0 = np.mean(y)
r = np.sqrt((x-x0)**2 + (y-y0)**2)
angles = np.where((y-y0) > 0, np.arccos((x-x0)/r), 2*np.pi-np.arccos((x-x0)/r))
mask = np.argsort(angles)
sort_deg = np.sort(np.degrees(angles))
sort_deg_min_ind = np.argwhere(sort_deg > ang_shift)[0][0]
rearr_mask = np.hstack((mask[sort_deg_min_ind:], mask[0:sort_deg_min_ind]))
x_sorted = x[rearr_mask]
y_sorted = y[rearr_mask]
angles_sorted = np.where((y_sorted-y0) > 0, np.arccos((x_sorted-x0)/r), 2*np.pi-np.arccos((x_sorted-x0)/r))
return x_sorted, y_sorted
def outline2map(outline, image, n_images, ang_shift, avg_range=0):
avg_list = []
total_val_list = []
# Iterate through images
for i in range(0, n_images):
# Sort outline pixels clockwise
x, y = (outline[i] == True).nonzero()
x_sorted, y_sorted = sort_xy(x, y, ang_shift)
val_list = []
# Iterate through sorted pixels and extract pixel values
for j in range(0, len(x_sorted)):
val = image[i, x_sorted[j], y_sorted[j]]
val_list.append(val)
val_array = np.array(val_list)
total_val_list.append(val_list)
if avg_range > 0:
# Average values within windows
split_list = np.array_split(val_array, avg_range)
split_mean = [np.mean(k) for k in split_list]
avg_list.append(split_mean)
if avg_range > 0:
out = np.array(avg_list)
else:
out = total_val_list
return out, x_sorted, y_sorted