修正改装手机拍出的偏粉照片 色彩校准后续实验Correcting the Pink Tint in Photos from a Modified Phone, a Color Calibration Follow-Up


AI free token/AI免费积分

修正改装手机拍出的偏粉照片 色彩校准后续实验

大约一周前,我发布了一篇 Ask Hackaday 求助帖,在帖中我向社区求助,解决我在把手机改装成 C 卡口(C mount)时遇到的一个常见相机改装问题。简而言之,当你把一台带有微小镜头的相机换个镜头时,拍出来的照片会有色偏。通常中心的颜色是正确的,然后向外逐渐变成一圈粉红色。评论者们给出了正确的答案,指出问题出在手机软件施加了一项校正,以补偿那颗相当糟糕的微型镜头。这个问题并不只出现在我身上,它在许多类似项目中都出现过,包括这一个。所以在弄清了问题的本质之后,我的下一步就是研究如何解决它。

当然,我并不是第一个以某种形式遇到这个问题的人,因此不出所料,摄影界有一项经过充分验证的技术来纠正它。它被称为平场校正(flat field correction),其原理是校正图像中的每个像素,使其增益与图像中心像素的增益相匹配。这是一个相当直接的数学过程,为了测量这些像素的相对增益,需要相机对一块均匀照明的白色表面拍摄一张校准图像。其思路是,这张图像中的每个像素都会给出它与中心像素之间的差异,从而得出该像素所需的校正量。

拍摄校准图像很容易,只需将相机对准某个白色且光照均匀的物体,并使其失焦,以消除任何表面纹理的影响。这张照片的分辨率必须与要处理的照片相同。我在我的创客空间(hackerspace)里,从拍摄毛绒玩具照片时大约不到一米的距离处,对着同一张白色桌子拍了一张照片,镜头设为无限远对焦。得到的图像单独呈现出圆形的粉色图案,非常清楚地表明了问题的本质。

进行平场校正最简单的方法,或许就是在你常用的照片编辑软件里完成。我用的是 GIMP,不过我猜其他软件的操作路径也大同小异。

打开你要校正的照片。结果会更亮,因此你需要降低其曝光(颜色菜单 > 曝光)。

将编码精度更改为 32 位浮点(32-bit floating point)(图像菜单 > 编码)。由于这是一个数学处理过程,这样做可以减少任何可能影响画质的舍入误差。

将校准图像作为新图层打开,放在照片上方(文件菜单 > 作为图层打开)。一些指南随后建议对该图层应用高斯模糊,以进一步降噪。我两种方式都试过,发现差别不大。

将图层模式从“正常”(Normal)改为“除法”(Divide)。该选项位于图层面板中,在图层列表上方的下拉菜单里。

按照这些步骤操作,就能得到一张已应用平场校正的图像。当然,它可能仍需要其他处理,比如白平衡,但粉色圆环已经消失了。

对于单张照片,简单的 GIMP 工作流程是一种轻松的校正方法,但要转换一整天拍摄的照片,很可能就成了一件头疼的事。对于一个装满图片的目录,如果可以的话,使用 ImageMagick 更合适;而对于视频,我需要能够使用 ffmpeg。所以经过一番摸索,下面就是针对这两个命令行工具执行相同处理的过程。我还把 shell 命令放在了一个 GitHub 仓库里,以防你的浏览器把下面这些命令的复制粘贴弄乱。

第一项任务是用我之前拍摄的校准图像创建一张校准图(calibration map)。这是一张 PNG 图像,其像素表示与图像中心的电平差,并归一化到 50% 灰色;而原始图像显示的是其在拍摄时各种光照条件下的相对值。-blur 选项应用模糊以去除图像中的任何纹理,而 -fx 选项则包含执行该计算所需的数学公式。我并不羞于承认,当初我得到了很多帮助才把这些步骤弄对,对此我心怀感激。

创建好校准图之后,现在就可以将其应用到照片上。乘以 0.5 是为了补偿校准图以 50% 灰色为中心这一点,而除法(divide)功能与上面 GIMP 版本中的相同。

现在来处理视频!在 ffmpeg 中执行同样的任务,其过程与 ImageMagick 相同,但校准图必须与视频具有相同的分辨率。最直接的方法是拍摄一段被照亮的白色表面的短视频,而不是拍一张静态照片,然后将其所有帧平均成一张图像。得到的结果与静态校准图像类似,但这样做很重要,因为摄像应用可能对图像施加与拍照应用不同的处理。

然后,这张图像可以用与 ImageMagick 版本完全相同的方式处理。同样地,先应用模糊,再用相同的公式来制作校准图。

有了这张校准图,就可以对视频本身执行转换了。不过这是一个缓慢的过程,在我那台普通的 Thinkpad 上,它每秒只能处理 1.8 帧。

这样我们就把问题确定为平场校正,并给出了三种处理方法。这台相机现在离派上用场近多了,即使处理步骤可能有点繁琐,照片也可能需要多一点后期处理。显然,在手机硬件中完成这件事是最好的,但由于无法访问一台 13 年前三星手机的内部结构,这已经是次优的选择了。

本文来源:https://hackaday.com/2026/11/23/correcting-the-pink-photo-from-my-modified-phone-a-follow-up/
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

Correcting the Pink Tint in Photos from a Modified Phone, a Color Calibration Follow-Up

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.

Of course I am not the first to encounter this in some form, thus as expected there’s a well-tested technique in photography to correct it. It’s called flat field correction , and it works by correcting each pixel in the image to match the gain to that of the pixels in the centre of the image. It’s a straightforward enough mathematical procedure, and to measure those relative pixel gains it requires that the camera take a calibration image of a uniformly lit white surface. The idea is that each pixel in this image will deliver the difference between it and the pixels in the centre, and thus the required correction for that pixel.

Taking a calibration image is easy, and involves pointing the camera at something white and reasonably lit, and unfocusing it to remove any surface texture effects. The picture must be at the same resolution as the photo to be processed. I took a picture of the same white table in my hackerspace from the stuffed animals picture at somewhere under a metre’s distance, with the lens set to infinity focus. The resulting image shows the circular pink pattern alone, and very clearly indicates the nature of the problem.

Perhaps the easiest way to perform flat field correction is in your normal photo editing package. I use GIMP, but I am guessing that other packages will follow a similar path.

Open your photo to be corrected. The result will be brighter so you’ll need to reduce its exposure (Colours menu > Exposure).

Change the encoding precision to 32-bit floating point (Image menu > Encoding). Since this is a mathematical process, this reduces any rounding errors that might affect picture quality.

Open the calibration image as a new layer above the photo (File menu > Open as layers). Some guides then suggest applying a Gaussian blur to this layer to further reduce noise. I tried either, and found it made little difference.

Change the layer’s mode from “Normal”, to “Divide”. This can be found in the Layers panel, in a pulldown above the list of layers.

Following these steps delivers an image with the flat field correction applied. Of course, it may still need other processing such as white balance, but the pink ring is gone.

A straightforward GIMP workflow is an easy way to correct a single photo, but converting a day’s worth of pictures is likely to become a major pain. For a 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 in a GitHub repository , in case your browser manages to mangle cut-and-pasting the commands below.

The first task is to create a calibration map from the calibration image I took earlier. This is a PNG image whose pixels represent the level difference from the centre of the image normalised to 50% grey, as opposed to the original image which shows their relative values at whatever illumination it was taken under. The -blur option applies a blur to remove any textures in the image, while the -fx option contains the mathematical formula required to apply the calculation. I am not ashamed to admit that I had a lot of help in getting these steps right, for which I am thankful.

Having created the calibration map image, it can now be applied to the photo. The multiplication by 0.5 is a compensation for the calibration map being centred around 50% grey, and the divide function is the same as that in the GIMP version above.

Now for video! Performing the same task in ffmpeg follows the same process as with Image Magick, but the calibration map must be the same resolution as the video. The most straightforward way to do this is to make a short video of the illuminated white surface rather than a still photo, and average all of its frames into a single image. The result is a similar one to the still calibration image, but it’s important to do it this way as the video camera app may apply different processing to the still camera one.

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.

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.

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.

本文来源:https://hackaday.com/2026/11/23/correcting-the-pink-photo-from-my-modified-phone-a-follow-up/
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry