A week or so ago I posted an Ask Hackaday, in which I asked the community for help with a common camera hacking problem I was having with my mobile phone to C mount conversion. In short, when you take a camera with a tiny lens and change that lens for another, the resulting photographs have a colour cast. It’s usually the right colour in the centre, and moves to a pink colour in a ring around the photo. The commenters came up with the right answer, and identified the problem as the phone software applying a correction to compensate for the relatively awful tiny lens. It’s a problem not limited to me, it’s appeared in many such projects including this one. So having been educated as to the nature of the problem, my next step was to look at how to fix it.
It’s A Flat Field Correction Problem, Folks!

Of course I am not the first to encounter this in some form, thus as directory full of pictures it makes sense to use Image Magick if I can, and for a video I need to be able to use ffmpeg. So after a bit of messing about, here is the same process, for both command line packages. I have also placed the shell commands
This image can then be processed in exactly the same way as the Image Magick version. Again, it’s applying a blur, and the same formula to make the calibration map.
ffmpeg -i calibration-image-video.png -vf "format=gbrp16le,gblur=sigma=60, geq=r='32768*r(X,Y)*g(W/2,H/2)/(max(g(X,Y),1)*max(r(W/2,H/2),1))': g='32768': b='32768*b(X,Y)*g(W/2,H/2)/(max(g(X,Y),1)*max(b(W/2,H/2),1))'" -frames:v 1 -update 1 -pix_fmt rgb48be calibration-map-video.png
Using the map, the conversion can then be performed on the video itself. This is a slow process though, on my run-of-the-mill Thinkpad it was processing only 1.8 frames per second.
ffmpeg -i your-video.mp4 -loop 1 -i calibration-map-video.png -filter_complex "[0:v]format=gbrp16le; [1:v]format=gbrp16le[map]; [map]blend=all_expr='A*32768/max(B,1)':shortest=1, format=yuv420p[out]" -map '[out]' -map '0:a?' -c:v libx264 -preset medium -crf 18 -c:a copy your-video-corrected.mp4
So we’ve identified the problem as flat-field correction, and here are three ways to do it. The camera is now a lot closer to being useful, even if the processing steps can be a little cumbersome and the photos may require a little more post-processing. It’s obvious that in the phone hardware is the best place to do this, but without access to the internals of a 13-year-old Samsung this is the next best thing.


