I have not 1 new script, but 2 (for the price of none).
First how to use the script ? ...
- start SharpCap (SC 3.2 and SC 3.3, note that I test only on the 64 bit version)
- connect QHY-174-GPS camera (I have no idea what happen if it is only the QHY-174 camera)
- switch on the LED illumination ** (for the first test, no GPS is needed, the camera must be covered)
- move the USB traffic or change the exposure time (so that SC fill the Start/End Pos values)
** I add in this new version the automatic switch-on of the LED in case that the LED is off (and switch-off after the LED calibration).
The first script is the "single" LED calibration with the actual settings (8/16 bit, exposure time, USB Traffic, ...).
I test with 16 bit => some problems (and some line code to solve it) ... the SC Start/End Position are all far to the correct values (~ 4000 delta).
If Robin correct the starting values for 16 bit, then it will be possible update the script (or it is a special case with my camera).
The second script performs the LED calibration from a list of user defined exposure time (you should modify the variable Time_List).
At the end, the script gives a summary of all the Start/End Position with the "LED-time" (for me in 8 bit all the time ~ 0.025 ms difference to the exposure time in SC).
Code: Select all
import time
import math
# (Begin) - Parameter to be changed by the user
search_loop = 10 # Number of fine search
power_threshold = 4 # (10**power_threshold) digit
slope = 1.2 # value between 1.0 and 1.3
# (End) - Parameter to be changed by the user
Cal_End_Pos = SharpCap.SelectedCamera.Controls[19].Value
Cal_Start_Pos = SharpCap.SelectedCamera.Controls[20].Value
Colour_Space = SharpCap.SelectedCamera.Controls.ColourSpace.Value
LED_status = SharpCap.SelectedCamera.Controls[21].Value
print LED_status
if (LED_status == "Off"):
SharpCap.SelectedCamera.Controls[21].Value = "On"
print SharpCap.SelectedCamera.Controls[18]
print("Calibration Start Pos Adjust: %i") % (Cal_Start_Pos)
print("Calibration End Pos Adjust : %i") % (Cal_End_Pos)
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)
print
cloneRect = SharpCap.Transforms.SelectionRect
print("Rectangle Selection : "), cloneRect
print
expos_ms = SharpCap.SelectedCamera.Controls.Exposure.ExposureMs
#wait_time = 2.1 *(expos_ms / 1000.0)
wait_time = 2.1 *(expos_ms / 1000.0) + 0.1
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_L(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") % (Pnew, L[-1])
if (abs((L[-1] - average)/average) < 0.05):
break
M = zip(P,L)
M.sort()
return(M)
def LED_Start_search_R(Pos, LED, n):
P = Pos
L = LED
loop = 0
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") % (Pnew, L[-1])
if (abs((L[-1] - average)/average) < 0.05):
break
M = zip(P,L)
M.sort()
return(M)
dumpdata = True
SharpCap.CaptureEvent += evthandler
monitorFrames()
# *****************************************************************************
# LED Calibration Start Position Adjustment
if (Colour_Space == "MONO16"):
cal_step = math.floor(math.log10(Cal_Start_Pos))
if (Colour_Space == "MONO8"):
cal_step = math.floor(math.log10(Cal_Start_Pos)) - 1
Pos = [Cal_Start_Pos]
if (cal_step > power_threshold):
cal_step = power_threshold
print("Digit position selection: "),
for p in range(int(cal_step), 0, -1):
print 10**p,
if (Colour_Space == "MONO16"):
Pos.append(Cal_Start_Pos + 10**(p+1))
Pos.append(Cal_Start_Pos + 10**p)
Pos.append(Cal_Start_Pos - 10**p)
Pos.sort()
print
print("SC Cal_Start_Pos : "), Cal_Start_Pos
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") % (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)):
break
print
print("Min / Max : "), min(LED), " ", max(LED)
print("average: "), (min(LED) + max(LED)) / 2.0
Pos = Pos[0 : len(LED)]
M = zip(Pos, LED)
M.sort()
print
time.sleep(wait_time)
Result = LED_Start_search_L(Pos, LED, search_loop)
Cal_Start_Pos = Pos[-1]
SharpCap.SelectedCamera.Controls[20].Value = int(Cal_Start_Pos)
print
# END LED Calibration Start Position Adjustment
# *****************************************************************************
# *****************************************************************************
# LED Calibration End Position Adjustment
cal_step = math.floor(math.log10(Cal_End_Pos)) - 1
Pos = [Cal_End_Pos]
if (cal_step > power_threshold):
cal_step = power_threshold
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("SC Cal_End_Pos : "), Cal_End_Pos
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)):
break
print
print("Min / Max : "), min(LED), " ", max(LED)
print("Half height: "), (min(LED) + max(LED)) / 2.0
Pos = Pos[0 : len(LED)]
M = zip(Pos, LED)
M.sort()
print
time.sleep(wait_time)
Result = LED_Start_search_R(Pos, LED, search_loop)
Cal_End_Pos = Pos[-1]
SharpCap.SelectedCamera.Controls[19].Value = int(Cal_End_Pos)
# END LED Calibration End Position Adjustment
# *****************************************************************************
dumpdata = False
print
print("***************************")
print("Cal_Start_Pos : %i") % (Cal_Start_Pos)
print("Cal_En_Pos : %i") % (Cal_End_Pos)
print("***************************")
if (LED_status == "Off"):
SharpCap.SelectedCamera.Controls[21].Value = "Off"
Code: Select all
import time
import math
# (Begin) - Parameter to be changed by the user
search_loop = 12 # Number of fine search
power_threshold = 4 # (10**power_threshold) digit
slope = 1.2 # value between 1.0 and 1.3
Time_List = [20] # [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 = [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,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]
# (End) - Parameter to be changed by the user
SharpCap.SelectedCamera.Controls.Exposure.ExposureMs = Time_List[0]
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") % (Pnew, L[-1])
if (abs((L[-1] - average)/average) < 0.1):
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") % (Pnew, L[-1])
if (abs((L[-1] - average)/average) < 0.1):
break
M = zip(P,L)
M.sort()
return(M)
# LED Calibration Start Position Adjustment
def LED_Start(expos_ms):
if (Colour_Space == "MONO16"):
cal_step = math.floor(math.log10(Cal_Start_Pos))
if (Colour_Space == "MONO8"):
cal_step = math.floor(math.log10(Cal_Start_Pos)) - 1
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,
if (Colour_Space == "MONO16"):
Pos.append(Cal_Start_Pos + 10**(p+1))
Pos.append(Cal_Start_Pos + 10**p)
Pos.append(Cal_Start_Pos - 10**p)
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") % (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_Start_search(Pos, LED, search_loop)
SharpCap.SelectedCamera.Controls[20].Value = int(Pos[-1])
print
return(Pos[-1])
# LED Calibration End Position Adjustment
def LED_End(expos_ms):
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)
SharpCap.SelectedCamera.Controls[19].Value = int(Pos[-1])
print
return(Pos[-1])
# *****************************************************************************
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.1
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 "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()
print
print
print("Camera Settings:")
print("========================================================")
print("Colour Space : "), SharpCap.SelectedCamera.Controls.ColourSpace.Value
print("Binning : "), SharpCap.SelectedCamera.Controls.Binning.Value
print("USB Traffic : "), SharpCap.SelectedCamera.Controls.Usb.Value
print
print "Calculation time: ", round((time_end - time_start)/60, 2), " minutes"
print
print(" [ms] Cal_Start Cal_End Exp. LED-time Delta SC_Start SC_End ")
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])
dumpdata = False
if (LED_status == "Off"):
SharpCap.SelectedCamera.Controls[21].Value = "Off"
For example here the result of several LED calibration ...
You can try to calibrate manually the same list ... it is now much quicker with my script
In some case, the LED calibration time can be shorter, but for the use for all exposure time, I add some delay before a new image is used after the setting of the LED Start/End Position.
If you find a combination of settings that produce an error, please communicate the used camera settings.
Feature request for SC 3.3.1 ... a button in the control panel "GPS Controls" that perform the same LED calibration as my script.