forked from kanishk-ux/Tethered-cell-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
168 lines (145 loc) · 5.96 KB
/
utilities.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
'''
func initializer takes input from the GUI and saves them in variables for furthur use anywhere in the program
func preview displays a preview of initial 50 frames to help cropping of rotating cell by the user
'''
import cv2
import os
import json
import imgpros
import cv2
'''defines variables for storing the inputs obtained from the user from the GUI'''
image = []
refPt = []
cropping = False
folder_path = ''
filename = ''
num_frames =0
meta=0
delay=0
outfilename = ''
out_folder = ''
time = 0
cell_name=''
def intializer(argv):
'''
Initializes the inputs taken from encrypt.py and stores them for furthur use
Input: argv (input taken in command line from the user)
'''
global folder_path,filename,num_frames,meta,delay,outfilename,out_folder,time,cell_name #defines global variables for furthur use
folder_path = argv[1] #folder path is taken from the user from command line
print (folder_path)
folder=folder_path.split("/") #splits absolute folder path to separate folder path and file name
a=0
folder_path="" #initializes folder path
while a<(len(folder)-1): #stores folder path from split name
folder_path += folder[a]
folder_path += "/"
a=a+1
filename= folder[a] #stores file name
tuta = folder_path.split('/') #stores split folder path
cell_name = tuta[len(tuta) - 2] #defining variable cell_name
out_folder = folder_path + cell_name + "_out/"
global num_frames #defining global variable num_frames to define number of frames for image analysis
num_frames = argv[2] #number of frames is input from user
num_frames = int(num_frames) #coverting variable to int
if not os.path.isdir(out_folder):
print ('creating new directory')
os.makedirs(out_folder) #creates new folder with name out_folder to save output data
outfilename = "raw_data.csv" #defining output file name for storing csv data
meta = int(argv[3])
if meta == 1: #takes metadata from folder and changes file name to specified format
fd1 = 'FrameKey-'
fd2 = '-0-0'
print (fd1 + str(num_frames - 1) + fd2)
with open(folder_path + '/metadata.txt') as f:
data = json.load(f)
time = (data[fd1 + str(num_frames - 1) + fd2]['ElapsedTime-ms'] - data[fd1 + '0' + fd2]['ElapsedTime-ms']) / num_frames
else: #takes file name as per obtained filename convention
fps = argv[4] #fps is input from user
fps = int(fps)
# int(raw_input('Enter FPS: '))
time = 1.0 / float(fps) #time inteval b/w 2 frames equals to inverse of the user-specified fps
print ('time between frames ' + str(time))
delay = argv[5] #time difference to be taken between adjacent frames to be displayed to user in GUI
delay = int(delay) #takes values from 1-100 with 100 bring slowest and 1 being fastest
def preview():
'''
Displays a preview of initial 50 frames to help cropping of rotating cell by the user
'''
global meta,filename,folder_path,delay #defining global variables
addr = folder_path + '/img_000000'
ad = '_Default_000.tif'
frame = addr + '000' + ad
if meta == 0: #preview preferences if no metadata is present
frame = cv2.imread(folder_path + filename) #reads image from folder and stores it as a frame
else: #preview preferences if metadata is present
frame = cv2.imread(addr+ '000'+ad) #preview preferences if metadata is present
print ('\nPreviewing.....\n')
count = 0
while (count<50): #loop for limiting number of frames to be previewed to 50
if meta == 0: #case when metadata is not present
print (count)
print (filename)
print (folder_path + filename.replace("00000",str("%05d" % count)))
frame = cv2.imread(folder_path + filename.replace("00000",str("%05d" % count))) #processes 1 frame
else: #case when metadata is present
frame = cv2.imread(addr+ str("%03d" % count)+ad)
image = imgpros.threshold(frame) #thresholds the image before displaying it. Explained in imgpros file
cv2.imshow('image',image)
cv2.waitKey(delay) #imparts delay in displaying frames in GUI
count+=1 #increases count
'''
def click_and_crop(event, x, y, flags, param):
# grab references to the global variables
global refPt, cropping,image
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
refPt.append((x, y))
cropping = False
# draw a rectangle around the region of interest
cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
cv2.imshow("image", image)
def crop(meta,filename,folder_path):
global image
addr = folder_path + '/img_000000'
ad = '_Default_000.tif'
print ('crop cell for analysis.....')
print ('Press c to proceed after cropping')
print ('Press r to redo')
if meta == 1:
frame = addr + '000' + ad
else:
frame = folder_path + filename
# load the image, clone it, and setup the mouse callback function
image = cv2.imread(frame)
image = threshold.threshold(image)
clone = image.copy()
cv2.namedWindow("image")
cv2.setMouseCallback("image", click_and_crop)
# keep looping until the 'q' key is pressed
while True:
# display the image and wait for a keypress
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
# if the 'r' key is pressed, reset the cropping region
if key == ord("r"):
image = clone.copy()
# if the 'c' key is pressed, break from the loop
elif key == ord("c"):
break
# if there are two reference points, then crop the region of interest
# from teh image and display it
# close all open windows
cv2.destroyAllWindows()
a = image[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
trace = numpy.zeros((a.shape[0],a.shape[1]), numpy.uint8)
return refPt,trace
'''