-
Notifications
You must be signed in to change notification settings - Fork 0
/
troponin.py
181 lines (151 loc) · 6.66 KB
/
troponin.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import cv2
import numpy as np
from skimage import exposure
from skimage.util import invert
import mrcfile
def find_actin(img, kern_size=5, sigma=5, low_th=30, high_th=80, rh=2, tht=180, threshld=150,
min_ll=50, max_lg=20, thickness=5, erosion_reps=10):
# convert to image to single-channel
if len(img.shape)>2:
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img = img.astype(np.uint8)
img_inv = invert(img)
# smooth with gaussian blur
kernel_size = kern_size
img_inv = cv2.GaussianBlur(img_inv,(kernel_size, kernel_size),sigma)
# edge detection using Canny algorithm
low_threshold = low_th
high_threshold = high_th
edges = cv2.Canny(img_inv, low_threshold, high_threshold)
# run Hough lines on edge image
rho = rh # distance resolution in pixels of the Hough grid
theta = np.pi / tht # angular resolution in radians of the Hough grid
threshold = threshld # minimum number of votes (intersections in Hough grid cell)
min_line_length = min_ll # minimum number of pixels making up a line
max_line_gap = max_lg # maximum gap in pixels between connectable line segments
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
# draw lines onto blank image
line_image = np.copy(img) * 0
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(line_image,(x1,y1),(x2,y2),255,thickness)
# thicken lines into a searchable area
thick_lines = np.copy(line_image)
thick_lines = invert(thick_lines)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)) # structuring Element
# erode the negative space around line image from Hough stage
for i in range(erosion_reps):
erode = cv2.erode(thick_lines,kernel)
thick_lines = erode.copy()
thick_lines = invert(thick_lines)
percent = np.count_nonzero(thick_lines)/thick_lines.size # percent coverage of highlighted area
thick_lines_image = cv2.addWeighted(img, 1, thick_lines, 0.25, 0) # superimpose highlighted area over image
return thick_lines, thick_lines_image, percent, edges
def find_actin_ll(img, kern_size=5, sigma=0, low_th=30, high_th=80, rh=2, tht=180, threshld=150,
min_ll=50, max_lg=20, true_min_ll=150, thickness=5):
# convert to image to single-channel
if len(img.shape)>2:
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img = img.astype(np.uint8)
img_inv = invert(img)
# smooth with gaussian blur
kernel_size = kern_size
img_inv = cv2.GaussianBlur(img_inv,(kernel_size, kernel_size),sigma)
# edge detection using Canny algorithm
low_threshold = low_th
high_threshold = high_th
edges = cv2.Canny(img_inv, low_threshold, high_threshold)
# run Hough lines on edge image
rho = rh # distance resolution in pixels of the Hough grid
theta = np.pi / tht # angular resolution in radians of the Hough grid
threshold = threshld # votes needed
min_line_length = min_ll
max_line_gap = max_lg
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
# draw lines onto blank image
line_image = np.copy(img) * 0
long_lines = []
for line in lines:
for x1,y1,x2,y2 in line:
if np.sqrt((y2-y1)**2 + (x2-x1)**2) > true_min_ll: # set minimum length for lines included
cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),thickness)
long_lines.append([x1,x2,y1,y2])
line_image = cv2.cvtColor(line_image,cv2.COLOR_BGR2GRAY)
line_image_s = cv2.addWeighted(img, 0.8, line_image, 1, 0)
return line_image, line_image_s, long_lines
def get_trp_coords(starfilename):
data = dict()
with open(starfilename) as f:
# Walk through line by line
name = ""
labels = []
data = []
mode = ""
# Get the data
rr = f.read().splitlines()
l = len(rr)
for line in rr:
if line[0:5] == "data_":
# gets the table name
name = line
elif line[0:5] == "loop_":
# gets into a loop thing and tells the program to expect just labels
mode = "labels"
elif line[0:4] == "_rln":
if mode == "labels": # get normal labels here
params = line.split()
labels.append(params[0][4:])
else:
# labels also hava data just behind
params = line.split()
labels.append(params[0])
if len(data) == 0:
data.append([])
data[0].append(params[1])
# since data came, set the mode
mode = "data"
elif line == "":
# emtpy row, closes table if data was read before
"""
if mode == "data":
self.makeTable(starfilename, name, labels, tuple(data))
# Unset all the vars
name = ""
labels = []
data = []
mode = ""
"""
else:
# mode has to be labels or data before
if mode == "labels" or mode == "data":
d = line.split()
if len(d) != 0:
# If there is empty fields, they will be filled with NULL
if len(d) < len(labels):
for i in range(len(labels)-len(d)):
d.append("NULL")
data.append(d)
mode = "data"
data = np.array(data)
data = data.astype(np.float)
data = data.astype(np.int)
return labels, data
def get_mrc_image(mrc_location, eq=False):
imfile = mrcfile.open(mrc_location)
imdata = imfile.data
image = np.array(imdata)
if eq:
image = exposure.equalize_hist(image)
image = np.interp(image, (image.min(), image.max()), (0,255))
image = image.astype(np.uint8)
return image
def grab_troponin(image, x_coord, y_coord, grab_rad):
xmin = x_coord - grab_rad if x_coord - grab_rad >= 0 else 0
xmax = x_coord + grab_rad if x_coord + grab_rad <= image.shape[1] else image.shape[1]
ymin = y_coord - grab_rad if y_coord - grab_rad >= 0 else 0
ymax = y_coord + grab_rad if y_coord + grab_rad <= image.shape[0] else image.shape[0]
troponin = [i[xmin:xmax] for i in image[ymin:ymax]]
return troponin