I decided to finish the work this Sunday to free my mind for my next job... so here it is:
I decided to created the algorithm considering the isos 100/800 combo (3 fstops difference). So it can also be 80/640, 160/1250 and so on... This is due to Magic Lantern advice says it is a safe maximum difference to avoid aliasing when merging the isos. Also I think it will be good idea to avoid noise increase so much, so 3 fstops difference does not increase the high iso so much.
It is possible to implement all isos difference in the same algorithm (100/200, 100/400, 100/800, 100/1600, 100/3200), see in the end of this topic how to do.
The isos will be called hi and lo (high and low). (this remembers me the A-HA band: "hunting high and low, ahhhh ahh ahhh...")
I did some tests with less and more light shooting the grey wall to perceive the wave amplitude better. In these tests I found when the highlight gets too high the results was better keeping the values a little below the maximum. So I found a safe maximum value result for the formula, and it is 245, just a little less than 255.
I foud it will be impossible to reconstruct the two lines structure. The reason is: the camera debayering process turns the two lines separation in a mixture of two or three lines resulting in the wave. After reconstructing the lines, the interpolation algorithm must do the job considering this pattern.
wavelength = 32 lines in 7 groups and 14 sub-groups.
each group contains the adjacent hi-lo lines
each sub-group contains hi or lo lines
the groups and sub-groups distribution inside the wave is:
hi-lo:
2-3
2-2
3-2
2-2
3-2
2-3
2-2
Numbers in the left are iso hi, numbers on the right are iso lo.
There are 3 lines together two times in the left and two times in the right.
The first two groups have same desing as the last two groups.
Three groups in the middle are in symmetric arrangement.
This design makes things calibrated.
So the algorithm is (see the attached original wave and reconstructed wave images below):
DeWaving Algorithm by Apefos
(for H264 dual iso with two lines per iso)
General Function: to reconstruct the lines in h264 video recorded in Canon DSLR using Magic Lantern Firmware using dual iso module with two lines per iso set to 3 fstops iso difference, removing the wave pattern generated by camera debayer algorithm and allowing extraction of two images with different isos.
Three suggestions to match the algorithm with the wave pattern in the image:
1 - User must reposition the video file height in timeline to put the first line of the first wave in the line 01 of the frame.
2 - Insert a wave 00 before the wave 01 in the algorithm and leave the image from the camera as is. The user types a number between 00 and 31 and the entire algorithm is moved down using the typed number as how many lines to move down the first line of wave 01. This way the wave 00 will fit the lines above.
3 - User types a number between 01 and 32 and this number will be the line where the first wave 01 starts. The wave 00 will fill the lines above/before the wave 01. (best option)
(After some try, the user will find the number for his camera).
(all video files from the same camera are expected to need the same amount of height reposition).
Main functions (rules all line functions):
main function A: decimal results below 0,5 = discard
main function B: decimal results equal or above 0,5 = +1
main function C: results above 245 = 245
main function D: (this must be decided by the programmer)
idea 1: line functions apply it's function separately to the R, G and B values of each pixel in the line.
idea 2: line functions apply it's function to the global luminance value of each pixel in the line, some kind of brightness adjust.
(luminance values are measured in RGB 8bit, from 0 to 255, for both ideas 1 and 2)
The Wave design: Line Isos and Line Functions:
wave 00 ( to process the lines before/above wave 01)
(repeat the lines from wave 01)
wave 01 (starts at line 0001) or (starts at line 0001 + number typed by user) or (starts at line typed by user)
wave line 01 = iso hi, line function: luminance x 1.132184
wave line 02 = iso hi, line function: luminance x 1.010356
wave line 03 = iso lo, line function: luminance x 0.523438
wave line 04 = iso lo, line function: luminance x 0.870130
wave line 05 = iso lo, line function: luminance x 0.592920
wave line 06 = iso hi, line function: luminance x 1.031414
wave line 07 = iso hi, line function: luminance x 1.082418
wave line 08 = iso lo, line function: luminance x 0.770115
wave line 09 = iso lo, line function: luminance x 0.957143
wave line 10 = iso hi, line function: luminance x 1.238994
wave line 11 = iso hi, line function: luminance x 1.020725
wave line 12 = iso hi, line function: luminance x 1.387324
wave line 13 = iso lo, line function: luminance x 0.837500
wave line 14 = iso lo, line function: luminance x 0.683673
wave line 15 = iso hi, line function: luminance x 1.036842
wave line 16 = iso hi, line function: luminance x 1.000000
wave line 17 = iso lo, line function: luminance x 0.663366
wave line 18 = iso lo, line function: luminance x 0.917808
wave line 19 = iso hi, line function: luminance x 1.368056
wave line 20 = iso hi, line function: luminance x 1.020725
wave line 21 = iso hi, line function: luminance x 1.270968
wave line 22 = iso lo, line function: luminance x 0.817073
wave line 23 = iso lo, line function: luminance x 0.807229
wave line 24 = iso hi, line function: luminance x 1.042328
wave line 25 = iso hi, line function: luminance x 1.010256
wave line 26 = iso lo, line function: luminance x 0.582609
wave line 27 = iso lo, line function: luminance x 0.893333
wave line 28 = iso lo, line function: luminance x 0.523438
wave line 29 = iso hi, line function: luminance x 1.026042
wave line 30 = iso hi, line function: luminance x 1.158824
wave line 31 = iso lo, line function: luminance x 0.788235
wave line 32 = iso lo, line function: luminance x 1.000000
wave 02 (starts at line 0033) or (starts at line 0033 + number typed by user) or (just after previous wave)
(repeat same lines from wave 01)
(and goes like this until reaches line 1080)
(the 1080 lines needs 33.75 waves, so the algorithm can have 35 waves (from wave 01 to wave 34 plus the wave 00 before/above the wave 01, total = 35 waves)
End of algorithm.
To create the lines function table for the other isos difference: shoot a grey wall with perfect illumination, with the lens in f8 to avoid vignetting, defocused. In computer, convert the image to 16bit, discard colors turning into greyscale. Find the first line of the wave and use the color picker to find the luminance value for each line. Line 16 in the wave will be the maximum luminance value for high iso, line 32 will be the lowest luminance value for low iso. The other high iso lines must be multiplyed by a factor/ratio number to get same luminance value of line 16. The other low iso lines must be multiplyed by a factor/ratio number to get same luminance of line 32. In the conversion table of wave 01 in the algorithm, it shows which line is low iso and high iso. To find the factor/ratio number for each line, divide the luminance value of line 32 by the low iso lines luminance value, and divide the luminance of line 16 by the high iso lines luminance value. After finding the factor/ratio numbers, then create the table. (the wavelength is 32 lines and finding the correct first line, the line 16 and line 32 will be the maximum and minimum luminance values respectively, the wave's ridge and valley). To find the first line of the wave in h264 canon dslr dual iso video, compare the image pixels with the images below.
Other line functions can be created in the algorithm for each line, for example: hue correction, saturation correction, white balance correction...
See the wave and the reconstructed wave:
