How To Add Time Delay Between Array of Sequential Exporsures

Discussions on extending SharpCap using the built in Python scripting functionality
skithebird
Posts: 12
Joined: Tue Sep 15, 2020 7:39 pm

How To Add Time Delay Between Array of Sequential Exporsures

Post by skithebird » Tue Sep 15, 2020 8:50 pm

This is my first post, and I'm a newbie to Python and SharpCap scripting. I've been attempting to collect a series of 20 sequentially larger (and relatively short) exposures to test the linearity of my camera (ZWO ASI 178MC) under various conditions. This camera and SharpCap have been playing nicely together for single manual snapshots. But when the script that I've written runs, the images that are saved appear quite erratic in mean intensity. I suspected a timing issue between the code, SharpCap and my camera. So I've tried adding in a second capture line and even a 2 second pause between exposures, allowing time for synchronization. Here's my code so far. I'm using two sequential capture single frame lines because without this double exposure, the saved results are much more erratic (see last image below).

print SharpCap.Cameras
import time
print time
SharpCap.SelectedCamera = SharpCap.Cameras[0]# Find( lambda x:x.DeviceName == "Test Camera 2 (High Speed)") # edit to grab from your camera
SharpCap.SelectedCamera.Controls.Gain.Value = 383 # Set the gain to 383
Path = "c:\Documents\white1\exposure" # edit this to match your computer folder structure
type = ".png"

exposure_array = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0] # edit to select exposure

for exposure in exposure_array : # Loop through all exposures
SharpCap.SelectedCamera.Controls.Exposure.Value = exposure # Set the camera exposure
SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + type) # snap a photo, save in directory and name according to exposure
time.sleep(2)
SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + type) # repeat added to try to avoid erratic exposures

My 20 desired exposures are 100 ms, 200 ms, 300 ms ... up to 2 seconds. I'm saving the exposure data to a folder (documents) and a sub-folder (white1). Everything looks fine in Camera Control menu while this is running. I see the exposures advancing properly. All data is saved properly. But when I use ImageJ to analyze the saved images I see an unanticipated variation in image intensity. I import this image sequence. I choose an area in the field. Then I plot a z-axis profile of the 20-layer stack. And here's what I see.
Z Axis Profile of sequentially saved images.
Z Axis Profile of sequentially saved images.
Exposure_Stack_Profile.JPG (39.96 KiB) Viewed 491 times
And to make matters more confusing to me, I see less erratic data without the time delays.
Same Sequential exposure series without time delays.
Same Sequential exposure series without time delays.
Exposure_Stack_Profile_No_Delays.JPG (37.81 KiB) Viewed 491 times
And here's what happens when I only used a single incidence of the code line that captures single frames.
Code revised for only one single frame capture line
Code revised for only one single frame capture line
Exposure_Stack_Profile_One_Single_Frame_Capture_Line.JPG (42.5 KiB) Viewed 491 times
Any ideas as to what's going on? Are my time delays properly scripted? Is my syntax correct for capturing the sequential exposures without a time delay? SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + type)

Thanks!

Bob
Attachments
Exposure_Stack_Profile_One_Single_Frame_Capture_Line.JPG
Exposure_Stack_Profile_One_Single_Frame_Capture_Line.JPG (42.5 KiB) Viewed 491 times

User avatar
admin
Site Admin
Posts: 5150
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: How To Add Time Delay Between Array of Sequential Exporsures

Post by admin » Wed Sep 16, 2020 6:16 pm

Hi,

what you have to remember is that SharpCap currently runs like a video camera, so it is taking images all the time. When you send a request to change the exposure, there is no guarantee that the request will be honoured in the next frame that arrives. SharpCap (or the camera software that SharpCap talks to) may do either of the following things

* Finish the frame it is currently on at the original exposure value and then start a new frame with the new exposure value
* finish the frame it is currently on as soon as possible and then start a new frame with the new exposure value (current frame finishes early)

In addition to that, even if SharpCap finishes the frame as quickly as possible your script may beat it by starting the capture before the current frame is complete which would then capture the frame that is being finished early. Just to add to the confusion, for real hardware cameras there may be a queue of frames (one being captured by the camera, one in the camera's memory, one being transferred from the camera to the computer, one being prepared for SharpCap by the cameras SDK software) – if you change the exposure then you may have to wait for several frames to pass before you are sure that the new frame will represent the changed exposure value. In SharpCap's own sensor analysis code, it typically waits five frames after a setting change before taking measurements.

A better approach may be to try to do this in the beta version of SharpCap 3.3 – you can put the camera into still mode (via scripting set camera.LiveView = False) - once you've done that the camera will only be taking images on demand, so hopefully the whole problem go away.

Cheers, Robin

Jean-Francois
Posts: 54
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

Re: How To Add Time Delay Between Array of Sequential Exporsures

Post by Jean-Francois » Wed Sep 16, 2020 9:26 pm

Hello,

I can confirm that it could be annoying to take an image before the camera receive/use the new setting(s).
It is necessary to wait until the camera sends the image with the new setting(s) to SharpCap.
I program a script for the LED calibration of the QHY-174-GPS camera.
It is important to wait a sufficient time for analyzing the correct image.

Regards,
Jean-Francois

skithebird
Posts: 12
Joined: Tue Sep 15, 2020 7:39 pm

Re: How To Add Time Delay Between Array of Sequential Exporsures

Post by skithebird » Thu Sep 17, 2020 7:47 pm

Hi Robin,

This is what we tried with 3.3 Beta.

print SharpCap.Cameras

SharpCap.SelectedCamera = SharpCap.Cameras[0]# Find( lambda x:x.DeviceName == "Test Camera 2 (High Speed)") # edit to grab from your camera
SharpCap.SelectedCamera.Controls.Gain.Value = 383 # Set the gain to 383
#camera.LiveView = False
SharpCap.SelectedCamera.LiveView = False
Path = "c:\Documents\white1\exposure" # bob edit this to match your computer folder structure
type = ".png"

exposure_array = [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0] # edit to select exposure

for exposure in exposure_array : # Loop through all exposures
SharpCap.SelectedCamera.Controls.Exposure.Value = exposure # Set the camera exposure
SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + type)
SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + type) # snap a photo, save in directory and name according to exposure

But unfortunately the result has been even more erratic.

1 1562.691
2 1285.068
3 1597.153
4 1608.037
5 1614.227
6 1637.610
7 1651.694
8 1664.256
9 1670.082
10 769.980
11 778.331
12 1408.211
13 1712.209
14 1442.876
15 1444.412
16 1767.995
17 1467.847
18 1494.448
19 1493.239
20 1682.237
21 1527.037
Exposure_Stack_Profile_BETA3.3_1.JPG
Exposure_Stack_Profile_BETA3.3_1.JPG (33.41 KiB) Viewed 464 times
Any idea what to try next? Any chance we can work on this together live? We've tried giving this long-long waits.

Bob

skithebird
Posts: 12
Joined: Tue Sep 15, 2020 7:39 pm

Re: How To Add Time Delay Between Array of Sequential Exporsures

Post by skithebird » Thu Sep 17, 2020 8:42 pm

Update Robin,

We're still foundering. It sure seems to us that the software isn't yet reproducing the 0.1 second increments in exposure, somehow...

Here's our latest code variation:

print SharpCap.Cameras
import time
print time
SharpCap.SelectedCamera = SharpCap.Cameras[0]# Find( lambda x:x.DeviceName == "Test Camera 2 (High Speed)") # edit to grab from your camera
SharpCap.SelectedCamera.Controls.Gain.Value = 383 # Set the gain to 383

SharpCap.SelectedCamera.LiveView = False
Path = "c:\Documents\white1\exposure" # bob edit this to match your computer folder structure
type = ".png"

exposure_array = [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1, 1.1] #1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0] # edit to select exposure

for exposure in exposure_array : # Loop through all exposures
SharpCap.SelectedCamera.Controls.Exposure.Value = exposure # Set the camera exposure
time.sleep(2)
SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + "_1" + type)
SharpCap.SelectedCamera.LiveView = False
SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + "_2" + type) # snap a photo, save in directory and name according to exposure
SharpCap.SelectedCamera.LiveView = False

Maybe you can spot something that's not right...

1 2087.969
2 1790.186
3 2641.266
4 2635.939
5 3180.194
6 3180.395
7 3451.338
8 3789.216
9 4332.402
10 4340.584
11 4901.928
12 4596.418
13 5461.355
14 5168.053
15 6037.273
16 5729.396
17 6609.584
18 6611.796
19 7172.947
20 7222.074
21 6908.654
22 7193.349
Exposure_Stack_Profile_BETA3.3_1_and_2.JPG
Exposure_Stack_Profile_BETA3.3_1_and_2.JPG (44.56 KiB) Viewed 461 times
And here's the _2 data only... This shows that NOW we're getting pretty close to what we want except for the last exposure!

1 1842.546
2 2755.644
3 3359.167
4 4034.839
5 4649.516
6 4966.654
7 5602.896
8 6229.98
9 7177.699
10 7851.816
11 7818.876
Exposure_Stack_Profile_BETA3.3_only_2.JPG
Exposure_Stack_Profile_BETA3.3_only_2.JPG (37.23 KiB) Viewed 461 times
Is there any better test of sequential exposure time than the one we're doing?

Thanks,

Bob

User avatar
admin
Site Admin
Posts: 5150
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: How To Add Time Delay Between Array of Sequential Exporsures

Post by admin » Fri Sep 18, 2020 6:29 pm

Hi Bob,

it didn't jump out at me until now, but you are using exposures from 0.1 ms through to 1.1 ms – those are very short and at those levels I wouldn't at all be surprised if the internal way that the camera times its frames may not be 100% accurate. What happens if you try running exposures more in the range 100 ms to 1 second? Any jitter in the frame timings will be much less significant…

Cheers, Robin

skithebird
Posts: 12
Joined: Tue Sep 15, 2020 7:39 pm

Re: How To Add Time Delay Between Array of Sequential Exporsures

Post by skithebird » Fri Sep 18, 2020 6:45 pm

Hi Robin,

I agree, if we were calling such short exposure times. We're not, or at least trying not to call 0.1 ms. Our understanding has been that the software and camera respond to seconds as the default. When we write:

exposure_array = [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1, 1.1]

we are expecting 0.1 to mean 0.1 seconds, or 100 ms, etc.

Perhaps we are misunderstanding? I watch the SharpCap windows during the run. I thought it was behaving correctly. The last call of 1.1 sure seems to be a 1.1 second exposure.

How should we write the exposure times to be certain that we are getting 100 ms, 200 ms, 300 ms and so on?

Oh, and we've tried adding a 2 second pause in between each exposure using "time.sleep(2)". We wrote this assuming that 2 in this usage would represent 2 seconds and not 2 ms.

Please set us straight!!

Thanks,

Bob

User avatar
admin
Site Admin
Posts: 5150
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: How To Add Time Delay Between Array of Sequential Exporsures

Post by admin » Fri Sep 18, 2020 7:09 pm

Hi,

Ah okay, it's going to depend on your camera type - I tested with an Altair camera where the raw exposure values are measured in ms. Setting the value of an exposure control is not a portable way to set the exposure between cameras as they can implement different units. The way to be sure that you are getting the results that you want is to do

Code: Select all

SharpCap.SelectedCamera.Controls.Exposure.ExposureMs = exposure*1000 # Set the camera exposure
The delay that you are adding is going to be 2s - that's a standard python sleep call.

Here's an improved script that will dump the data out to the python console during the run, rather than you having to analyze the files after :

Code: Select all

print SharpCap.Cameras
import time
print time
SharpCap.SelectedCamera = SharpCap.Cameras[0]# Find( lambda x:x.DeviceName == "AA533CPROTEC") # edit to grab from your camera
SharpCap.SelectedCamera.Controls.Gain.Value = 383 # Set the gain to 383

SharpCap.SelectedCamera.LiveView = False

def handler(sender,args):
	print args.Frame.Info.ExposureMs, ", ", args.Frame.GetStats().Item1

Path = "f:\\" # bob edit this to match your computer folder structure
type = ".png"

exposure_array = [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1, 1.1] #1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0] # edit to select exposure

SharpCap.SelectedCamera.FrameCaptured += handler;

for exposure in exposure_array : # Loop through all exposures
	SharpCap.SelectedCamera.Controls.Exposure.ExposureMs = exposure*100 # Set the camera exposure
	time.sleep(2)
	SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + "_1" + type)
	SharpCap.SelectedCamera.LiveView = False
	SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + "_2" + type) # snap a photo, save in directory and name according to exposure
	SharpCap.SelectedCamera.LiveView = False

SharpCap.SelectedCamera.FrameCaptured -= handler;
Here's the data I got from running this on an Altair camera (I used exposures from 10ms to 110ms as that suited my light levels).

Code: Select all

10.0 ,  10.8053140994
10.0 ,  10.8089704514
20.0 ,  19.2348054613
20.0 ,  19.2247328780
30.0 ,  27.6371348919
30.0 ,  27.6449005480
40.0 ,  36.0889131233
40.0 ,  36.0898603061
50.0 ,  44.6675340668
50.0 ,  44.5726884013
60.0 ,  53.1776614660
60.0 ,  53.0441633966
70.0 ,  61.6515521326
70.0 ,  61.4760941759
80.0 ,  70.0680293849
80.0 ,  69.9347069703
90.0 ,  78.5395546465
90.0 ,  78.4244343202
100.0 ,  87.0433651265
100.0 ,  86.9147393450
110.0 ,  95.5312570323
110.0 ,  95.4078589541
Here's the plot of that
Capture.PNG
Capture.PNG (9.97 KiB) Viewed 443 times
I guess I should mention the importance of constant light levels and making sure that nothing in the field of view of the camera is giving a saturation brightness in any of the measurements.

Robin

skithebird
Posts: 12
Joined: Tue Sep 15, 2020 7:39 pm

Re: How To Add Time Delay Between Array of Sequential Exporsures

Post by skithebird » Fri Sep 18, 2020 9:26 pm

Thanks Robin! Now were getting close. My exposure level is now just as linear as yours with rising exposure time!! :shock:

The only "issue" is that the code written to print exposure time versus level is showing "None, number" in console.

List[Camera]([<SharpCap.Models.Camera object at 0x000000000000006D [ZWO ASI Cameras::ZWO ASI178MC]>, <SharpCap.Models.Camera object at 0x000000000000006E [DirectShow Cameras::Logitech HD Pro Webcam C920]>, <SharpCap.Models.Camera object at 0x000000000000006F [DirectShow Cameras::Integrated Camera]>, <SharpCap.Models.Camera object at 0x0000000000000070 [DirectShow Cameras::Logi Capture]>, <SharpCap.Models.Camera object at 0x0000000000000071 [ASCOM Cameras::Camera V2 simulator]>, <SharpCap.Models.Camera object at 0x0000000000000072 [Virtual Cameras::Folder Monitor Camera]>, <SharpCap.Models.Camera object at 0x0000000000000073 [Test Cameras::Test Camera 1 (Deep Sky)]>, <SharpCap.Models.Camera object at 0x0000000000000074 [Test Cameras::Test Camera 2 (High Speed)]>])
<module 'time' (built-in)>
None , 2167.60649987
None , 2167.60649987
None , 2167.60649987
None , 2166.32035661
None , 2166.32035661
None , 2166.32035661
None , 2732.93776896
None , 2732.93776896
None , 2732.93776896
None , 2732.07085184
None , 2732.07085184
None , 2732.07085184
None , 3306.98841637
None , 3306.98841637
None , 3306.98841637
None , 3307.36333681
None , 3307.36333681
None , 3307.36333681
None , 3884.56267385
None , 3884.56267385
None , 3884.56267385
None , 3883.01906652
None , 3883.01906652
None , 3883.01906652
None , 4454.64769637
None , 4454.64769637
None , 4454.64769637
None , 4461.28288024
None , 4461.28288024
None , 4461.28288024
None , 5046.77749041
None , 5046.77749041
None , 5046.77749041
None , 5038.62574821
None , 5038.62574821
None , 5038.62574821
None , 5626.08416411
None , 5626.08416411
None , 5626.08416411
None , 5630.69613157
None , 5630.69613157
None , 5630.69613157
None , 6222.77756663
None , 6222.77756663
None , 6222.77756663
None , 6222.11412585
None , 6222.11412585
None , 6222.11412585
None , 6813.87421073
None , 6813.87421073
None , 6813.87421073
None , 6810.44772692
None , 6810.44772692
None , 6810.44772692
None , 7417.41389479
None , 7417.41389479
None , 7417.41389479
None , 7413.96929463
None , 7413.96929463
None , 7413.96929463
None , 7107.75065326
None , 7107.75065326
None , 7107.75065326
None , 7440.93491859
None , 7440.93491859
None , 7440.93491859
>>>

Something must be amiss in my script copy, but I can't see it. Here's my script. Do you see why I'm not getting the exposure values printed as well?

print SharpCap.Cameras
import time
print time

SharpCap.SelectedCamera.LiveView = False

def handler(sender,args) :
print args.Frame.Info.ExposureMs, ", ", args.Frame.GetStats().Item1


Path = "c:\Documents\white1\exposure" # bob edit this to match your computer folder structure
type = ".png"

exposure_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] #1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0] # edit to select exposure

SharpCap.SelectedCamera.FrameCaptured += handler;

for exposure in exposure_array : # Loop through all exposures
SharpCap.SelectedCamera.Controls.Exposure.ExposureMs = exposure*100 # Set the camera exposure
time.sleep(2)
SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + "_1" + type)
SharpCap.SelectedCamera.LiveView = False
SharpCap.SelectedCamera.CaptureSingleFrameTo(Path + str(exposure) + "_2" + type) # snap a photo, save in directory and name according to exposure
SharpCap.SelectedCamera.LiveView = False

SharpCap.SelectedCamera.FrameCaptured -= handler;

Here are my two plotted results. The only snafu is in the 11th exposure of each set.
Exposure_Sequence_1.JPG
Exposure_Sequence_1.JPG (42.88 KiB) Viewed 437 times
Exposure_Sequence_2.JPG
Exposure_Sequence_2.JPG (42.25 KiB) Viewed 437 times

User avatar
admin
Site Admin
Posts: 5150
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: How To Add Time Delay Between Array of Sequential Exporsures

Post by admin » Sat Sep 19, 2020 2:41 pm

Hi,

it's more likely to be a glitch in the ZWO code in SharpCap – possibly not putting a value into that exposure field on each frame. I will investigate.

Cheers, Robin

Post Reply