Automatic adjustment of the LED (QHY-174-GPS)
Posted: Wed Sep 09, 2020 11:23 pm
Hello,
Here a (not final) version of my script for setting automatically the Start Pos and End Pos of the LED illumination.
The script saves an image in a temporary file on the disc (ideally, it would use the image directly from the memory ... it is planed for a following version of the script). The user can modify the directory (or create C:\Temp\) before use of the script.
The script sets the Start/End Pos values with +/- 10, +/- 100 up to the "power_threshold" value (that permits to limit the number of steps when the value is very large). The script performs an iterative search with a defined number of steps "search_loop".
A future version will use the fine steps results for solving the transition curve at the inflection point.
And I wish to have a visual view of the solution with some nice curves.
I test for several parameters (binning, exposure time, USB traffic, ...) in the most of the case, the script found both adjustment position.
Sometimes it does not found ... I see some "problems" ...
For example for the camera parameters: 1900x1200 binned with 2x2, 10 ms exposure and USB = 9:
SC guess value : 232776 / 978280 (false)
Manual search : 7090 / 1130250 (false because of "third transition"
Script after better starting values: 1130257 / 1874724 (the value can differ on other camera)
Note that the most of my tests were correct with the USB traffic = 0 and binning 1x1.
How to use the script ? ...
- start SharpCap
- 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)
- show "The Image Histogram" window
- activate the "FX Selection Area" (for showing the red selection rectangle)
- move and scale the selection rectangle above the most intense region of the LED illumination
- start the script
- maybe modify "power_threshold", "search_loop" parameters (first to not modify larger than 10)
And here the script:
Regards,
Jean-Francois
Here a (not final) version of my script for setting automatically the Start Pos and End Pos of the LED illumination.
The script saves an image in a temporary file on the disc (ideally, it would use the image directly from the memory ... it is planed for a following version of the script). The user can modify the directory (or create C:\Temp\) before use of the script.
The script sets the Start/End Pos values with +/- 10, +/- 100 up to the "power_threshold" value (that permits to limit the number of steps when the value is very large). The script performs an iterative search with a defined number of steps "search_loop".
A future version will use the fine steps results for solving the transition curve at the inflection point.
And I wish to have a visual view of the solution with some nice curves.
I test for several parameters (binning, exposure time, USB traffic, ...) in the most of the case, the script found both adjustment position.
Sometimes it does not found ... I see some "problems" ...
For example for the camera parameters: 1900x1200 binned with 2x2, 10 ms exposure and USB = 9:
SC guess value : 232776 / 978280 (false)
Manual search : 7090 / 1130250 (false because of "third transition"
Script after better starting values: 1130257 / 1874724 (the value can differ on other camera)
Note that the most of my tests were correct with the USB traffic = 0 and binning 1x1.
How to use the script ? ...
- start SharpCap
- 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)
- show "The Image Histogram" window
- activate the "FX Selection Area" (for showing the red selection rectangle)
- move and scale the selection rectangle above the most intense region of the LED illumination
- start the script
- maybe modify "power_threshold", "search_loop" parameters (first to not modify larger than 10)
And here the script:
Code: Select all
import clr
clr.AddReference("System.Drawing")
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
import sys
import time
import System.Drawing
import math
# (Begin) - Parameter to be changed by the user
temp_file = "c:\Temp\LED_test.tif" # Change the directory, if necessary
search_loop = 8 # Number of fine search
power_threshold = 4 # (10**power_threshold) digit
# (End) - Parameter to be changed by the user
print("GPS Calibration LED on/off : %s") % (SharpCap.SelectedCamera.Controls[21].Value)
print
Cal_End_Pos = SharpCap.SelectedCamera.Controls[19].Value
Cal_Start_Pos = SharpCap.SelectedCamera.Controls[20].Value
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 Stabilizazion : %s") % (SharpCap.SelectedCamera.Controls[22].Value)
print("GPS : %s") % (SharpCap.SelectedCamera.Controls[23].Value)
print
SharpCap.SelectedCamera.Controls.OutputFormat.Value = 'TIFF files (*.tif)'
cloneRect = SharpCap.Transforms.SelectionRect
print("Rectangle Selection : "), cloneRect
print
expos_ms = SharpCap.SelectedCamera.Controls.Exposure.ExposureMs
def rect_average():
time.sleep(expos_ms / 990) # Wait for the frame "used" the Pos Adjust value
SharpCap.SelectedCamera.CaptureSingleFrameTo(temp_file)
bm = System.Drawing.Bitmap(temp_file)
cloneBitmap = bm.Clone(cloneRect, bm.PixelFormat)
sum = 0.0
nb_pixel = cloneRect.Height * cloneRect.Width
for i in range(cloneRect.Height):
for j in range(cloneRect.Width):
pixel = cloneBitmap.GetPixel(j,i)
sum += float(pixel.R)
bm.Dispose()
cloneBitmap.Dispose()
return(sum/nb_pixel)
def Show_matrix(M):
for i in range(len(M)):
for j in range(len(M[0])):
print str(M[i][j]) + " ",
print
return
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 = Pnew
P.append(Pnew)
L.append(rect_average())
print Pnew, " ", L[-1]
M = zip(P,L)
M.sort()
return(M)
def LED_Start_search_R(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 = Pnew
P.append(Pnew)
L.append(rect_average())
print Pnew, " ", L[-1]
M = zip(P,L)
M.sort()
return(M)
# *****************************************************************************
# LED Calibration Start Position Adjustment
# Need to be sure that the last value is used
SharpCap.SelectedCamera.Controls[20].Value = Cal_Start_Pos + 1
SharpCap.SelectedCamera.Controls[20].Value = Cal_Start_Pos - 1
cal_step = math.floor(math.log10(Cal_Start_Pos)) - 1
Pos = [Cal_Start_Pos]
print cal_step
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_Start_Pos + 10**p)
Pos.append(Cal_Start_Pos - 10**p)
print
print("SC Cal_Start_Pos : "), Cal_Start_Pos
print("Cal_Start_Pos list: "), Pos
LED = []
for L in range(0,len(Pos)):
SharpCap.SelectedCamera.Controls[20].Value = Pos[L]
LED.append(round(rect_average(),3))
print Pos[L], " ", LED[L]
print
print("Min / Max : "), min(LED), " ", max(LED)
print("average: "), (min(LED) + max(LED)) / 2.0
M = zip(Pos, LED)
M.sort()
print
Result = LED_Start_search_L(Pos, LED, search_loop)
print
print
# END LED Calibration Start Position Adjustment
# *****************************************************************************
# *****************************************************************************
# LED Calibration End Position Adjustment
# Need to be sure that the last value is used
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]
print cal_step
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)
print
print("SC Cal_End_Pos : "), Cal_End_Pos
print("Cal_End_Pos list: "), Pos
LED = []
for L in range(0,len(Pos)):
SharpCap.SelectedCamera.Controls[19].Value = Pos[L]
LED.append(round(rect_average(),3))
print Pos[L], " ", LED[L]
print
print("Min / Max : "), min(LED), " ", max(LED)
print("Half height: "), (min(LED) + max(LED)) / 2.0
M = zip(Pos, LED)
M.sort()
print
Result = LED_Start_search_R(Pos, LED, search_loop)
# END LED Calibration End Position Adjustment
# *****************************************************************************
Regards,
Jean-Francois