-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui2.py
1077 lines (823 loc) · 38.7 KB
/
gui2.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# menu.py
import Tkinter as tk
from Tkinter import *
import tkMessageBox
import pygubu
from collections import deque
import zipfile
try:
import tkinter as tk # for python 3
except:
import Tkinter as tk # for python 2
import pygubu
from Tkinter import *
from tkFileDialog import askopenfilename
from tkFileDialog import askdirectory
from tkFileDialog import asksaveasfilename
from tkFileDialog import asksaveasfile
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import tkMessageBox
from skimage.feature import peak_local_max
from skimage.morphology import watershed
from scipy import ndimage
from sklearn import neighbors
import os, csv
import cv2
import tkFont
from PIL import Image as img2
from Tkinter import Image
import numpy as np
from libtiff import TIFF
import platform
import time
from Tkinter import tkinter
import ttk
import gifmaker
import ImageSequence
import glob
import ImageTk
import mahotas
# get the platform
if platform.system() == 'Linux':
dash = '/'
if platform.system() == 'Windows':
dash = ''
tmppath = os.getcwd()
tmppath = tmppath + dash + time.strftime("%d_%m_%Y_%I")
if os.path.exists(tmppath) is False:
os.mkdir(tmppath)
# create directories
trackingdir = os.path.join(tmppath + os.sep, 'tracking/')
trajectorydir = os.path.join(tmppath + os.sep, 'finalplot/')
overlaytrajectorydir = os.path.join(tmppath + os.sep, 'overlaytrajecory/')
overlaytrajectoryanidir = os.path.join(tmppath + os.sep, 'overlaytrajecoryani/')
masktrajectorydir = os.path.join(tmppath + os.sep, 'masktrajector/')
csvdir = os.path.join(tmppath + os.sep, 'datafiles/')
if os.path.exists(trackingdir) is False:
os.mkdir(trackingdir)
if os.path.exists(trajectorydir) is False:
os.mkdir(trajectorydir)
if os.path.exists(csvdir) is False:
os.mkdir(csvdir)
if os.path.exists(overlaytrajectorydir) is False:
os.mkdir(overlaytrajectorydir)
if os.path.exists(masktrajectorydir) is False:
os.mkdir(masktrajectorydir)
if os.path.exists(overlaytrajectoryanidir) is False:
os.mkdir(overlaytrajectoryanidir)
# parameters for Shi-Tomasi Corner detectors
# Parameters for lucas kanade optical flow
lk_params = dict(winSize=(20, 20), maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.5))
# Create some random colors
color = np.random.randint(0, 255, (500, 3))
def read_tiff(self, path):
start = time.time()
frames = []
tif = TIFF.open(path, mode='r')
try:
for cc, tframe in enumerate(tif.iter_images()):
frames.append(tframe)
print cc
cv2.imwrite('/home/sami/framesPPT/nextFrame/newImg/%d.jpg'%cc, tframe)
except EOFError:
pass
end = start - time.time()
print end
return frames
def read_avi(self, path):
frames, timestamp = [], []
cap = cv2.VideoCapture(path)
try:
while cap.isOpened():
ret, img = cap.read()
# get the frame in seconds
t1 = cap.get(0)
timestamp.append(t1)
if img is None:
break
frames.append(img)
except EOFError:
pass
return frames, timestamp
def read_others(self, path):
frames = cv2.imread(path)
return frames
def resize_image(trackingdir):
r = 100.0 / image.shape[1]
dim = (100, int(image.shape[0] * r))
# perform the actual resizing of the image and show it
resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
def animate(self, trackingdir):
# look_directory(trackingdir)
# CHECK IF LIST IS EMPTY
if len(self.gifBackgroundImages) == 0:
# CREATE FILES IN LIST
for foldername in os.listdir(trackingdir):
self.gifBackgroundImages.append(foldername)
self.gifBackgroundImages.sort(key=lambda x: int(x.split('.')[0]))
if self.atualGifBackgroundImage == len(self.gifBackgroundImages):
self.atualGifBackgroundImage = 0
try:
self.background["file"] = trackingdir + self.gifBackgroundImages[self.atualGifBackgroundImage]
self.label1["image"] = self.background
self.atualGifBackgroundImage += 1
except EOFError:
print (trackingdir + self.gifBackgroundImages[self.atualGifBackgroundImage])
pass
# MILISECONDS\/ PER FRAME
self.after(300, lambda: animate(self, trackingdir))
def morph_dilate(self, image):
image = cv2.dilate(image, cv2.MORPH_DILATE, kernel)
return image
def morph_close(self, image):
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
return image
def morph_open(self, image):
image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
return image
def morph_gradient(self, image):
image = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel)
return image
def morph_erode(self, image):
image = cv2.morphologyEx(image, cv2.MORPH_ERODE, kernel)
return image
def white_background(image, kernel):
im = cv2.threshold(image, 173, 255, cv2.THRESH_BINARY)
im = im[1]
dilation = cv2.dilate(im, kernel, iterations=1)
gradient = cv2.morphologyEx(dilation, cv2.MORPH_GRADIENT, kernel)
closing = cv2.morphologyEx(gradient, cv2.MORPH_CLOSE, kernel)
shifted = cv2.pyrMeanShiftFiltering(closing, 10, 20)
gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
D = ndimage.distance_transform_edt(thresh)
localMax = peak_local_max(D, indices=False, min_distance=10,
labels=thresh)
# perform a connected component analysis on the local peaks,
# using 8-connectivity, then appy the Watershed algorithm
markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
labels = watershed(-D, markers, mask=thresh)
# create a mask
mask2 = np.zeros(image.shape, dtype="uint8")
# loop over the unique labels returned by the Watershed algorithm for
for label in np.unique(labels):
# if the label is zero, we are examining the 'background' so simply ignore it
if label == 0:
continue
# otherwise, allocate memory for the label region and draw
# it on the mask
mask2[labels == label] = 255
# close gaps
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
mask2 = cv2.morphologyEx(mask2, cv2.MORPH_OPEN, kernel)
mask2 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kernel)
return mask2
def basic_seg(img, images):
noOfFrames = len(images)
bgFrame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
for i in range(1, 4):
bgFrame = bgFrame / 2 + \
cv2.cvtColor((images[i]),
cv2.COLOR_BGR2GRAY) / 2
# Array to save the object locations
objLocs = np.array([None, None])
# Kernel for morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
# Display the frames like a video
# Read each frame
frame = images[1]
# Perform background subtraction after median filter
diffFrame = cv2.absdiff(cv2.cvtColor(cv2.medianBlur(frame, 7), \
cv2.COLOR_BGR2GRAY), cv2.medianBlur(bgFrame, 7))
# Otsu thresholding to create the binary image
[th, bwFrame] = cv2.threshold(diffFrame, 0, 255, cv2.THRESH_OTSU)
# Morphological opening operation to remove small blobs
bwFrame = cv2.morphologyEx(bwFrame, cv2.MORPH_OPEN, kernel)
return bwFrame
def black_background(image, kernel):
shifted = cv2.pyrMeanShiftFiltering(image, 10, 39)
gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
D = ndimage.distance_transform_edt(thresh)
localMax = peak_local_max(D, indices=False, min_distance=10,
labels=thresh)
# perform a connected component analysis on the local peaks,
# using 8-connectivity, then appy the Watershed algorithm
markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
labels = watershed(-D, markers, mask=thresh)
# create a mask
mask2 = np.zeros(gray.shape, dtype="uint8")
# loop over the unique labels returned by the Watershed algorithm for
for label in np.unique(labels):
# if the label is zero, we are examining the 'background' so simply ignore it
if label == 0:
continue
# otherwise, allocate memory for the label region and draw
# it on the mask
mask2[labels == label] = 255
return mask2
# histgram equalization
def histogram_equaliz(image):
old_gray_image = cv2.equalizeHist(image)
return old_gray_image
def shi_tomasi(image, maxCorner, qualityLevel, MinDistance):
# detect corners in the image
corners = cv2.goodFeaturesToTrack(image,
maxCorner,
qualityLevel,
MinDistance,
mask=None,
blockSize=7)
return corners
def harris_corner(image, maxCorner, qualityLevel, minDistance):
corners = cv2.goodFeaturesToTrack(image, # img
maxCorner, # maxCorners
qualityLevel, # qualityLevel
minDistance, # minDistance
None, # corners,
None, # mask,
7, # blockSize,
useHarrisDetector=True, # useHarrisDetector,
k=0.05 # k
)
return corners
def optical_flow(self, frames, old_gray_image1, intialPoints, segMeth, maxCorner, qualityLevel, minDistance,
updateconvax, progessbar):
old_gray_image2 = cv2.cvtColor(old_gray_image1, cv2.COLOR_BGR2GRAY)
old_gray_image2 = histogram_equaliz(old_gray_image2)
print intialPoints
mask = np.zeros_like(old_gray_image1,)
finalFrame = len(frames)
trajectoriesX, trajectoriesY, cellIDs, frameID = [], [], [], []
for i, frame in enumerate(frames):
try:
new_gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# perform histogram equalization to balance image color intensity
new_gray_image = histogram_equaliz(new_gray_image)
progessbar.step(i*2)
newPoints, st, err = cv2.calcOpticalFlowPyrLK(old_gray_image2, new_gray_image, intialPoints, None,
**lk_params)
# Select good points
good_new = newPoints[st == 1]
good_old = intialPoints[st == 1]
# draw the tracks
cont = len(good_new)
initialValue = 0
for ii, (new, old) in enumerate(zip(good_new, good_old)):
a, b = new.ravel()
c, d = old.ravel()
#mask = cv2.line(mask, (a, b), (c, d), color[ii].tolist(), 2)
mask = cv2.line(mask, (a, b), (c, d), (255, 255, 255), 2)
#frame = cv2.circle(frame, (a, b), 5, color[i].tolist(), -1)
#frame = cv2.putText(frame, "%d" % ii, (a, b), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1,
# color[ii].tolist())
#nextFrame = cv2.putText(nextFrame, "%d" % ii, (a, b), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1,
# (255, 255, 255))
img = cv2.add(frame, mask)
edged2 = np.hstack([mask, img])
# Now update the previous frame and previous points
old_gray_image2 = new_gray_image.copy()
intialPoints = good_new.reshape(-1, 1, 2)
# Keep the data of for later processing
trajectoriesX.append(a)
trajectoriesY.append(b)
cellIDs.append(ii)
frameID.append(i)
r = 500.0 / img.shape[1]
dim = (500, int(img.shape[0] * r))
# perform the actual resizing of the image and show it
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
mahotas.imsave(trackingdir + '%d.gif' % i, img)
mahotas.imsave(masktrajectorydir + '%d.gif' % i, mask)
mahotas.imsave(overlaytrajectorydir + '%d.gif' % i, resized)
tmp_path = trackingdir + '%d.gif' % i
image = img2.open(str(tmp_path))
image = ImageTk.PhotoImage(image)
root.image = image
imagesprite = updateconvax.create_image(283, 187, image=image, anchor='c')
time.sleep(1)
updateconvax.update_idletasks() # Force redraw
updateconvax.delete(imagesprite)
if i == finalFrame - 1:
cv2.imwrite(trajectorydir + 'finalTrajectory.png', img)
cv2.imwrite(trajectorydir + 'Plottrajector.png', mask)
image = img2.open(str(tmp_path))
image = ImageTk.PhotoImage(image)
root.image = image
imagesprite = updateconvax.create_image(280, 193, image=image, anchor='c')
except EOFError:
continue
unpacked = zip(frameID, cellIDs, trajectoriesX, trajectoriesY)
with open(csvdir + 'data.csv', 'wt') as f1:
writer = csv.writer(f1, lineterminator='\n')
writer.writerow(('frameID', 'CellIDs', 'x-axis', "y-axis",))
for value in unpacked:
writer.writerow(value)
def shape_matching(self, newframes, oldframes, kernel, segMeth=''):
# PERFORM SEGMENTATION USING WATERSHED ALGORITHM
if trackingMethd == 'white':
mask, _ = white_background(oldframes)
if trackingMethd == 'black':
mask, _ = black_background(oldframes)
else:
tkMessageBox.showinfo('Optical flow', 'defualt tracking is set to optical flow')
def centroid_matching(self, oldframe, frames, kernel, intialPoints, maxCorner, qualityLevel, minDistance,
updateconvax, segMeth):
# PERFORM SEGMENTATION USING WATERSHED ALGORITHM
if segMeth == 'white':
mask = white_background(oldframe)
if segMeth == 'black':
mask = black_background(oldframe)
if segMeth == 'haris':
#intialPoints = shi_tomasi(oldframe, maxCorner, qualityLevel, minDistance)
print 'ok'
if segMeth == 'shi':
#intialPoints = harris_corner(oldframe, maxCorner, qualityLevel, minDistance)
print 'ok'
print segMeth
mask = mask = np.zeros_like(oldframe,)
for i,frame in enumerate(frames):
#try:
tm_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
tm_img = histogram_equaliz(tm_img)
if segMeth =='shi':
newPoints = shi_tomasi(tm_img, maxCorner, qualityLevel, minDistance)
if segMeth == 'haris':
newPoints == harris_corner(tm_img, maxCorner,qualityLevel, minDistance)
good_new = newPoints
good_old = intialPoints
for ii, (new, old) in enumerate(zip(good_new, good_old)):
a, b = new.ravel()
c, d = old.ravel()
mask = cv2.line(mask, (a, b), (c, d), color[ii].tolist(), 2)
#mask = cv2.line(mask, (a, b), (c, d), (255, 255, 255), 2)
#frame = cv2.circle(frame, (a, b), 5, color[i].tolist(), -1)
#frame = cv2.putText(frame, "%d" % ii, (a, b), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1,
# color[ii].tolist())
#frame = cv2.putText(frame, "%d" % ii, (a, b), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1,
# (255, 255, 255))
img = cv2.add(frame, mask)
edged2 = np.hstack([mask, img])
# Now update the previous frame and previous points
oldframe = frame.copy()
intialPoints = good_new.reshape(-1, 1, 2)
# Keep the data of for later processing
#trajectoriesX.append(a)
#trajectoriesY.append(b)
#cellIDs.append(ii)
#frameID.append(i)
r = 500.0 / img.shape[1]
dim = (500, int(img.shape[0] * r))
# perform the actual resizing of the image and show it
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
mahotas.imsave(trackingdir + '%d.gif' % i, img)
mahotas.imsave(masktrajectorydir + '%d.gif' % i, mask)
mahotas.imsave(overlaytrajectorydir + '%d.gif' % i, resized)
tmp_path = trackingdir + '%d.gif' % i
image = img2.open(str(tmp_path))
image = ImageTk.PhotoImage(image)
root.image = image
imagesprite = updateconvax.create_image(280, 193, image=image, anchor='c')
time.sleep(1)
updateconvax.update_idletasks() # Force redraw
updateconvax.delete(imagesprite)
if i == 100 - 1:
cv2.imwrite(trajectorydir + 'finalTrajectory.png', img)
cv2.imwrite(trajectorydir + 'Plottrajector.png', mask)
image = img2.open(str(tmp_path))
image = ImageTk.PhotoImage(image)
root.image = image
imagesprite4 = updateconvax.create_image(280, 193, image=image, anchor='c')
#except EOFError:
# continue
#unpacked = zip(frameID, cellIDs, trajectoriesX, trajectoriesY)
#with open(csvdir + 'data.csv', 'wt') as f1:
# writer = csv.writer(f1, lineterminator='\n')
# writer.writerow(('frameID', 'CellIDs', 'x-axis', "y-axis",))
# for value in unpacked:
# writer.writerow(value)
'''
def centroid_matching(self, newframes, oldframes, kernel, intialPoints, maxCorner, qualityLevel, minDistance,
updateconvax, segMeth=''):
# PERFORM SEGMENTATION USING WATERSHED ALGORITHM
if segMeth == 'white':
mask = white_background(oldframes)
if segMeth == 'black':
mask = black_background(oldframes)
if segMeth =='haris':
image = shi_tomasi(newframes,maxCorner, qualityLevel, minDistance)
if segMeth =='shi':
image = harris_corner(newframes,maxCorner, qualityLevel, minDistance)
else:
tkMessageBox.showinfo('Optical flow', 'defualt tracking is set to optical flow')
# perform tracking
for index, refCont in enumerate(mask):
centroid = []
count = 0
M = cv2.moments(refCont)
divisor = M['m00']
if divisor != 0.0:
centroid_x = int(M['m10'] / divisor) # Get the x-centriod the cnt
centroid_y = int(M['m01'] / divisor) # get the y-centriod the cnt
XTrajectory.append(centroid_x)
YTrajectory.append(centroid_y)
period.append(t[0])
frameID.append(int(0))
for i, frame in enumerate(f[1:]):
i += 1
distances = []
if trackingMethd == 'white':
NextFrame = white_background(frame)
if trackingMethd == 'black':
NextFrame = black_background(frame)
for index2, NextFrameCell in enumerate(NextFrame):
# structural similarity index for the images
hd = cv2.createHausdorffDistanceExtractor()
d1 = hd.computeDistance(refCont, NextFrameCell)
if d1 is not None:
distances.append(d1)
ClosestCells.append(NextFrameCell)
# print distances
MinDistance = min(distances)
MinIndex = distances.index(MinDistance)
indexedCell = ClosestCells[MinIndex]
M1 = cv2.moments(indexedCell)
check = M1['m00']
if check != 0.0:
centroid_xx = int(M1['m10'] / check) # Get the x-centriod the cnt
centroid_yy = int(M1['m01'] / check) # get the y-centriod the cnt
# compared previous centroid
if i is 1:
previous_xx = centroid_xx
previous_yy = centroid_yy
AbsDifference = abs(previous_xx - centroid_xx)
AbsDifference2 = abs(previous_yy - centroid_yy)
# print AbsDifference
try:
while AbsDifference and AbsDifference2 > 3:
MinDistance = min(distances)
MinIndex = distances.index(MinDistance)
indexedCell = ClosestCells[MinIndex]
M1 = cv2.moments(indexedCell)
check = M1['m00']
if check != 0.0:
centroid_xx = int(M1['m10'] / check) # Get the x-centriod the cnt
centroid_yy = int(M1['m01'] / check) # get the y-centriod the cnt
AbsDifference = abs(previous_xx - centroid_xx)
AbsDifference2 = abs(previous_yy - centroid_yy)
if AbsDifference and AbsDifference2 <= 3:
break
del distances[MinIndex]
del ClosestCells[MinIndex]
except ValueError:
pass
if centroid_xx > previous_xx + 5 or centroid_yy > previous_yy + 5:
continue
previous_xx = centroid_xx
print 'x', previous_xx
previous_yy = centroid_yy
print 'y', previous_yy
# keep the trajectories of each cell
refCont = NextFrameCell
XTrajectory.append(centroid_xx)
YTrajectory.append(centroid_yy)
period.append(t[i])
frameID.append(int(i))
CID.append(int(index))
# free the distances
distances = []
centroid_xx = None
centroid_yy = None
del distances
centroid.append([frameID, CID, XTrajectory, YTrajectory, period])
xx = centroid[0]
# free variables
del previous_yy
del previous_xx
cellCentroid.append(xx)
cc = cellCentroid[0]
unpacked = zip(cc[0], cc[1], cc[2], cc[3], cc[4])
with open('Cancer_shape6.1_' + str(count) + '.csv', 'wt') as f1:
writer = csv.writer(f1, lineterminator='\n')
writer.writerow(('frameID', 'CellID', 'x-axis', "y-axis", 'time',))
for value in unpacked:
writer.writerow(value)
count += 1
'''
class Application:
def __init__(self, master):
# 1: Create a builder
self.builder = builder = pygubu.Builder()
# 2: Load an ui file
builder.add_from_file('celltracker.ui')
# 3: Create the widget using a master as parent
self.mainwindow = builder.get_object('mainwindow', master)
# 4: Get the labeled frame
self.labelframe1 = builder.get_object("Labelframe_19")
# 5: Get the filename or path
self.pathchooserinput_3 = builder.get_object("pathchooserinput_3")
# 6: Read the files
self.button = builder.get_object("Button_10")
# 7: Create a progress bar
self.progressdialog = ttk.Progressbar(self.labelframe1, mode='indeterminate', value=0)
self.progressdialog.grid(row=2, column=0, sticky=N + E + W)
self.Labelframe_22 = builder.get_object("Labelframe_22")
self.progressdialog2 = ttk.Progressbar(self.Labelframe_22, mode='indeterminate', value=0)
self.progressdialog2.grid(row=3, column=0, columnspan=5,sticky=N + E + W)
# 8: Manage a segmentation parameters
self.labelframe2 = builder.get_object("Labelframe_12")
# 8.1: Scale label
self.label = Label(self.labelframe2)
self.label.grid(row=1, column=5, sticky=W)
self.fixscale = 0.5
self.label.configure(text=self.fixscale)
# 8.2: Entry
self.cellEstimate = 200
self.minDistance = 10
# 9: Perform segmentation
self.preview = builder.get_object("Button_1")
self.convax1 = builder.get_object("Canvas_4")
self.segmentation = self.builder.get_variable("seg")
self.color = self.builder.get_variable("background")
self.kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
# 10: Create a tracking labels
self.track = builder.get_variable("track")
self.trackconvax = builder.get_object("Canvas_2")
#11: Create a file mining
self.generatefile = builder.get_object("Button_3")
#12: about the toolbox
self.clear = builder.get_object("Button_11")
builder.connect_callbacks(self)
##: set global variable
self.frames, self.timestamp = [], []
def readfile_process_on_click(self):
# Get the path choosed by the user
path = self.pathchooserinput_3.cget('path')
# show the path
if path:
tkMessageBox.showinfo('You choosed', str(path))
# Set a global variable
# Check for the file format
if '.tif' in path:
tif = TIFF.open(path, mode='r')
try:
for cc, tframe in enumerate(tif.iter_images()):
self.frames.append(tframe)
self.progressdialog.step(cc)
self.progressdialog.update()
# self.mainwindow.update()
except EOFError:
tkMessageBox.showinfo('Error', 'file cant be read!!!')
pass
self.progressdialog.stop()
if '.avi' in path:
cap = cv2.VideoCapture(path)
cc = 0
try:
while cap.isOpened():
ret, img = cap.read()
# get the frame in seconds
t1 = cap.get(0)
self.timestamp.append(t1)
if img is None:
break
self.frames.append(img)
self.progressdialog.step(cc)
self.progressdialog.update()
time.sleep(0.1)
cc += 1
# self.mainwindow.update()
except EOFError:
tkMessageBox.showinfo('Error', 'file cant be read!!!')
pass
self.progressdialog.stop()
tmp_img = self.frames[0]
r = 500.0 / tmp_img.shape[1]
dim = (500, int(tmp_img.shape[0] * r))
# perform the actual resizing of the image and show it
resized = cv2.resize(tmp_img, dim, interpolation=cv2.INTER_AREA)
mahotas.imsave('raw_image.gif', resized)
image1 = img2.open('raw_image.gif')
image1 = ImageTk.PhotoImage(image1)
root.image1 = image1
_ = self.convax1.create_image(270, 155, image=image1, anchor='c')
else:
tkMessageBox.showinfo("No file", "Choose a file to process")
# segmentation preview
def previe_on_click(self):
"Display the values of the 2 x Entry widget variables"
self.cellEstimate = self.builder.get_object('Entry_1')
self.minDistance = self.builder.get_object('Entry_3')
self.preconvax = self.builder.get_object("Canvas_5")
self.cellEstimate = self.cellEstimate.get()
self.minDistance = self.minDistance.get()
if self.frames:
# normalize histogram for improving the image contrast
self.normalizedImage = cv2.cvtColor(self.frames[0], cv2.COLOR_BGR2GRAY)
self.normalizedImage = histogram_equaliz(self.normalizedImage)
self.segMethod = self.segmentation.get()
if self.segMethod == 2:
if self.color.get() == 1:
self.prev_image = black_background(self.frames[0], self.kernel)
r = 500.0 / self.prev_image.shape[1]
dim = (500, int(self.prev_image.shape[0] * r))
# perform the actual resizing of the image and show it
self.prev_image = cv2.resize(self.prev_image, dim, interpolation=cv2.INTER_AREA)
mahotas.imsave('SegImage.gif', self.prev_image)
tmp_pre = img2.open('SegImage.gif')
tmp_pre = ImageTk.PhotoImage(tmp_pre)
root.tmp_pre = tmp_pre
segprev = self.preconvax.create_image(280, 185, image=tmp_pre)
if self.color.get() == 2:
self.prev_image = white_background(self.frames[0], self.kernel)
r = 500.0 / self.prev_image.shape[1]
dim = (500, int(self.prev_image.shape[0] * r))
# perform the actual resizing of the image and show it
self.prev_image = cv2.resize(self.prev_image, dim, interpolation=cv2.INTER_AREA)
mahotas.imsave('SegImage.gif', self.prev_image)
tmp_pre = img2.open('SegImage.gif')
tmp_pre = ImageTk.PhotoImage(tmp_pre)
root.tmp_pre = tmp_pre
segprev = self.preconvax.create_image(280, 185, image=tmp_pre)
if self.segMethod == 3:
self.prev_image = harris_corner(self.normalizedImage, int(self.cellEstimate), float(self.fixscale),
int(self.minDistance))
for corner in self.prev_image:
x, y = corner[0]
cv2.circle(self.normalizedImage, (int(x),int(y)),5, (0,0,255),-1)
r = 500.0 / self.normalizedImage.shape[1]
dim = (500, int(self.normalizedImage.shape[0] * r))
# perform the actual resizing of the image and show it
self.normalizedImage = cv2.resize(self.normalizedImage, dim, interpolation=cv2.INTER_AREA)
mahotas.imsave('SegImage.gif', self.normalizedImage)
tmp_pre = img2.open('SegImage.gif')
tmp_pre = ImageTk.PhotoImage(tmp_pre)
root.tmp_pre = tmp_pre
segprev = self.preconvax.create_image(280, 185, image=tmp_pre)
if self.segMethod == 4:
self.prev_image = shi_tomasi(self.normalizedImage, int(self.cellEstimate), float(self.fixscale),
int(self.minDistance))
for corner in self.prev_image:
x, y = corner[0]
cv2.circle(self.normalizedImage, (x, y), 5, (0, 255, 0), -1)
r = 500.0 / self.normalizedImage.shape[1]
dim = (500, int(self.normalizedImage.shape[0] * r))
# perform the actual resizing of the image and show it
self.normalizedImage = cv2.resize(self.normalizedImage, dim, interpolation=cv2.INTER_AREA)
mahotas.imsave('SegImage.gif', self.normalizedImage)
tmp_pre = img2.open('SegImage.gif')
tmp_pre = ImageTk.PhotoImage(tmp_pre)
root.tmp_pre = tmp_pre
segprev = self.preconvax.create_image(280, 185, image=tmp_pre)
if self.segMethod == 5:
self.prev_image = basic_seg(self.frames[0], self.frames)
#print corners
'''for corner in corners:
x, y = corner
cv2.circle(self.normalizedImage, (int(x), int(y)), 5, (0, 0, 255), -1)'''
r = 500.0 / self.prev_image.shape[1]
dim = (500, int(self.prev_image.shape[0] * r))
# perform the actual resizing of the image and show it
self.prev_image = cv2.resize(self.prev_image, dim, interpolation=cv2.INTER_AREA)
mahotas.imsave('SegImage.gif', self.prev_image)
tmp_pre = img2.open('SegImage.gif')
tmp_pre = ImageTk.PhotoImage(tmp_pre)
root.tmp_pre = tmp_pre
segprev = self.preconvax.create_image(280, 185, image=tmp_pre)
else:
tkMessageBox.showinfo('No file', 'no data is found!!!')
return self.segMethod
# perform tracking
def track_on_click(self):
if self.frames:
self.cellEstimate = self.builder.get_object('Entry_1')
self.minDistance = self.builder.get_object('Entry_3')
if self.frames:
self.normalizedImage = cv2.cvtColor(self.frames[0], cv2.COLOR_BGR2GRAY)
self.normalizedImage = histogram_equaliz(self.normalizedImage)
else:
pass
if self.segmentation.get() == 2:
if self.color.get() == 1:
self.mask = black_background(self.frames[0], self.kernel)
#self.mask = histogram_equaliz(self.mask)
self.initialpoints = shi_tomasi(self.mask, int(self.cellEstimate.get()),
float(self.fixscale),
int(self.minDistance.get()))
self.seg = 'black'
if self.color.get() == 2:
self.mask = white_background(self.frames[0], self.kernel)
self.mask = cv2.cvtColor(self.mask, cv2.COLOR_BGR2GRAY)
self.mask = histogram_equaliz(self.mask)
self.initialpoints = shi_tomasi(self.mask, int(self.cellEstimate.get()),
float(self.fixscale),
int(self.minDistance.get()))
self.seg = 'white'
if self.segmentation.get() == 3:
self.initialpoints = harris_corner(self.normalizedImage, int(self.cellEstimate.get()), float(self.fixscale),
int(self.minDistance.get()))
self.seg = 'haris'
if self.segmentation.get() == 4:
self.initialpoints = shi_tomasi(self.normalizedImage, int(self.cellEstimate.get()), float(self.fixscale),
int(self.minDistance.get()))
self.seg = 'shi'
if self.segmentation.get() == 5:
self.mask = basic_seg(self.frames[0], self.frames)
self.initialpoints = shi_tomasi(self.mask, int(self.cellEstimate.get()), float(self.fixscale),
int(self.minDistance.get()))
self.seg = 'basic'
# manipulate a tracking method
if self.track.get() == 8:
tkMessageBox.showinfo('..','Segmentation method: %s \n' %self.seg, )
optical_flow(self, self.frames[1:], self.frames[0], self.initialpoints, str(self.seg),
int(self.cellEstimate.get()), float(self.fixscale), int(self.minDistance.get()), self.trackconvax, self.progressdialog2)
#for i in range(5):
# for j in range(4):
# l = Label(text='%d.%d' % (i, j), relief=RIDGE)
# l.grid(row=i, column=j, sticky=NSEW)
if self.track.get() == 5:
centroid_matching(self,self.frames[0], self.frames[1:],self.kernel,self.initialpoints,int(self.cellEstimate.get()), float(self.fixscale), int(self.minDistance.get()), self.trackconvax,
self.seg)
else:
tkMessageBox.showinfo('Missing data', 'no data to process')
# scale
def on_scale_click(self, event):
scale = self.builder.get_object('Scale_1')
self.fixscale = float("%.1f" % round(scale.get(), 1))
self.label.configure(text=str(self.fixscale))
# generate files
def generate_click(self):
if not (os.path.join(overlaytrajectoryanidir,'animation.gif')):
save_gif = True
title = ''
images, imgs = [], []
for foldername in os.listdir(overlaytrajectorydir):
images.append(foldername)
images.sort(key=lambda x: int(x.split('.')[0]))
for _, file in enumerate(images):
print file
print os.path.join(overlaytrajectorydir,file)
im = img2.open(os.path.join(overlaytrajectorydir,file))
imgs.append(im)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_axis_off()
ims = map(lambda x: (ax.imshow(x), ax.set_title(title)), imgs)