-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.py
162 lines (126 loc) · 4.46 KB
/
util.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
152
153
154
155
156
157
158
159
160
161
162
import scipy.io as sio
import os
from PIL import Image
import matplotlib as mpl
import matplotlib.pyplot as plt
import time
import numpy as np
import skimage
from skimage import draw
#import cv2
IMAGE_SHAPE = (512, 512, 1)
WINDOW_SHAPE = (50, 50)
STEP_SIZE = (7, 7)
def correct_image_path(path_imageBF):
pl = path_imageBF.split(':')
correct_path = 'I:' + pl[1]
return correct_path
def load_csg(filename_csg):
mat_contents = sio.loadmat(filename_csg)
hh = mat_contents['hh']
val = hh[0, 0]
seg_data = dict()
seg_data['cellsegperim'] = val['cellsegperim']
seg_data['filenameBF'] = val['filenameBF']
seg_data['path_imageBF'] = str(val['pathnameBF'][0])
# 下步仅用于矫正不同机器上驱动器名称的误差
seg_data['path_imageBF'] = correct_image_path(seg_data['path_imageBF'])
return seg_data
def transform_cellseg(cellseg):
cellsegs = list()
for i in range(cellseg.shape[0]):
seg = cellseg[i, 0]
if(seg.shape[1]==2):
cellsegs.append(seg)
return cellsegs
def get_seg_im(seg_data, idx):
seg_im = dict()
seg_im['cellseg'] = transform_cellseg(seg_data['cellsegperim'][0, idx])
seg_im['filenameBF'] = str(seg_data['filenameBF'][0, idx][0])
image_file = os.path.join(seg_data['path_imageBF'], seg_im['filenameBF'])
seg_im['imageBF'] = np.array(Image.open(image_file))
return seg_im
def segperim_generator(seg_data):
for i in range(seg_data['cellsegperim'].shape[1]):
seg_im = get_seg_im(seg_data, i)
yield seg_im
def plot_cellseg(seg_im):
colormap = mpl.cm.gray
plt.imshow(seg_im['imageBF'], cmap=colormap)
for idx in range(len(seg_im['cellseg'])):
seg = seg_im['cellseg'][idx, 0]
plt.plot(seg[:, 1], seg[:, 0], 'r')
plt.plot(find_center(seg)[1], find_center(seg)[0], 'r*')
plt.show()
def find_center(seg):
c_mean = np.mean(seg[:, 1])
r_mean = np.mean(seg[:, 0])
return (int(r_mean), int(c_mean))
"""
def get_circle(seg):
circle = np.zeros(IMAGE_SHAPE)
circle[seg[:, 0], seg[:, 1]] = 1
return circle
def get_circles(seg_im):
circles = np.zeros(IMAGE_SHAPE)
for idx in range(len(seg_im['cellseg'])):
seg = seg_im['cellseg'][idx]
circles[seg[:, 0], seg[:, 1]] = 1
return circles
"""
"""
seg_data = load_csg('E:/LTQ work/tanglab/deepYeast/xy01 1-120.csg')
seg_im = next(segperim_generator(seg_data))
plot_cellseg(seg_im)
"""
"""
# 看来还是不能保存为.mat, 不然再次打开内部结构和数据类型就乱了
def load_seg_im_data(filename):
mat_contents = sio.loadmat(filename)
data = mat_contents['data']
"""
def load_data(filename_list):
Xy_list = list()
for filename in filename_list:
Xy_list.append(np.loadtxt(filename, dtype=np.int32, comments='#', delimiter=' '))
Xy = np.vstack(tuple(Xy_list))
np.random.shuffle(Xy)
X = Xy[:, 1:]
y = Xy[:, 0]
X = X.reshape(X.shape[0], WINDOW_SHAPE[0], WINDOW_SHAPE[1])
return X, y
def load_rect_data(filename_list):
Xy_list = list()
for filename in filename_list:
Xy_list.append(np.loadtxt(filename, dtype=np.int32, comments='#', delimiter=' '))
Xy = np.vstack(tuple(Xy_list))
np.random.shuffle(Xy)
X = Xy[:, 4:]
y = Xy[:, 0:4]
X = X.reshape(X.shape[0], WINDOW_SHAPE[0], WINDOW_SHAPE[1])
return X, y
def save_image(data, path):
for idx in range(data.shape[0]):
im = data[idx]
img = Image.fromarray(np.uint16(im))
img.save(os.path.join(path, '%s.tif'%idx))
def plot_rect(imageBF, vertex):
colormap = mpl.cm.gray
plt.imshow(imageBF, cmap=colormap)
(r1, r2, c1, c2) = vertex
plt.plot(np.ones(r2-r1)*c1, np.array(range(r1, r2)), 'r')
plt.plot(np.ones(r2-r1)*c2, np.array(range(r1, r2)), 'r')
plt.plot(np.array(range(c1, c2)), np.ones(c2-c1)*r1, 'r')
plt.plot(np.array(range(c1, c2)), np.ones(c2-c1)*r2, 'r')
plt.xlim(0, WINDOW_SHAPE[1])
plt.ylim(WINDOW_SHAPE[0], 0)
plt.show()
def poly2mask(vertex_row_coords, vertex_col_coords, shape):
fill_row_coords, fill_col_coords = draw.polygon(vertex_row_coords, vertex_col_coords, shape)
mask = np.zeros(shape, dtype=np.bool)
mask[fill_row_coords, fill_col_coords] = True
return mask
def pol2cart(phi, rho):
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return (x, y)