-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
1241 lines (1000 loc) · 46.8 KB
/
main.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
# -*- coding: utf-8 -*-
"""
Author:
Taylor Cooper
Description:
Estimates contact angle from goniometer videos.
Date Created:
Aug 4, 2014
Arguments and Inputs:
config.py:
PATH - videoPath/filePath - a single .MOV or file structure with .MOVs
VSF - video start frame - Starting frame for analysis
VEF - video end frame - Ending frame for analysis
N - frames analyzed - Ratio of frames analyzed to frames ignored
R - rotation - Rotate video by this angle before analyzing
ROI - roi - Region of interest
MASK - mask - Region masked (basically max x extent of bubble)
BR - brightness - Value added to brighten images
TH - threshold - Value used to binary threshold images
BED - bubble edge depth - (BED) Interface between bubble and surface
Outputs Normal Mode:
.txt containing frameNumber, leftCA, rightCA, diameter
Outputs Debug Mode:
.png comparing filtered and unfiltered image
.png of fits and points fitted
.txt containing frameNumber, leftCA, rightCA, diameter
Dependencies:
numpy, cv2, os, string, pylab, shutil, csv, time
Limitations:
Cannot measure contact angles > 90 degrees
Pending Major Changes:
Tuning: CA1
08 video inadequate
17 bad bed / noisy
20 noisy
27 move mask in maybe...? region C is problematic
CA2
46 noisy
CA3
40 N resolution may not be fine enough to see receding CA
Tilting Changes:
Changed polyfit to 3degree
Changed bubmax to 600
Changed derivOffset to 20... (will need to change on a per plot
basis for this one..)
Overlay plots on image
Consider storing syringe loc as a self var or have a mod to pick it
Measurement algo for tilt goniometer photos or side injection
Way to measure angles over 60 or 90... seems to have problems right now
History:
--------------------------------------------------------------
Date: 2015.01.07
Author: Taylor Cooper
Modification: Fixed deriv offset, added max fit points
--------------------------------------------------------------
"""
import numpy as np
import cv2, os, string, shutil, csv, time, sys
import matplotlib.pyplot as plt
from pylab import * # plotting tools
import config
# Video config file variables
varsCA1237 = {
# Always provide the trailing \\ for 'PATH' if it's a filepath
'PATH' : 'D:\\_Work\\0000-AFCC_MetalFSU_A2\\20140808_Dataset\\Rerun1\\',
'VSF' : 100, # Minimum image
'VEF' : 30000, # Set high for CA7
'N' : 5, # Read ever Nth image
'R' : 2, # Rotation (positive is CCW)
'ROI' : (750,1080,800,1175), # row_min, row_max, col_min, col_max
'MASK' : (30,340,255), # col_min, col_max, value outside mask
'BR' : 90, # Brightness added
'TH' : 180, # Threshold value for binary threshold
'POLYDEG' : 5, # Polyfit degree
'DERIVOFF' : 5, # Offset from bed for taking derivative in pixels/10
'MINSYGPOS' : 120, # Minimum distance from left side of image to syringe
'SYGOFF' : 6, # Number of pixels to offset from syringe after finding it
'VIDMID' : 250, # Middle of video used to generate BED image
'BUBMAX' : 600, # Max bubble diameter (no idea why I set this)
'MINFITPTS' : 12, # Minimum points required to attempt a fit
'MAXFITPTS' : 30, # Maximum points for fit attempt, prevents garbage pts.
'DPI' : 100, # plot quality (lower = faster)
'PLOTXOFF' : 120, # Graph offsets from image center
'PLOTYOFF' : -40, # Graph offsets
'DEBUG' : False, # Plots and saves images
'RESETBED' : False, # Will ignore _BED if present
'ASSIGNBEDS' : False, # Will ignore analysis and assign beds
'REMOVEBEDS' : False, # Will ignore assignBeds/analysis and remove _BED
}
varsCA8 = {
# Always provide the trailing \\ for 'PATH' if it's a filepath
'PATH' : 'D:\\_Work\\0000-AFCC_MetalFSU_A2\\Runs\\Example\\',
'VSF' : 100,
'VEF' : 30000, # Set high for CA7
'N' : 5,
'R' : 2,
'ROI' : (480,730,750,1250), # For CA8 rerun
'MASK' : (30,440,255), # For CA8 rerun
'BR' : 90,
'TH' : 180,
'POLYDEG' : 5,
'DERIVOFF' : 10,
'MINSYGPOS' : 120,
'SYGOFF' : 6,
'VIDMID' : 250,
'BUBMAX' : 600,
'MINFITPTS' : 12,
'DPI' : 100, # plot quality (lower = faster
'PLOTXOFF' : 250, # Graph offsets from image center
'PLOTYOFF' : 120, # Graph offsets
'DEBUG' : True, # Plots and saves images
'RESETBED' : True, # Will ignore _BED if present
'ASSIGNBEDS' : False, # Will ignore analysis and assign beds, responds to RESETBED
'REMOVEBEDS' : False, # Will ignore assignBeds and analysis and remove _BED
}
headersVid = [
'Filename',
'FolderID',
'FabID',
'Sample#',
'Region',
'AdvCALeft(deg)',
'AdvCARight(deg)',
'MaxDiameter(px)',
'BED(px)'
]
# Tilt Angle config file variables
varsImgTilt = {
# Always provide the trailing \\ for 'PATH' if it's a filepath
'PATH' : 'D:\\_Work\\0000-AFCC_MetalFSU_A2\\20140810_TiltPhotos\\Derv10_Poly8\\',
'R' : 2,
'BR' : 130,
'TH' : 190,
'POLYDEG' : 8,
'DERIVOFF' : 10, # Or 20 in some cases
'MINSYGPOS' : 120,
'SYGOFF' : 6,
'BUBMAX' : 600,
'MINFITPTS' : 12,
'DPI' : 100, # plot quality (lower = faster
'PLOTXOFF' : 500, # Graph offsets from image center
'PLOTYOFF' : 240, # Graph offsets
'DEBUG' : True, # Plots and saves images
'RESETBED' : False, # Will ignore _BED if present
'ASSIGNBEDS' : False, # Will ignore analysis and assign beds, responds to RESETBED
'REMOVEBEDS' : False, # Will ignore assignBeds and analysis and remove _BED
}
varsImgStatic = {
# Always provide the trailing \\ for 'PATH' if it's a filepath
'PATH' : 'D:\\_Work\\0000-AFCC_MetalFSU_A2\\20140808_Dataset\\Static_Measurements\\3DS\\',
'VSF' : None,
'VEF' : None, # Set high for CA7
'N' : None,
'R' : 2,
'ROI' : (1700,2000,2275,2700), # ??
'MASK' : (50,375,255), # For static measurements
'BR' : 50,
'TH' : 160,
'POLYDEG' : 5,
'DERIVOFF' : 10,
'MINSYGPOS' : 120,
'SYGOFF' : 6,
'BUBMAX' : 600,
'MINFITPTS' : 12,
'DPI' : 100, # plot quality (lower = faster
'PLOTXOFF' : 250, # Graph offsets from image center
'PLOTYOFF' : -40, # Graph offsets
'DEBUG' : True,
'RESETBED' : False,
'ASSIGNBEDS' : False,
'REMOVEBEDS' : False,
}
headersImg = [
'Filename',
'Material',
'index',
'tiltAngle(deg)',
'RecedingCALeft(deg)',
'AdvCARight(deg)',
'MaxDiameter(px)',
'BED(px)',
]
class getContactAngle():
"""
Description: Gets contact angle from folder of goniometer videos.
Input: .MOV
Output: Filtered and contact angle plots
"""
def __init__(self, configurables, masterLogHeaders, assignBeds=False,
isVideo=True):
""" Initialize getContactAngle.
Input: configFile parameters
Output: None
"""
self.isVideo = isVideo
# Video only parameters
if isVideo:
self.vsf = configurables['VSF']
self.vef = configurables['VEF']
self.n = configurables['N']
self.roi = configurables['ROI']
self.mask = configurables['MASK']
self.vidMid = configurables['VIDMID']
# Internal Parameters
self.w = self.roi[3] - self.roi[2]
self.h = self.roi[1] - self.roi[0]
else:
self.w = None
self.h = None
# External parameters
self.path = configurables['PATH']
self.r = configurables['R']
self.br = configurables['BR']
self.th = configurables['TH']
self.polyDeg = configurables['POLYDEG'] # Default 5 for videos
self.derivOffset = configurables['DERIVOFF'] # Default 10 for videos
self.dpi = configurables['DPI']
self.debug = configurables['DEBUG']
self.plotXOff = configurables['PLOTXOFF']
self.plotYOff = configurables['PLOTYOFF']
self.resetbed = configurables['RESETBED']
self.assignBeds = configurables['ASSIGNBEDS']
self.removeBeds = configurables['REMOVEBEDS']
self.syringMinPos = configurables['MINSYGPOS']
self.sygOffset = configurables['SYGOFF']
self.bubMax = configurables['BUBMAX']
self.minFitPoints = configurables['MINFITPTS']
self.maxFitPoints = configurables['MAXFITPTS']
self.headers = masterLogHeaders
# Internal parameters
self.workDir = None
self.itemPath = None
# Human input parameters, warning this value changes with the roi
self.bed = None
def fitContactAngle(self, xLeft, yLeft, xRight, yRight, count):
"""Plots left, right contact angles based on polyfit.
Input: 4 lists to plot and a frame for naming
Output: saves .png of data points, fits, contact angles and self.BED
Returns left, right contact angle or none.
"""
# Warning: xLeft = xFromLeft etc. in this function
makePlot = False
caL = None # left contact angle
caR = None # right contact angle
# Condition to make sure polyfit will not crash
if xLeft != [] and len(xLeft) > self.minFitPoints:
makePlot = True
# Convert to np.arrays
xLeft = np.asarray(xLeft)
yLeft = np.asarray(yLeft)
# Fit data set with self.polyDeg polynomial
coefLeft = np.polyfit(xLeft,yLeft,self.polyDeg)
polyLeft = np.poly1d(coefLeft)
# Get values to plot fit, assume min and max locations
xsLeft = np.arange(xLeft[0], xLeft[-1], 0.1)
ysLeft = polyLeft(xsLeft)
# Get derivative @ left most point and values to plot derivative
# Derivative calculated up from edge, images too noisy for
# calculation right at self.bed
derivLeft = polyLeft.deriv()
# Invert because rotating graph
mL = 1/derivLeft(xLeft[0+self.derivOffset])
if self.debug:
# Get CA lines to plot
bL = xLeft[0] - mL*yLeft[0] # Also inverted
x1L,y1L = (-10000, mL*-10000+bL)
x2L,y2L = (10000, mL*10000+bL)
# Plot lines
plot((x1L,x2L),(y1L,y2L),'b')
plot(yLeft, xLeft,'r.') #Inverted
plot(ysLeft, xsLeft, 'g') #Inverted
# Get contact angle
caL = np.arctan(mL)*180/np.pi
if caL < 0: caL = 180+caL
if xRight != [] and len(xRight) > self.minFitPoints:
makePlot = True
# Same as above
xRight = np.asarray(xRight)
yRight = np.asarray(yRight)
coefRight = np.polyfit(xRight,yRight,self.polyDeg)
polyRight = np.poly1d(coefRight)
xsRight = np.arange(xRight[0], xRight[-1], 0.1)
ysRight = polyRight(xsRight)
derivRight = polyRight.deriv()
mR = 1/derivRight(xRight[0+self.derivOffset])
if self.debug:
bR = xRight[0] - mR*yRight[0]
x1R,y1R = (-10000, mR*-10000+bR)
x2R,y2R = (10000, mR*10000+bR)
plot((x1R,x2R),(y1R,y2R),'b')
plot(yRight,xRight,'r.')
plot(ysRight, xsRight, 'g')
caR = -1*np.arctan(mR)*180/np.pi
if caR < 0: caR = 180+caR
# Only generate plots in debug mode
if makePlot and self.debug:
axhline(self.h - self.bed)
fileName = self.workDir + count + "-plt.svg"
xlim(self.w/2-self.plotXOff,self.w/2+self.plotXOff)
ylim(self.h-self.bed-20,self.h+self.plotYOff)
title(fileName, size=8)
savefig(fileName)#, dpi=self.dpi, papertype='b10',
#orientation='portrait', bbox_inches='tight')
clf() # Clear figure
return caL,caR
def findBubbleEdge(self, arr, useBed=True):
""" Find the top edge of a goniometer bubble.
Input: Transposed numpy array of filtered image.
Output: Returns false if no edge found, else edge position.
"""
# j is position in arr, pxVal is value of the pixel
# cannot used np.iternd here it only iterates forward
for j,pxVal in enumerate(arr):
if j > self.bed and useBed: # If no value found before self.bed
return None
if pxVal < 30: # if a point is found return that points y value
return j
return None # If some how a horizontal white line exists in the image
def findSyringeEdge(self, img, img_T, count):
"""Finds left then right edge of syringe needle at top of image.
Input: np.array of img and transposed np.array of img, frame number
Output: Returns left, right contact angles
"""
xLeft = [] # Column positions on bubble left of syringe
yLeft = [] # Matching row positions on bubble left of syringe
xRight = []
yRight = []
syringeFound = False
done = False
for i,pxVal in enumerate(np.nditer(img[0])):
# Basically don't start looking until at least 120 px
# This avoids noise in the top left corner.
if i < self.syringMinPos:
continue
# Condition for finding left side of syringe
if pxVal < 30 and syringeFound == False:
# Set left bound for indexing
leftMin = i - self.bubMax
if leftMin < 0: leftMin=0
# Look at every point distance 3 from syringe to bubMax
for k in xrange(i-self.sygOffset,i-self.bubMax,-2):
# Basically break loop if you are getting near image edge
if k < 5: break
j = self.findBubbleEdge(img_T[k])
if j:
xLeft.append(k) # column or x
yLeft.append(self.h-j) # row or y
else:
break # if no pixel found before BED break
if k == i-self.bubMax+10:
print 'Warning:: Bubble may exceed min left px', k
syringeFound = True # Raise flag
# Condition for finding right side of syringe
if pxVal > 220 and syringeFound == True:
# Set right bound for indexing
rightMax = i + self.bubMax
if rightMax > self.w: rightMax = self.w
# Look at every point distance sygOffset to bubMax
for k in xrange(i+self.sygOffset,i+self.bubMax,2):
if k > self.w-5: break
j = self.findBubbleEdge(img_T[k])
if j:
xRight.append(k) # column or x
yRight.append(self.h-j) # row or y
else:
break # if no pixel found before BED break
if k == i+self.bubMax-10:
print 'Warning:: Bubble may exceed max right px', k
done = True
# If both edges of the syringe have been read you're done
if done:
break
# X is a function of Y, Y is a relation of X
xFromLeft = [] # Actually a y value but becomes x viewed from left
yFromLeft = [] # Actually an x value but becomes y viewed from left
xFromRight = [] # Actually a y value but becomes x viewed from right
yFromRight = [] # Actually an x value but becomes y viewed from right
# Fit horizontally using an offset derived from xLeft yLeft
# iterate over rows from BED up to highest bubble point
if xLeft != [] and len(xLeft) > self.minFitPoints:
for x in xrange(self.bed+1,(self.h-max(yLeft)),-1):
# Look for bubble edge horizontally
# Ignore self.bed because you are looking horizontally
y = self.findBubbleEdge(img[x][min(xLeft)-5:], useBed=False)
if y == None: break # Don't add null data points
y = y + min(xLeft)-5
xFromLeft.append(self.h-x)
yFromLeft.append(y)
### Major issue encountered here
# For some reason you must do this assignment, you cannot do all
# these operations and [::-1] in the same line. Also np.nditer only
# iterates in one direction no matter what you send it...
# very confusing implementation.
if xRight != [] and len(xRight) > self.minFitPoints:
for x in xrange(self.bed+1,(self.h-max(yRight)),-1):
# Look at row x from +5 px right of edge of bubble
# Required separate line unsure why...
array = img[x][:max(xRight)+5]
# Send reversed array because you need look from right
y = self.findBubbleEdge(array[::-1], useBed=False)
if y == None: break
y = max(xRight)-y+5
xFromRight.append(self.h-x)
yFromRight.append(y)
# Estimate diameter of bubble
if xLeft != [] and xRight != []:
diameter = max(xRight) - min(xLeft)
elif xLeft != []:
# diameter with estimated syringe width
diameter = max(xLeft) - min(xLeft) + self.sygOffset*2 + 4
elif xRight != []:
# diameter with estimated syringe width
diameter = max(xRight) - min(xRight) + self.sygOffset*2 + 4
else:
diameter = None
# Make plots from points 0 to maxFitPoints
# This will remove syringes that appear due to bright points in their
# center, and other abnormalities at the top of the drop let
caL, caR = self.fitContactAngle(xFromLeft[:self.maxFitPoints],
yFromLeft[:self.maxFitPoints],
xFromRight[:self.maxFitPoints],
yFromRight[:self.maxFitPoints], count)
# Return data or none if no data points found (diameter, caL, caR)
return diameter, caL, caR
# mouse callback function
def filterImg(self, img, rot_Matrx, imgNum, makePlot=True):
"""Filters and saves every Nth image in a video with getContactAngle()
Input:
self.itemPath = a string, path of video to be analyzed
self.workDir = a string, working directory
Parameters from config.py
Output:
A cropped, filtered, rotated .png (in debug mode)
A call to find syringe edge
A csv log file
"""
if self.isVideo: # Basically if you're not processing a video
rows,cols,channels = img.shape
if rows < self.roi[1] or cols < self.roi[3]:
print 'Warning:: ROI larger then image ', imgNum
# Rotate and crop image based on self.r / self.roi
img = cv2.warpAffine(img,rot_Matrx,(cols,rows))
# Crop to self.roi [row_min:row_max, col_min:col+max]
img_Nom = img[self.roi[0]: self.roi[1], self.roi[2]: self.roi[3]]
# Gray scale and apply a mask to remove unnecessary points
img = cv2.cvtColor(img_Nom,cv2.COLOR_BGR2GRAY) # convert to GS
mask = np.zeros([self.h,self.w], np.uint8)
mask[:,:self.mask[0]] = self.mask[2]
mask[:,self.mask[1]:] = self.mask[2]
img = cv2.add(img,mask)
# Brighten all pixels then apply binary threshold
img = cv2.add(img,self.br)
r,img = cv2.threshold(img,self.th,255,cv2.THRESH_BINARY)
else:
# Filter
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img = cv2.add(img,self.br)
r,img = cv2.threshold(img,self.th,255,cv2.THRESH_BINARY)
# Get width and height from black and white image
self.h,self.w = img.shape
# Mask filtered image to provide fake syringe
mask = np.zeros([self.h,self.w], np.uint8)
colMax = int(self.w/2 + 2)
colMin = int(self.w/2 -2)
mask[0:5,colMin:colMax] = 255
img = cv2.subtract(img,mask)
# Image specific parameters for debug plotting
savePath = self.itemPath[:-4]+'-fltrd.png'
img_Debug = img
if self.debug and makePlot: # Save debugging image
if self.isVideo:
# Video specific parameters for debug plotting
img_Nom = cv2.cvtColor(img_Nom,cv2.COLOR_BGR2GRAY)
img_Debug = np.hstack((img,img_Nom))
savePath = self.workDir+imgNum+'.png'
cv2.imwrite(savePath, img_Debug)
return img
def getAvgContactAngle(self, frames, angles):
""" Get average advancing contact angle. This algo assumes the best
average can be found between frames 240-270
Input: list of frames, list of angles
Output: float value for average advancing contact angle
"""
# frames = frames
# Make sure array is not empty
if len(frames) != len(angles):
lmin = lmax = None
if len(frames) > 280/self.n:
lmin = int(240/self.n)
lmax = int(270/self.n)
elif len(frames) > 10:
lmin = int(len(frames)/2-2)
lmax = int(len(frames)/2+2)
else:
lmin = lmax = None
# Calculate average
if lmin:
avgL = angles[lmin:lmax]
avgAdvCA = reduce(lambda x, y: x + y, avgL)/len(avgL)
else:
avgAdvCA = None
return avgAdvCA
def measureVideo(self):
"""Filters and saves every Nth image in a video with getContactAngle()
Input:
self.itemPath = a string, path of video to be analyzed
self.workDir = a string, working directory
Parameters from config.py
Output:
A cropped, filtered, rotated .png (in debug mode)
A call to find syringe edge
A csv log file
"""
count = 0
leftAngles = []
rightAngles = []
leftFrames = []
rightFrames = []
diameters = []
diameterFrames = []
makePlot = False
# Start log file
name = self.itemPath.split('\\')[-1:][0].split('_BED')[0]
logPath = self.workDir + name + '_data.csv'
logFile = open(logPath, 'wb')
log = csv.writer(logFile, delimiter=',', escapechar='|',
quoting=csv.QUOTE_NONE)
# Headers
runParams = [
'bed='+str(self.bed),
'N='+str(self.n),
'r='+str(self.r),
'roi='+str(self.roi),
'mask='+str(self.mask),
'br='+str(self.br),
'th='+str(self.th),
'dpi='+str(self.dpi),
'debug='+str(self.debug),
'resetbed='+str(self.resetbed),
'syringeMinPos='+str(self.syringMinPos),
'sygOffset='+str(self.sygOffset),
'bubMax='+str(self.bubMax),
'derivOffset='+str(self.derivOffset),
'polyDeg='+str(self.polyDeg),
'minFitPoints='+str(self.minFitPoints),
'w='+str(self.w),
'h='+str(self.h),
'assignBeds='+str(self.assignBeds),
]
log.writerow(runParams)
log.writerow(['frameNumber','diameter','leftCA','rightCA'])
# Rotation matrix centered in self.roi and rotated by self.r degrees
x_cent = int((self.roi[0]+self.roi[1])/2)
y_cent = int((self.roi[2]+self.roi[3])/2)
rot_Matrx = cv2.getRotationMatrix2D((x_cent,y_cent),self.r,1)
# Start reading input video
cap = cv2.VideoCapture(self.itemPath)
while True:
ret, frame = cap.read()
# If frameNum/N=0 is returned do analysis
if ret and (count % self.n) == 0 and count >= self.vsf:
imgNum = string.zfill(count,4) # Change 1 to 0001
# Filter image
img = self.filterImg(frame, rot_Matrx, imgNum)
# Get contact angles and plot results
diameter, caL, caR = self.findSyringeEdge(img, img.T, imgNum)
if self.debug:
print imgNum, diameter, caL, caR
# Append to lists for plots later
if caL:
leftAngles.append(caL)
leftFrames.append(count)
if caR:
rightAngles.append(caR)
rightFrames.append(count)
if diameter:
diameters.append(diameter)
diameterFrames.append(count)
# Output data to log file
log.writerow([count, diameter, caL, caR])
if count > self.vef or not ret:
cap.release()
break
count += 1
logFile.close() # Close your log file
fig = plt.figure()
ax = fig.add_subplot(111)
ax2 = ax.twinx()
# Plot
if diameters != []:
makePlot = True
ax2.plot(diameterFrames, diameters, 'g')
if leftAngles != []:
makePlot = True
ax.plot(leftFrames,leftAngles,'r')
if rightAngles != []:
makePlot = True
ax.plot(rightFrames,rightAngles,'b')
if makePlot:
ax.set_ylabel('ContactAngle (Deg)', size=8)
ax.set_xlabel('Image Number', size=8)
ax2.set_ylabel('Diameter (Px)', size=8)
ax.set_title('Contact Angle (Deg) & Diameter (Px) vs. Image Number',
size=8)
p1 = plt.Rectangle((0, 0), 1, 1, fc='g')
p2 = plt.Rectangle((0, 0), 1, 1, fc='r')
p3 = plt.Rectangle((0, 0), 1, 1, fc='b')
ax.legend([p1,p2,p3], ['Diameter','Left CA','Right CA'],
loc=1,prop={'size':8})
plotPath = self.workDir + name + '_data.svg'
savefig(plotPath) #, dpi=300, papertype='b10',
#orientation='portrait', bbox_inches='tight')
clf() # Clear your figure
# Get average advancing contact angles
avgAdvCALeft = self.getAvgContactAngle(leftFrames, leftAngles)
avgAdvCARight = self.getAvgContactAngle(rightFrames, rightAngles)
return max(diameters), avgAdvCALeft, avgAdvCARight
def mouseCallback(self,event,x,y,flags,param):
""" Mouse callback function, selects cursor point to populate self.bed
"""
if event == cv2.EVENT_LBUTTONDOWN:
self.bed = y
print 'You selected: ', x, y
def userSelectBed(self, img):
""" Opens window and prompts user to select BED
Input: Filtered image.
Output: None
"""
# Set up window & call back
windowName = 'Left click base of bubble. Press Esc to exit.'
cv2.namedWindow(windowName,cv2.WINDOW_NORMAL) # Can be resized
cv2.resizeWindow(windowName, self.w*3, self.h*3) # Resize window
cv2.setMouseCallback(windowName, self.mouseCallback) # Set callback
imgDefault = img # Ideally this resets the image later
while True:
# Plot cursor click
if self.bed:
h = self.bed
else:
h = 0
cv2.line(img, (-1000,h), (1000,h), (0,0,0))
cv2.imshow(windowName, img)
img = imgDefault
k = cv2.waitKey(4) & 0xFF # Basically delay 1000 ms
#print 'BED is currently: ', self.bed
if k == 27: # Press escape to exit
break
cv2.destroyAllWindows()
def getBED(self, img=None, imgSent=False):
"""Attempts to pull BED from file name else prompts user to select it.
Input: videoPath
Output: renames video file to store BED
"""
# Pull BED from file name assumes this file structure:
# [Name of any format exclude the string _BED]_BED###.MOV
# ### indicating an integer value for BED counted down from the top
# of the cropped and filtered image.
if '_BED' in self.itemPath and self.resetbed == False:
self.bed = int(self.itemPath.split('BED')[1][:-4])
return
# Check to see what's being worked on then manually get BED, let's
# choose frame 250 for .MOVs
if '.jpg' in self.itemPath or '.png' in self.itemPath:
img = cv2.imread(self.itemPath)
# Get width, height and channels
self.h,self.w,channels = img.shape
img = self.filterImg(img, None, None, makePlot=True)
self.userSelectBed(img)
elif imgSent: # If filtered image supplied
self.userSelectBed(img)
else: # Process as video
count = 0
# Rotation matrix centered in self.roi and rotated by self.r degrees
x_cent = int((self.roi[0]+self.roi[1])/2)
y_cent = int((self.roi[2]+self.roi[3])/2)
rot_Matrx = cv2.getRotationMatrix2D((x_cent,y_cent),self.r,1)
print self.r, rot_Matrx
# Start reading input video
cap = cv2.VideoCapture(self.itemPath)
# User selects BED from filtered image
while True:
ret, frame = cap.read()
if count == self.vidMid: #Arbitrary mid point
imgNum = '0250'
# Filter image and get BED
img = self.filterImg(frame, rot_Matrx, imgNum,
makePlot=False)
self.userSelectBed(img)
cap.release()
break
if count > self.vef or not ret:
cap.release()
break
count += 1
# Store self.bed in file name
ext = self.itemPath.split('.')[-1]
# Remove _BED###.ext
if self.resetbed and '_BED' in self.itemPath:
name = self.itemPath.split('_BED')
bedName = name[0]+'_BED'+str(self.bed)+'.' + ext
else: # Remove .ext
bedName = self.itemPath[:-4]+'_BED'+str(self.bed)+'.' + ext
if self.itemPath != bedName:
print 'Renaming: ', self.itemPath
print 'New Name: ', bedName
os.rename(self.itemPath, bedName)
self.itemPath = bedName
else:
print 'Not renaming, BED unchanged.'
print 'Name: ', self.itemPath
print 'New Name: ', bedName
def removeBED(self):
""" Remove _BED from file names
"""
# Rename file so this doesn't need to happen again
if '_BED' in self.itemPath: # Remove _BED###.MOV
name = self.itemPath.split('_BED')
bedName = name[0]+'.MOV'
print 'Renaming: ', self.itemPath
print 'New Name: ', bedName
def analyzeVideos(self):
""" Executes getContactAngle on an individual video or recursively over
an entire folder tree.
Input:
Parameters from config.py
Output:
In non debug mode it deletes folders with duplicate names.
"""
if '.MOV' in self.path: # For individual file call directly
self.workDir = self.path[:-4]+'\\'
self.itemPath = self.path
print 'Analyzing: ', self.itemPath
self.getBED() # Pull BED from filename or ask user
# If self.workDir exists and not running debug mode, delete it
if os.path.isdir(self.workDir)and not self.debug:
print 'Deleting: ', self.workDir
shutil.rmtree(self.workDir)
# Make self.workDir if it doesn't exist
if not os.path.isdir(self.workDir):
print 'Making: ', self.workDir
os.mkdir(self.workDir)
# Measure contact angles in Video and record outputs to workDir
self.measureVideo()
else: # Directory given, use os.walk
# Set up a master log file, timestamp prevents overwriting
mlPath = 'F:\\AFCC Metal FSU Testing 2014\\A3\\masterLog_'
ts = str(time.time())[2:-3] # timestamp
mlPath = mlPath + ts + '.csv'
print 'Saving master data to: ', mlPath
masterLogFile = open(mlPath, 'wb')
mlog = csv.writer(masterLogFile, delimiter=',',
quoting=csv.QUOTE_NONE)
runParams = [
'bed='+str(self.bed),
'br='+str(self.br),
'th='+str(self.th),
'dpi='+str(self.dpi),
'debug='+str(self.debug),
'resetbed='+str(self.resetbed),
'assignBeds='+str(self.assignBeds),
'syringeMinPos='+str(self.syringMinPos),
'sygOffset='+str(self.sygOffset),
'bubMax='+str(self.bubMax),
'derivOffset='+str(self.derivOffset),
'polyDeg='+str(self.polyDeg),
'minFitPoints='+str(self.minFitPoints)
]
mlog.writerow(runParams)
mlog.writerow(self.headers)
for (root, subFolders, files) in os.walk(self.path):
#Second condition forces this to run in only the index dir
for item in files:
print item
if '.MOV' in item:
if 'FIX' in item: continue # Ignore pre-filtered video
self.itemPath = root+'\\'+item
# Only remove _BED from file names
if self.removeBeds:
print 'Remove BEDs.'
self.removeBED()
continue
# Only assign beds no analysis
if self.assignBeds:
print 'Assigning BEDs.'
self.getBED() # Pull BED from filename or ask user
continue
else:
self.getBED()
print 'Analyzing: ', self.itemPath
self.workDir = self.itemPath[:-4]+'\\' # Remove .MOV
if os.path.isdir(self.workDir) and not self.debug:
print 'Deleting: ', self.workDir
shutil.rmtree(self.workDir)
if not os.path.isdir(self.workDir):
print 'Making: ', self.workDir
os.mkdir(self.workDir)
# Get averaged values for things we car about
d,caL,caR = self.measureVideo()
# Splits 20140805-12_49_15-CA7_L002A_02_BED140.MOV
# to ['CA7', 'L002A', '02', 'BED140.MOV']
info = item.split('-')[2].split('_')
# Log filename, folderID, fabID, sample#, region,
# run#, caL, caR, diameter, bed
row = [item, info[0], info[1][:-4], info[1][-4:-1],
info[1][-1], caL, caR, d, self.bed]
print row
# Write data row to master log file
# Flush to prevent data loss in crash
mlog.writerow(row)
masterLogFile.flush()
masterLogFile.close()