An outdoor camera that must run all day faces a scene whose brightness changes by more than one fixed exposure can hold. The installation that prompted this guide — a Linux PC and a camera exposing nothing but gain and one exposure control — needed that parameter moved automatically from dawn to dusk to keep object detection working. The fix was to close the loop in software: measure image brightness every frame, correct the exposure on a schedule, and prove the result with tests. This guide gives the loop, the numbers and the code.
The decision, up front
Own the loop; do not leave brightness to the camera's firmware. Disable every auto mode, measure the green-channel mean of each frame, and correct the exposure parameter with a PID step every eighth grab — 400 ms when a frame takes 50 ms. The whole procedure:
- Turn auto exposure, auto gain and auto white balance off; park gain at its lowest step.
- Read the green mean and the clipped-pixel fraction from every frame.
- Target a green mean of 100 on the 8-bit scale — 0.39 of full scale.
- Guard highlights: above 1 % of pixels clipped, cut exposure 20 % at once.
- Correct every eighth frame with the velocity-form PID below, each write clamped to the range your own day test produced.
- Tune from one step test: Ki = 0.5 / g and Kp = 0.5 × Ki, where g is your scene's grey levels per exposure count. Kd stays 0.
Why this shape: daylight moves on second and minute scales, a written exposure reaches the image one to two frames later, and the histogram is the expensive per-frame work. A 400 ms correction interval runs far ahead of the sky, waits out the actuation lag, and spends one eighth the CPU of per-frame control — the exact schedule that settled the original problem. A cloud takes tens of seconds to cross a view; at one correction per 400 ms the loop gets dozens of shots at it.
What you need
Three things: frames you can read into memory, an exposure control you can write while grabbing, and somewhere to run a histogram. Any machine-vision camera with a software interface qualifies — for example the LS-CMR-0166, a 12 MP USB3 area scan camera with a rolling-shutter sensor:
- Sensor format
- 1/1.7"
- Interface
- USB3
- Resolution
- 12 MP
- Shutter
- Rolling
If you would rather not keep a PC at the camera, run the loop on the device itself — the LS-SMC-0017, an 8.8 MP global-shutter smart camera on an X86 platform with a C-mount, runs the same Python beside the inspection logic:
- Interface
- USB 2.0
- Resolution
- 6-10 MP
- Sensor format
- 1"
- Shutter
- Global
The case that prompted this guide sat at the sparse end: under V4L2 on Linux, the camera exposed exactly two controls, gain and an exposure control named abs_exposure. That is enough. Query the control's unit, minimum, maximum and step before writing numbers — auto exposure limit units in microseconds: query before you set shows how easily the unit is misread. Writing parameters while the grab loop runs is its own pattern; runtime camera parameter control: exposure and white balance live covers it. Starting from scratch instead? The camera finder filters by interface and mount, and the configurator's camera step sequences a whole new station.
Step 1 — Turn every auto mode off
Every automatic mode is a second controller fighting yours: auto exposure undoes your writes, auto gain moves the mean without telling you, and auto white balance reshuffles the very channel you measure. Turn them all off — camera image brightness drifts over time: turn every auto mode off lists the modes that hide in firmware. Set white balance once with a one-shot call rather than per frame; auto white balance drift between frames: use one-shot instead shows the artifact you avoid. Park gain at its lowest step — it is your reserve actuator, not your primary one.
With the modes off, write one exposure value by hand and watch the mean move. Note both numbers: that reading is your plant gain for Step 4.
Step 2 — Measure the green histogram
Two numbers per frame: the mean of the green channel, and the fraction of green pixels above 94 % of full scale (240 at 8-bit). Green earns the job on physics: half the photosites in a Bayer colour filter array are green, the green band carries about 59 % of the luminance signal, and averaging it gives the quietest brightness estimate. On a mono camera use the whole-frame mean; on a demosaiced RGB frame take channel index 1.
The mean is the right loop input because it is proportional to exposure below clipping — the tuning in Step 4 assumes that linearity. Percentiles and full histograms answer other questions; histogram exposure metrics for auto exposure and contrast control compares the statistics for scenes with a bright sky band.
The numbers scale with the pixel format: target 400 at 10-bit, 1600 at 12-bit, and when a 12-bit payload rides in a 16-bit container, 25,600 — 12-bit data in a 16-bit V4L2 frame: why values top out near 65520 explains that container.
Step 3 — Set target, guard and clamp
Target 0.39 of full scale. At 8-bit that is 100, which leaves a highlight 2.4x brighter than the average scene still below the 240 clip line — the margin that saves chrome edges and sun glints. Detection tasks have their own best target: barcodes and backlit silhouettes read better darker, textured surfaces lighter, and set an auto exposure brightness target for inspection works those cases through.
The mean cannot see a bright patch covering a fraction of a percent of pixels, so the guard does: above 1 % clipped, multiply exposure by 0.8 immediately and clear the error history. Saturation feedback pays for its two lines of code — a 2022 mobile-robotics auto-exposure study measured roughly 15 frames to converge with it, against up to 100 frames without.
Clamp every write to the range a real site test produced. In the original installation, 160 to 600 counts held dawn-to-dusk detection — a 3.75:1 swing, since 600 / 160 = 3.75.
Step 4 — Correct every eighth frame
An exposure write does not reach the image instantly: the new value takes effect on the next exposure, so your measurement lags each write by one to two frames. Correct every frame and the loop reacts to its own stale feedback and rings; correct every eighth frame and the image has long settled. The arithmetic falls out of the frame time: 8 × 50 ms = 400 ms per correction, 2.5 corrections per second, 150 per minute.
The correction is the velocity form of a PID — it acts on change, so there is no accumulated sum to reset when the guard fires:
Tuning is one step test: with the loop open, raise exposure by 100 counts and read the mean's change; that ratio is g, in grey levels per count. Set Ki = 0.5 / g — each correction then closes about half the error — and Kp = 0.5 × Ki to damp it, Kd = 0. For a scene reading 18 grey levels per 100 counts, that gives Ki = 0.5 / 0.18 = 2.8, rounded to 3, and Kp = 1.5. The control theory behind the law is in build a software auto exposure algorithm: the control law, and the safety-bounded variant in software auto exposure for a raw camera: the bounded loop. Plant-floor PID loops get tuned with dedicated tools — the same gain discipline compared in this industrial PID tuning software comparison — but a camera loop settles with the step test alone.
Worked example: a cloud crosses the scene
Step test at mid-morning: +100 counts moved the green mean by +20 grey levels, so g = 20 / 100 = 0.2 grey levels per count. The law gives Ki = 0.5 / 0.2 = 2.5 and Kp = 0.5 × 2.5 = 1.25. The clamp is 160 to 600 counts, target 100, and the scene sits on target at E = 500 counts.
A cloud cuts the light by 9 %: the mean falls to 500 / 5.5 = 90.9 — under the cloud each count returns 1 / 5.5 = 0.18 grey levels — and the error opens to 9.1. The loop's next five corrections, one per 400 ms:
| Correction | E (counts) | Green mean | Error | dE |
|---|---|---|---|---|
| cloud arrives | 500.0 | 90.9 | 9.1 | — |
| +0.4 s | 534.1 | 97.1 | 2.9 | +34.1 |
| +0.8 s | 533.6 | 97.0 | 3.0 | -0.5 |
| +1.2 s | 541.2 | 98.4 | 1.6 | +7.6 |
| +1.6 s | 543.5 | 98.8 | 1.2 | +2.3 |
| +2.0 s | 546.0 | 99.3 | 0.7 | +2.5 |
First correction in full: dE = (Kp + Ki) × e = 3.75 × 9.1 = 34.1 counts, so E = 500 + 34.1 = 534.1 and the mean returns 534.1 / 5.5 = 97.1. After five corrections — 2.0 s — the error is under one grey level and the clamp never engaged. Exposure had to rise 10 % to buy back a 9 % light loss, which is the loop doing its job: the setting ends near 550 counts, exactly 100 × 5.5.
The loop in code, and its test
target, e_min, e_max = 100.0, 160.0, 600.0 # 8-bit grey, exposure counts
kp, ki, kd = 1.25, 2.5, 0.0 # from the step test
stride = 8 # correct every 8th frame
def step(E, mean, sat, e_prev, e_prev2):
# one correction; sat = fraction of green pixels over 240
if sat > 0.01: # highlight guard
return 0.8 * E, 0.0, 0.0
e = target - mean
d = kp * (e - e_prev) + ki * e + kd * (e - 2 * e_prev + e_prev2)
E = min(max(E + d, e_min), e_max)
return E, e, e_prev
# grab() and write_exposure() are ours; your SDK supplies them, and in
# the V4L2 case above the write sets abs_exposure.
E, e_prev, e_prev2 = 500.0, 0.0, 0.0
for i, frame in enumerate(grab()):
green = frame[:, :, 1] # demosaiced RGB
mean = float(green.mean())
sat = float((green > 240).mean())
if i % stride == 0:
E, e_prev, e_prev2 = step(E, mean, sat, e_prev, e_prev2)
write_exposure(int(round(E)))
The test replaces the camera with the cloud model mean = E / 5.5 and proves the convergence claim:
E, e_prev, e_prev2 = 500.0, 0.0, 0.0
for _ in range(5):
E, e_prev, e_prev2 = step(E, E / 5.5, 0.0, e_prev, e_prev2)
assert abs(E / 5.5 - 100.0) < 1.0 # within one grey level
Five simulated corrections leave the mean at 99.3, so the assert holds with margin. Point the same function at recorded frames from a real day and it becomes your regression test.
If the camera has no exposure control, move the light
Board-level cameras and vision sensors sometimes expose no exposure parameter at all, only an image stream. The loop does not change — the actuator does: write the correction to a light controller channel as an intensity level, and the plant gain g becomes grey levels per intensity step. Camera has no software exposure control: what to do instead walks that wiring, and the light controller finder lists channels and output types. Keep the drive inside the light's rated current while you chase a dark scene; an overdriven LED dies early.
The LS-CTL-0322 is one such actuator: a 4-channel digital controller for 16-24 V lights with 8 A of total output.
- Output
- Constant voltage 24 V
- Channels
- 4 channels
- Control
- Digital
In a strobed cell, exposure is the wrong knob entirely — it must stay locked to the strobe pulse width. Give the PID the light channel and fix the camera side; camera strobe output timing: exposure pulse and image timestamp aligns the pair, and strobe light on but camera image is black: fix the timing is the failure a mismatch produces.
Which actuator to move
| Actuator | Where it lives | Reaches the image | Cost to image quality | Move it when |
|---|---|---|---|---|
| Exposure time | camera control | after 1-2 frames | none — more photons, same noise floor | by default, daylight and static scenes |
| Analog gain | camera control | immediately | amplifies read noise with the signal | exposure sits at its ceiling and the mean is still low |
| Light output | controller channel | within one strobe cycle | none — real photons added | no writable exposure, or a strobe locks exposure |
| Aperture | lens ring | after a manual change | shifts depth of field | once, at commissioning — never in the loop |
Gain is a reserve actuator, never a parallel one; the cascade rule that hands control from exposure to gain sits in the FAQ below.
Checks before you ship it
- Step test at commissioning: lamp on, lamp off — the mean returns to 100 within 2 s each time, matching the worked example.
- One full day of logs: the guard fires only in direct glint. If it fires all morning, lower the target to 90.
- CPU share of the grab loop before and after the stride: the histogram cost drops to about one eighth.
- Re-read the exposure control's unit and limits after any power cycle, before the first write.
- Log E and mean with every image — chunk data on camera frames: carry timestamp and exposure per image keeps the pair attached at the source.
- One loop per camera in a multi-head cell; independent exposure per camera: control each head separately covers the state you must keep apart.
When the usual answer is wrong
Rolling-shutter cameras under mains-powered lighting band once exposure gets short: snap every write to a whole multiple of the light's ripple period — 10 ms on a 50 Hz supply, 8.33 ms on 60 Hz. Black bars in some camera images: fix lighting flicker vs exposure tells those stripes apart from other causes.
The decision by case: a PC-hosted or X86 smart camera with a writable exposure runs the loop above on exposure, gain parked at minimum. A camera whose exposure has hit its ceiling hands the actuator to the slower gain cascade. A camera with no writable exposure writes the light controller channel instead. A strobed cell fixes exposure to the pulse width and loops the light. More software-side vision patterns are collected on the systems topic page.
Questions
Why correct every eighth frame instead of every frame?
Two reasons: cost and lag. The histogram is the expensive per-frame work, and the stride spends one eighth of it. More to the point, a written exposure reaches the image one or two frames later, so per-frame corrections react to stale feedback and ring. Hold the correction interval between 200 and 500 ms: at a 10 ms frame time that is every 40th frame, at 100 ms every 3rd. Sky and clouds move on second scales, so nothing is lost.
How fast does the loop react when the light steps suddenly?
Detection costs one stride — 400 ms at 50 ms frames — plus one or two frames for the write to reach the image, so the first correction lands within about half a second. Full-range travel is slower: 600 - 160 = 440 counts at roughly 34 counts per correction takes about 13 corrections, or 5 s, when the loop starts at the floor. Small steps like the 9 % cloud in the worked example converge inside 2 s.
The mean sits on target but small highlights still clip — what gives?
A mean cannot see an area covering a fraction of a percent of the frame; a chrome edge barely moves it. Lower the target: dropping from 100 to 90 lifts highlight headroom from 2.4x to 2.7x (240 / 90). Tighten the guard to 0.2 % so it trims harder. The best fix is computing the mean inside the detection region only, so the target describes the pixels your algorithm actually reads.
Should gain live inside the same PID?
No — cascade it. Hold gain at its lowest step until exposure has sat at its ceiling for three consecutive corrections, then hand the actuator to a second, slower loop running a third of the exposure loop's Ki. When light returns, drop gain back to minimum first and let exposure take over again. One PID writing both at once gives you two controllers fighting over one mean.
How do I test a full day of behaviour without waiting for the weather?
Record one real day of frames, then replay them through the loop with the recording standing in for the camera — the code test above is the pattern. Assert two things: the green mean stayed within 5 grey levels of 100 outside direct glint, and the guard fired only when something bright genuinely entered the scene. Replay at 20x speed and a nine-hour day takes about half an hour to check.


