Auto Exposure > 5 sec?

Somewhere to ask questions about the best way to use SharpCap
Forum rules
‍

If you have a problem or question, please check the FAQ to see if it already has an answer : https://www.sharpcap.co.uk/sharpcap-faqs
Post Reply
StarFlea
Posts: 169
Joined: Wed Dec 21, 2022 12:22 pm

Auto Exposure > 5 sec?

#1

Post by StarFlea »

Hello!

I’ve tried a few times now to capture an all-night time-lapse using my fisheye lens (via the sequencer), taking a shot every 15 seconds. I’m using auto-exposure for this.
Generally, it works quite well; however, I’ve noticed that the exposure time never seems to exceed 5 seconds, even though the profile file specifies 15 seconds.

My goal is to capture the Milky Way, but that’s likely to be difficult with exposure times of only 5 seconds. It would also be ideal if the exposure time could be reduced—for instance, when the moon rises.

Regards,
StarFlea
Attachments
FishEye Video 15s 500 (veLOX_C) (G3M178C).ini
(2.06 KiB) Downloaded 3 times
User avatar
admin
Site Admin
Posts: 17496
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Auto Exposure > 5 sec?

#2

Post by admin »

Hi,

that's a ToupTek camera you're using there right? On that make of cameras (including all the derived brands with the exception of Altair) the autoexposure only runs up to 5s - it's a limitation of the camera SDK. The API to set the maximum autoexposure value will fail on those cameras if I try to use a value longer than 5s. Altair being the exception has a maximum of 60s - this seems to be for historical reasons.

Unfortunately there isn't really much I can do about this - the SDK imposes a hard limit and SharpCap doesn't do its own autoexposure except in limited circumstances (and what it does isn't suitable for this sort of use case) :(

sorry about that,

Robin
StarFlea
Posts: 169
Joined: Wed Dec 21, 2022 12:22 pm

Re: Auto Exposure > 5 sec?

#3

Post by StarFlea »

Hi Robin!

Thanks for shedding some light on this! That explains a lot, though it is naturally a bit of a letdown.

Is an automatic gain adjustment possible, perhaps?

Otherwise, I guess I'll just have to live with this flaw...
In that case, I'll try working with a fixed exposure...

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

Re: Auto Exposure > 5 sec?

#4

Post by admin »

Hi,

if I remember rightly, there is no 'auto gain' on its own, but you can enable auto gain with auto exposure. You'll find that down a few controls from the exposure and gain controls, where you can set 'auto gain with auto exposure' to either off or 'up to gain XXXX'. So for example if set to 'up to 1600', the automatic adjustment of the gain can be anywhere in the range 100 to 1600 while in auto mode. I'm not sure if that will really be sufficient for your situation though :(

cheers,

Robin
StarFlea
Posts: 169
Joined: Wed Dec 21, 2022 12:22 pm

Re: Auto Exposure > 5 sec?

#5

Post by StarFlea »

Hello,

I’ve been experimenting with Java in this context and testing various things. It really seems that the camera is locked to a maximum automatic exposure time of 5 minutes—likely due to either the ToupTek driver or the camera’s own firmware...

However, I’m wondering if it might be possible to use a Python script to control the exposure during capture according to a predefined ramp—something like this:

Code: Select all

import time

exposures_ms = [10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 30000]

for exposure in exposures_ms:
    SharpCap.SelectedCamera.Controls.Exposure.ExposureMs = exposure
    time.sleep(600)
The question is, how can I implement this in parallel with the actual recording?
My idea is, for instance, to start with an exposure of 0.01 seconds at sunset (or even earlier) and increase it to 30 seconds by the time nautical dusk begins.
And in the morning, the process would work in reverse...

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

Re: Auto Exposure > 5 sec?

#6

Post by admin »

Hi,

the trick here would be to hook the FrameCaptured event on the camera, which would then get your code run every time a new frame arrives. Define your frame captured handler a bit like this

Code: Select all

def frameCaptured(sender, args)
	# probably only bother to process once per second if frames are faster than that?
	frame = args.Frame 
	histogram = frame.CalculateHistogram(None) # None is not supplying an optional rectangle to evaluate the histogram over - will use whole frame
	stats = histogram.GetStats() # stats has mean and sigma
	# or
	centiles = histogram.GetCentilePoints(10) # get the histogram levels for the 10th centile in each channel in order L, R, G, B
	# make decision on exposure
	SharpCap.SelectedCamera.Controls.ExposureMS = value
	
# hook event
SharpCap.SelectedCamera.FrameCaptured += frameCaptured

# unhook event later
SharpCap.SelectedCamera.FrameCaptured -= frameCaptured

The syntax may not be 100% accurate, but the code above gives the gist of the solution.

cheers,

Robin
StarFlea
Posts: 169
Joined: Wed Dec 21, 2022 12:22 pm

Re: Auto Exposure > 5 sec?

#7

Post by StarFlea »

Thanks, Robin—that was exactly the inspiration I needed! :)

I’ve now put together two Python scripts based on this: one to start the automatic exposure and another to stop it.

Code: Select all

import time

if not globals().get("auto_exposure_running", False):

    UPDATE_INTERVAL = 5.0  # sec

    MIN_EXPOSURE = 0.244  # ms
    MAX_EXPOSURE = 30000.0  # ms

    TARGET_MEDIAN = 100
    CENTILE = 50

    last_update = 0.0

    auto_exposure_camera = SharpCap.SelectedCamera

    def calculate_exposure(centile, current_exposure):

        if centile <= 0:
            new_exposure = current_exposure * 2.0
        else:
            new_exposure = current_exposure * (TARGET_MEDIAN / centile)

        return max(MIN_EXPOSURE, min(MAX_EXPOSURE, new_exposure))


    def frameCaptured(sender, args):

        global last_update
        now = time.time()

        if now - last_update < UPDATE_INTERVAL:
            return

        last_update = now

        frame = args.Frame 
        histogram = frame.CalculateHistogram(None)
        centile = float(histogram.GetCentilePoints(CENTILE)[0])

        current_exposure = auto_exposure_camera.Controls.Exposure.ExposureMs

        auto_exposure_camera.Controls.Exposure.ExposureMs = calculate_exposure(centile, current_exposure)


    auto_exposure_camera.FrameCaptured += frameCaptured
    auto_exposure_running = True

Code: Select all

if globals().get("auto_exposure_running", False):

    auto_exposure_camera.FrameCaptured -= frameCaptured
    auto_exposure_running = False
The initial test was very promising! The capture runs from sunset to sunrise (sequencer-controlled), and the exposure adjusts dynamically throughout the process.

Now it might just need a bit of fine-tuning...
Do you have any other ideas for these scripts?

If you’re up for it, perhaps you could turn them into sequencer steps...

StartAutoExposure.py
(1.19 KiB) Downloaded 1 time
StopAutoExposure.py
(143 Bytes) Not downloaded yet

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

Re: Auto Exposure > 5 sec?

#8

Post by admin »

Hi Flo,

glad it worked - you can actually set this up in the sequencer yourself as there are steps to both run python code from file and also to run a single line of python.

Unfortunately I can't just take this algorithm and drop it into SharpCap for everyone because there are a lot of potential problems that can crop up across all the different brands of camera that SharpCap supports that might break something like this... Some cameras round the exposure to the nearest 0.1s or 0.2s when above 1s exposure, others will have a number of 'old' frames queued up so that you don't see the result of the exposure change straight away and still others may give a dark frame when changing exposure (a new frame starts, then the code says 'change exposure from 2s to 1.8s' and the camera stops the 2s exposure after 0.1s, returns a dark frame and starts a 1.8s exposure). I've bumped into all these things and more when doing the sensor analysis tool, so any SharpCap autoexposure would have to cope with them :(

cheers,

Robin
Post Reply