Black Calibration
[WIP]
1 What does this mean?
Finding the sensor output value, per pixel, in the absence of any illumination.
It covers: - dark frame subtraction - dark current compensation - using black reference columns (called "optical black" by other manufacturers) to find the black level and to correct some of the row noise
Note: black reference columns can be used to reduce row noise, that's why we include it here. That means, we will correct some (but not all) of the row noise as part of black calibration process.
2 Calibration methods
2.1 Dark frame subtraction
This is a basic technique: take a picture with the lens cap on, and subtract it from your image. To make really sure no light is reaching the sensor, also cover the entire camera with something.
2.1.1 How many dark frames?
Problem: if you take only one dark frame, it will also contain read noise (assummed to be Gaussian and uncorrelated with the read noise from other frames). Therefore, subtracting only one image will actually increase the noise in the final output, by sqrt(2).
Solution: use a master dark frame, averaged from many images.
How many?
If you take N images, the signal will be multiplied by N, and the noise will be multiplied by sqrt(N). Therefore, the SNR will increase by log2(sqrt(N)) stops.
So, 16 dark frames will reduce the read noise in the dark frame by 2 stops, 64 frames by 3 stops, and 256 frames by 4 stops.
Okay, but how much noise will be added in the output image?
Let's say the read noise stdev in one image is r, so a dark frame averaged from N frames will have noise stdev = r/sqrt(N). Therefore, the noise in the output image will be: sqrt(r^2 + r^2/N) = r * sqrt(1 + 1/N).
Check it in octave:
octave:1> N = 4; octave:2> a = randn(1,1000000); # one noise sample octave:3> b = randn(N,1000000); # N noise samples octave:4> std(a + mean(b)) # add one noise sample to N averaged noise samples ans = 1.1180
So, it seems that averaging a small of dark frames will not introduce significant noise in your images (4 should be enough if you are in a hurry, and 16 should give a very good result).
2.1.2 What about camera settings?
Unfortunately, dark frames depend on most of the camera settings: gain (ISO), exposure, and other sensor settings like offset, black shading protection, PLR configuration and so on. Temperature is a variable as well.
Luckily, the dependence on exposure appears to be linear, so we can take calibration frames at various exposures, combine them into a single dark frame, adjust it for the dark current and use it for the entire range of exposure settings (hopefully). We'll discuss that in the next section.
2.2 Dark current
The overall brightness in the dark frame changes with exposure in a linear fashion. We'll try to account for this in two ways: with a simple scalar value, and with a per-pixel correction.
The values in the black reference columns do not appear to compensate for the dark current, so we'll need to do it ourselves.
Currently, calibration frames are identified by gain, so we compute a set of calibration frames at gain x1, another at gain x2, and so on.
2.2.1 Simple correction
Experimentally, we have found the dark frame changes with exposure at roughly 0.055 digital units for each ms (without looking at black reference columns), or 0.06 dn/ms after subtracting the black reference columns. This value is multiplied by gain. To find this value, take the dark frames at different exposure times (say 1...50 ms), then do a linear fit for the frame average (or median).
Command-line:
raw2dng *x1*.raw12 --calc-darkframe
2.2.2 Dark current nonuniformity correction
"One common use of bias frames is for scaling dark frames. By subtracting a bias frame from a dark frame, you end up with a “thermal frame.” A thermal frame contains pixel values showing just the effect of dark current. Because dark current in any given pixel accumulates at a constant rate, a thermal frame allows you to predict with reasonable accuracy how much dark current there would be for different length exposures. However, given the opportunity, you’re always better off taking dark frames that match the exposure times of your light frames." [1]
"Dark current non-uniformity is a noise that results from the fact that each pixel generates a slightly different amount of dark current. This noise can be eliminated by subtracting a dark reference frame from each image. The dark reference frame should be taken at the same temperature and with the same integration time as the image." [2]
Rather than using a single scalar value (0.06 dn/ms/gain) for all pixels, we can try finding the individual dark current for each pixel. So, rather than doing a linear fit on the overall dark frame brightness (vs exposure), we will do the linear fit per pixel. We'll have to acquire a lot more dark frames to compute a good result, but it might be worth the trouble.
Command-line:
raw2dng *x1*.raw12 --calc-dcnuframe
2.3 Black reference columns
This sensor has 8+8 columns that can be used for calibrating the black levels; they are also useful for reducing the dynamic row noise.
Experimentally, we have noticed that odd rows have different statistics, compared to even rows. This happens in both the black columns and the active area, and it may indicate two parallel circuits used for readout, each having slightly different electrical response (noise levels, gains, offsets and so on).
Therefore, it may be wise to process the black columns for odd and even rows separately, which should already fix some issues like static row noise, or the need for green equilibration.
In raw2dng, this correction is enabled by default, as long as you use a dark frame. You can turn it off with --no-blackcol, if you want.
2.3.1 Black level
The sensor has two registers that can be used to adjust the black level: one for odd rows, another for even rows. This confirms our finding about two parallel readout circuits.
You might be tempted to adjust the black level to 0 (like Nikon does). Please don't. Here's why:
- If you adjust the offset until the black level becomes roughly 0, you will clip all the data below this level. Good luck subtracting a dark frame after that.
- Even if you change the level after doing all the black corrections, you may still have useful data below zero. You will need it when stacking multiple frames, or when doing noise reduction.
I recommend setting the offset so that only a few isolated pixels (if any) reach the value of 0. A black offset of 2047 (registers 87/88) is a good choice. You won't lose any dynamic range by doing that.
On most recent Canon DSLRs, black level is 2048. That's a little on the large side, but it's a good thing. Some may argue that you may lose 2 stops or more of dynamic range by doing that [3], but this is wrong. On Canons, the raw output is 14-bit, so by setting the offset to 2048 instead of 0, the useful range will be "just" log2(16384-2048) = 13.8 stops, instead of 14. So, yeah, you lose 0.2 stops from the ADC range.
With the black offset of 2047 on the CMV12K (a 12-bit sensor), the black level ends up at around 150, so you lose a whooping 0.05 stops.
Note: for easier processing, raw2dng shifts the raw data in order to fix the black level at 128.
2.3.2 Row noise correction from black columns
3 Proposed calibration pipeline
- use black reference columns to find the black levels for odd and even rows
- subtract dark offset and dark current
- use variations in black reference columns to reduce row noise
These operations should be simple enough to be implemented in FPGA, as real-time corrections.
Details on Raw_preprocessing.
4 Calibration procedure
4.1 Performing the calibration
4.1.1 Quick calibration
Acquire 16 dark frames, gain x1, exposures between 1 and 50 ms, save them under the 'darkframes' subdirectory, then run:
raw2dng darkframes/*x1*.raw12 --calc-darkframe --swap-lines
Result: darkframe-x1.pgm
Repeat for other gains if needed.
4.1.2 Accurate calibration, with dark current nonuniformity
Acquire 256 dark frames, gain x1, exposures between 1 and 64 ms in linear increments (4 images at each setting), save them under the 'darkframes' subdirectory, then run:
raw2dng darkframes/*x1*.raw12 --calc-dcnuframe --swap-lines
Result: darkframe-x1.pgm and dcnuframe-x1.pgm.
Repeat for other gains if needed.
4.2 Using the reference frames to correct raw12 files
- place the calibration files in the working directory
- make sure your .raw12 images contain a metadata block
- raw2dng will recognize the dark frames and use them for correcting your image
cp /path/to/darkframes/*-x[1-4].pgm . raw2dng *.raw12 --swap-lines
(note: you need --swap-lines as an workaround for an old bug introduced in the Beta, but wasn't fixed yet in the FPGA, hint, hint)
This is not exactly useful when dealing with multiple folders, so until we'll have a better way to organize the calibration frames, you may try an alternative workflow:
- place the calibration files in some directory (let's call it "calibration directory")
- make sure your .raw12 images contain a metadata block
- use paths when passing input files raw2dng (the output files will be saved in the same directory as the input file)
cd /path/to/darkframes ls *.pgm darkframe-x1.pgm dcnuframe-x1.pgm ... raw2dng /path/to/images/*.raw12 --swap-lines