-
Notifications
You must be signed in to change notification settings - Fork 9
/
visualize_tracking.py
executable file
·106 lines (80 loc) · 3.79 KB
/
visualize_tracking.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
#!/usr/bin/python
import re
import sys
import argparse
import numpy as np
import matplotlib.pyplot as plt
from skimage import measure
from skimage.segmentation import find_boundaries
from scipy.interpolate import splprep, splev
from BKlib import tiff_to_ndarray, write_video
def correct_orientation(im):
"""Correct the frame orientations (to match imagej)"""
return np.fliplr(im)
def interpolate_boundary_pts(pts, N=200):
"""Interpolate sparse (closed) boundary points via spline."""
# interpolate boundary
tck, u = splprep(pts.T, u=None, s=0.0, per=1)
u_new = np.linspace(u.min(), u.max(), 1000)
x_new, y_new = splev(u_new, tck, der=0)
u_dense = np.linspace(0, 1, N+1)
x_dense, y_dense = splev(u_dense, tck, der=0)
return x_dense, y_dense
def parse_command_line_args():
# TODO: additional arguments: frames per second
description_str = 'View \"track cell\" output as a movie and optionally save the visualization to file.'
parser = argparse.ArgumentParser(description=description_str)
parser.add_argument('tiff_movie', metavar='<tiff movie>', type=str, help='an imagej-style tiff movie')
parser.add_argument('boundaries_fn', metavar='<cell boundary file>', type=str,
help='A numpy file containing the boundary points from cell tracking (cell[label]_boundary_points.npy)')
parser.add_argument('-o', '--out', metavar='<output movie file>', type=str, help='output movie file')
args = parser.parse_args()
return args.tiff_movie, args.boundaries_fn, args.out
# TODO: a mini video player would be useful here
if __name__ == '__main__':
### Parse command line arguments
tiff_fn, boundaries_fn, out_fn = parse_command_line_args()
### Load data
print 'Loading %s...' % tiff_fn,
sys.stdout.flush()
frames = tiff_to_ndarray(tiff_fn)
all_boundary_pts = np.load(boundaries_fn)
print 'done.'
sys.stdout.flush()
### visualize cell motion to check the results ###
boundary_pts = all_boundary_pts[0]
boundary_x, boundary_y = interpolate_boundary_pts(boundary_pts)
mask = measure.grid_points_in_poly(frames[0].shape, boundary_pts)
center = measure.regionprops(mask)[0].centroid
plt.ion()
fig = plt.figure()
implot = plt.imshow(frames[0], cmap='gray')
boundary_plot, = plt.plot(boundary_y, boundary_x, 'bo', markeredgecolor='none', markersize=1)
boundary_pts_plot, = plt.plot(boundary_pts[:,1], boundary_pts[:,0], 'ro', markeredgecolor='none', markersize=4)
center_point, = plt.plot(center[1], center[0], 'ro', markeredgecolor='r', markersize=7)
plt.axis('off')
fig.canvas.draw()
for frame_num, (frame, boundary_pts) in enumerate(zip(frames, all_boundary_pts)):
boundary_x, boundary_y = interpolate_boundary_pts(boundary_pts)
mask = measure.grid_points_in_poly(frame.shape, boundary_pts)
center = measure.regionprops(mask)[0].centroid
implot.set_data(frame)
boundary_plot.set_data(boundary_y, boundary_x)
boundary_pts_plot.set_data(boundary_pts[:,1], boundary_pts[:,0])
center_point.set_data(center[1], center[0])
plt.title('frame %i' % (frame_num+1))
fig.canvas.draw()
plt.ioff()
plt.show()
### save to file
if out_fn:
# cell_label = re.findall(r'\d+', boundaries_fn)[0]
movie_frames = np.empty_like(frames)
for frame_num, (frame, boundary_pts) in enumerate(zip(frames.copy(), all_boundary_pts)):
mask = measure.grid_points_in_poly(frame.shape, boundary_pts)
boundary = find_boundaries(mask)
frame += 0.5*frame.max()*boundary
movie_frames[frame_num,:,:] = correct_orientation(frame)
# fn = 'cell%i_visualization.avi' % cell_label
# print 'Saving visualization to', fn
write_video(movie_frames, out_fn)