In the past week, I do a lot of modification in my scripts and some tests.
- new calculation of the Start/End values with a least square fit algorithm (after linearisation of the Fermi/exponential function)
- add the export of the results in a text file (the file location can be changed ... code line 351/352)
I improve the search in the 16 bit mode, but it is sometimes difficult to find the correct values (because the Start/End value from SharpCap are sometimes far away from the correct values).
Code: Select all
import time
import math
import System
from System.IO import Directory
directory = Directory.GetCurrentDirectory()
# (Begin) - Parameter to be changed by the user
search_loop = 20 # Number of fine search
power_threshold = 4 # (10**power_threshold) digit
slope = 1.2 # value between 1.0 and 1.3
Time_List = [5] # [ms]
#Time_List = [1.0,1.05,1.1,1.15,1.2,1.25,1.3,1.35,1.4,1.45,1.5,1.55,1.6,1.65,1.7,1.75,1.8,1.85,1.9,1.95,2.0] # [ms]
#Time_List = [1.0,1.2,1.4,1.6,1.8,2.0,2.2,2.4,2.6,2.8,3.0,3.2,3.4,3.6,3.8,4.0,4.2,4.4,4.6,4.8,5.0,5.2,5.4,5.6,5.8,6.0,6.2,6.4,6.6,6.8,7.0,7.2,7.4,7.6,7.8,8.0,8.2,8.4,8.6,8.8,9.0,9.2,9.4,9.6,9.8,10.0] # [ms]
#Time_List = [7.05,7.1,7.15,7.2,7.25,7.3,7.35,7.4,7.45,7.5,7.55,7.6,7.65,7.7,7.75,7.8,7.85,7.9,7.95] # [ms]
#Time_List = [1,1.5,2,2.5,3,3.5,4,5,6,7,7.2,7.4,7.6,7.8,8,9,10,11,12,13,14,15,20,40,60,80,100,150,200,250,500,750,1000] # [ms]
#Time_List = [10,20,30,40,50,60,70,80,90,100]
#Time_List = [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,32,34,36,38,40,42,44,46,48,50] # [ms]
#Time_List = [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] # [ms]
#Time_List = [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] # [ms]
#Time_List = [12.1,12.2,12.3,12.4,12.5,12.6,12.7,12.8,12.9] # [ms]
#Time_List = [5,10,20,30,40,50,60,70,80,90,100,150,200,250,300,350,400,450,500,600,800,1000,1300,2000,3000] # [ms]
# (End) - Parameter to be changed by the user
SharpCap.SelectedCamera.Controls.Exposure.ExposureMs = Time_List[0]
Cal_Start_Pos = SharpCap.SelectedCamera.Controls[20].Value
Cal_End_Pos = SharpCap.SelectedCamera.Controls[19].Value
Colour_Space = SharpCap.SelectedCamera.Controls.ColourSpace.Value
LED_status = SharpCap.SelectedCamera.Controls[21].Value
if (LED_status == "Off"):
SharpCap.SelectedCamera.Controls[21].Value = "On"
print "Date:", time.ctime()
print SharpCap.SelectedCamera.Controls[18]
print("GPS Calibration LED on/off : %s") % (SharpCap.SelectedCamera.Controls[21].Value)
print("GPS Freq Stabilization : %s") % (SharpCap.SelectedCamera.Controls[22].Value)
print("GPS : %s") % (SharpCap.SelectedCamera.Controls[23].Value)
cloneRect = SharpCap.Transforms.SelectionRect
print("Rectangle Selection : "), cloneRect
print
def framehandler(sender, args):
if (dumpdata):
global Mean
cutout = args.Frame.CutROI(SharpCap.Transforms.SelectionRect)
Stat = cutout.GetStats()
Mean = Stat.Item1
cutout.Release()
def evthandler(sender, args):
if (SharpCap.SelectedCamera != None):
SharpCap.SelectedCamera.FrameCaptured -= framehandler
def monitorFrames():
SharpCap.SelectedCamera.FrameCaptured += framehandler
def Search_next_L(M, a):
k = 0
while (M[k][1] < a):
k = k + 1
return(math.floor((M[k][0] + M[k-1][0]) / 2.0))
def Search_next_R(M, a):
k = 0
while (M[k][1] > a):
k = k + 1
return(math.floor((M[k][0] + M[k-1][0]) / 2.0))
def LED_Start_search(Pos, LED, n):
P = Pos
L = LED
for i in range(0,n):
average = (min(L) + max(L)) / 2.0
M = zip(P,L)
M.sort()
Pnew = Search_next_L(M,average)
SharpCap.SelectedCamera.Controls[20].Value = int(Pnew)
time.sleep(wait_time)
P.append(Pnew)
L.append(Mean)
# print("%i %7.3f") % (P[-1], L[-1])
if (abs((L[-1] - average)/average) < 0.05):
break
M = zip(P,L)
M.sort()
return(M)
def LED_End_search(Pos, LED, n):
P = Pos
L = LED
for i in range(0,n):
average = (min(L) + max(L)) / 2.0
M = zip(P,L)
M.sort()
Pnew = Search_next_R(M,average)
SharpCap.SelectedCamera.Controls[19].Value = int(Pnew)
time.sleep(wait_time)
P.append(Pnew)
L.append(Mean)
# print("%i %7.3f") % (P[-1], L[-1])
if (abs((L[-1] - average)/average) < 0.05):
break
M = zip(P,L)
M.sort()
return(M)
# LED Calibration Start Position Adjustment
def LED_Start(expos_ms):
Cal_Start_Pos = SharpCap.SelectedCamera.Controls[20].Value
if (Colour_Space == "MONO16"):
cal_step = math.floor(math.log10(Cal_Start_Pos)) - 0
if (Colour_Space == "MONO8"):
cal_step = math.floor(math.log10(Cal_Start_Pos)) - 0
Pos = [Cal_Start_Pos]
if (cal_step > power_threshold):
cal_step = power_threshold
print
print("Digit position selection: "),
for p in range(int(cal_step), 0, -1):
print 10**p,
Pos.append(Cal_Start_Pos + 10**p)
Pos.append(Cal_Start_Pos - 10**p)
if (Colour_Space == "MONO16"):
Pos.append(Cal_Start_Pos + 10**(cal_step+1))
Pos.sort()
print
print("Cal_Start_Pos list: "), Pos
LED = []
loop = 1000
for L in range(0,len(Pos)):
SharpCap.SelectedCamera.Controls[20].Value = int(Pos[L])
time.sleep(wait_time)
LED.append(Mean)
if (L==0):
print("%i %8.3f") % (int(Pos[L]), LED[L])
else:
print("%i %8.3f") % (int(Pos[L]), LED[L]),
average = (LED[L]+LED[L-1])/2.0
diff = LED[L]-LED[L-1]
print
if (abs(diff / average) > slope):
loop = L
if (loop == (L - 1)):
print "Break"
break
print("Min / Max : %6.3f %6.3f") % (min(LED), max(LED))
print("Half height: %6.3f") % ((min(LED) + max(LED)) / 2.0)
Pos = Pos[0 : len(LED)]
M = zip(Pos, LED)
M.sort()
time.sleep(wait_time)
Result = LED_Start_search(Pos, LED, search_loop)
Pos, LED = zip(*Result)
print
l_min = min(LED) * 1.1
l_max = max(LED) * 0.95
Pos_m = []
LED_m = []
Y_m = []
for i in range(0,len(Pos)):
if (LED[i]>l_min) & (LED[i]<l_max):
Pos_m.append(Pos[i])
LED_m.append(LED[i])
Y_m.append(-(math.log((max(LED) - min(LED)) / (LED[i] - min(LED)) - 1.0)))
print("%i %10.4f") % (Pos_m[-1], LED_m[-1])
if (len(Pos_m) < 2):
# SharpCap.SelectedCamera.Controls[20].Value = int(Pos[-1])
print("Direct value:")
print("%i %10.4f") % (Pos[-1], LED[-1])
return(Pos[-1])
print
if (len(Pos_m) > 1.5):
Sx2 = 0
Sx = 0
Vx2 = []
Vx = []
n = len(Pos_m)
for i in range(0,len(Pos_m)):
xi = Pos_m[i] - Pos_m[0]
Vx.append(xi)
Vx2.append(xi**2)
Sx = Sx + xi
Sx2 = Sx2 + xi**2
Det = float(Sx2 * n - Sx * Sx)
Inv = [[n / Det,-Sx / Det],[-Sx / Det,Sx2 / Det]]
a = 0
b = 0
for i in range(0,len(Pos_m)):
a = a + (Inv[0][0]*Vx2[i] + Inv[0][1]) * LED_m[i]
b = b + (Inv[1][0]*Vx2[i] + Inv[1][1]) * LED_m[i]
k = a
m = -b / a
Cal_Start_Pos = round(Pos_m[0] + m)
print("Optimisation: n = %i, k = %10.6f, m = %10.6f, Cal Start Pos = %i") % (n,k,m,Cal_Start_Pos)
SharpCap.SelectedCamera.Controls[20].Value = int(Cal_Start_Pos)
return(Cal_Start_Pos)
# LED Calibration End Position Adjustment
def LED_End(expos_ms):
Cal_End_Pos = SharpCap.SelectedCamera.Controls[19].Value
SharpCap.SelectedCamera.Controls[19].Value = Cal_End_Pos + 1
SharpCap.SelectedCamera.Controls[19].Value = Cal_End_Pos - 1
cal_step = math.floor(math.log10(Cal_End_Pos)) - 1
Pos = [Cal_End_Pos]
if (cal_step > power_threshold):
cal_step = power_threshold
print
print("Digit position selection: "),
for p in range(int(cal_step), 0, -1):
print 10**p,
Pos.append(Cal_End_Pos + 10**p)
Pos.append(Cal_End_Pos - 10**p)
Pos.sort()
print
print("Cal_End_Pos list: "), Pos
LED = []
loop = 1000
for L in range(0,len(Pos)):
SharpCap.SelectedCamera.Controls[19].Value = int(Pos[L])
time.sleep(wait_time)
LED.append(Mean)
if (L==0):
print("%i %8.3f") % (Pos[L], LED[L])
else:
print("%i %8.3f") % (Pos[L], LED[L]),
average = (LED[L]+LED[L-1])/2.0
diff = LED[L]-LED[L-1]
print
if (abs(diff / average) > slope):
loop = L
if (loop == (L - 1)):
print "Break"
break
print("Min / Max : %6.3f %6.3f") % (min(LED), max(LED))
print("Half height: %6.3f") % ((min(LED) + max(LED)) / 2.0)
Pos = Pos[0 : len(LED)]
M = zip(Pos, LED)
M.sort()
time.sleep(wait_time)
Result = LED_End_search(Pos, LED, search_loop)
Pos, LED = zip(*Result)
print
l_min = min(LED) * 1.1
l_max = max(LED) * 0.95
Pos_m = []
LED_m = []
Y_m = []
for i in range(0,len(Pos)):
if (LED[i]>l_min) & (LED[i]<l_max):
Pos_m.append(Pos[i])
LED_m.append(LED[i])
Y_m.append((math.log((max(LED) - min(LED)) / (LED[i] - min(LED)) - 1.0)))
print("%i %10.6f") % (Pos_m[-1], LED_m[-1])
print
if (len(Pos_m) < 2):
# SharpCap.SelectedCamera.Controls[19].Value = int(Pos[-1])
print("Direct value:")
print("%i %10.4f") % (Pos[-1], LED[-1])
return(Pos[-1])
if (len(Pos_m) > 1.5):
Sx2 = 0
Sx = 0
Vx2 = []
Vx = []
n = len(Pos_m)
for i in range(0,len(Pos_m)):
xi = Pos_m[i] - Pos_m[0]
Vx.append(xi)
Vx2.append(xi**2)
Sx = Sx + xi
Sx2 = Sx2 + xi**2
Det = float(Sx2 * n - Sx * Sx)
Inv = [[n / Det,-Sx / Det],[-Sx / Det,Sx2 / Det]]
a = 0
b = 0
for i in range(0,len(Pos_m)):
a = a + (Inv[0][0]*Vx2[i] + Inv[0][1]) * LED_m[i]
b = b + (Inv[1][0]*Vx2[i] + Inv[1][1]) * LED_m[i]
k = a
m = -b / a
Cal_End_Pos = round(Pos_m[0] + m)
print("Optimisation: n = %i, k = %10.6f, m = %10.6f, Cal End Pos = %i") % (n,k,m,Cal_End_Pos)
SharpCap.SelectedCamera.Controls[19].Value = int(Cal_End_Pos)
return(Cal_End_Pos)
# *****************************************************************************
print
print "Time sequence [ms] :", Time_List
Cal_Start = []
Cal_End = []
SC_Start = []
SC_End = []
time_start = time.clock()
dumpdata = True
SharpCap.CaptureEvent += evthandler
monitorFrames()
for T in range(0,len(Time_List)):
SharpCap.SelectedCamera.Controls.Exposure.ExposureMs = Time_List[T]
expos_ms = SharpCap.SelectedCamera.Controls.Exposure.ExposureMs
wait_time = 2.1 *(expos_ms / 1000.0) + 0.2
Cal_End_Pos = SharpCap.SelectedCamera.Controls[19].Value
Cal_Start_Pos = SharpCap.SelectedCamera.Controls[20].Value
SC_End.append(int(Cal_End_Pos))
SC_Start.append(int(Cal_Start_Pos))
print
print
print "Exposure Time: ", Time_List[T], " ms"
print("=================================")
Cal_Start.append(int(LED_Start(Time_List[T])))
Cal_End.append(int(LED_End(Time_List[T])))
SharpCap.SelectedCamera.Controls[19].Value = Cal_End[-1]
SharpCap.SelectedCamera.Controls[20].Value = Cal_Start[-1]
time_end = time.clock()
date = time.localtime()
file_date = time.strftime("_%Y%m%d_%H%M%S",date)
file_name = directory + '\\LED_'
#file_name = 'D:\\Scripts\\GPS_control\\LED_'
file_name = file_name + "_" + str(SharpCap.SelectedCamera.Controls.ColourSpace.Value)
file_name = file_name + "_" + str(SharpCap.SelectedCamera.Controls.Resolution.Value)
file_name = file_name + "_" + str(SharpCap.SelectedCamera.Controls.Binning.Value)
file_name = file_name + "_USB" + str(SharpCap.SelectedCamera.Controls.Usb.Value)
file_name = file_name + file_date + '_.txt'
print
print
with open(file_name, "w") as myfile:
print("Camera Settings:")
myfile.writelines("Camera Settings:" + "\n")
print("========================================================")
print("Colour Space : "), SharpCap.SelectedCamera.Controls.ColourSpace.Value
myfile.writelines(("Colour Space :, %s \n") % (str(SharpCap.SelectedCamera.Controls.ColourSpace.Value)))
print("Binning : "), SharpCap.SelectedCamera.Controls.Binning.Value
myfile.writelines(("Binning :, %s \n") % (str(SharpCap.SelectedCamera.Controls.Binning.Value)))
print("USB Traffic : "), SharpCap.SelectedCamera.Controls.Usb.Value
myfile.writelines(("USB Traffic :, %s \n") % (str(SharpCap.SelectedCamera.Controls.Usb.Value)))
print
print "Calculation time: ", round((time_end - time_start)/60, 2), " minutes"
myfile.writelines(("Calculation time:, %s \n") % (str(round((time_end - time_start)/60, 2))))
print
print(" [ms] Cal_Start Cal_End Exp. LED-time Delta SC_Start SC_End ")
myfile.writelines("[ms],Cal_Start,Cal_End,Exp.,LED-time,Delta,SC_Start,SC_End" + "\n")
print("================================================================================")
for T in range(0,len(Time_List)):
LED_time = (Cal_End[T] - Cal_Start[T]) / 75000.0
Diff_time = Time_List[T] - LED_time
print("%5s %10s %10s %10.2f %10.3f %8.3f") % (Time_List[T],Cal_Start[T],Cal_End[T],Time_List[T],LED_time,Diff_time),
print("%10s %10s") % (SC_Start[T],SC_End[T])
myfile.writelines(("%5s,%10s,%10s,%10.2f,%10.3f,%8.3f,%10s,%10s") % (Time_List[T],Cal_Start[T],Cal_End[T],Time_List[T],LED_time,Diff_time,SC_Start[T],SC_End[T]) + "\n")
dumpdata = False
myfile.close()
if (LED_status == "Off"):
SharpCap.SelectedCamera.Controls[21].Value = "Off"