Flat frame experiment¶

Short version - for light spots in H.264 clips¶

Noticed some artifacts in a few H.264 clips recorded with 5D Mark III and 50/1.8 at f/22, crop_rec set to 1:1. Apparently these are caused by dust on the sensor and made obvious at narrow apertures. Here's one frame:

In [1]:
%%shell
ffmpeg -loglevel quiet -hide_banner -i sample.mov -vframes 1 -y sample.ppm
convert sample.ppm sample.jpg # for preview

sample

Luckily, the spot was large enough to be noticed on the camera screen, so I've recorded a short clip with just the blank sky, moving the camera around to average out the clouds.

In [2]:
%%shell
ffmpeg -loglevel quiet -hide_banner -i sky.mov -f image2pipe -vcodec ppm - | convert - -evaluate-sequence mean sky.ppm
convert sky.ppm sky.jpg # for preview

sky

Let's get a flat frame out of this!

In [3]:
sky = double(imread('sky.ppm'));
sr = sky(:,:,1);
sg = sky(:,:,2);
sb = sky(:,:,3);
ref = sky * 0;
ref(:,:,1) = mean(sr(:));
ref(:,:,2) = mean(sg(:));
ref(:,:,3) = mean(sb(:));
flat = sky ./ ref;
imshow(flat / 2)

Better just make it grayscale.

In [4]:
imshow([flat(:,:,1), flat(:,:,2), flat(:,:,3)])

Keeping just the green channel, as it seems to be cleaner.

In [5]:
gflat = flat(:,:,2);
imshow(gflat)
mean(gflat(:))
ans =  1.0000

LGTM, now let's correct our test frame.

In [6]:
im = double(imread('sample.ppm'));
ca(:,:,1) = im(:,:,1) ./ gflat;
ca(:,:,2) = im(:,:,2) ./ gflat;
ca(:,:,3) = im(:,:,3) ./ gflat;
imshow(ca/255)

LOL, now that's some overcorrection :)

Let's try some gamma correction (trial and error).

In [7]:
g = 1.6;
cb(:,:,1) = (im(:,:,1).^g ./ gflat).^(1/g);
cb(:,:,2) = (im(:,:,2).^g ./ gflat).^(1/g);
cb(:,:,3) = (im(:,:,3).^g ./ gflat).^(1/g);
imshow(cb/255)

Individual channels:

In [8]:
imshow([cb(:,:,1); cb(:,:,2); cb(:,:,3)])

Not perfect, but not that bad either.

It should work a lot better if you try to correct some raw footage, as the response there is linear. In H.264, it's not. Reversing the curves for the picture style used in this clip might also help.

To see that troublesome spot completely fixed, please check the long version. The extra steps are only needed in extreme cases with H.264 (not with RAW).

Correcting the entire footage is left as an exercise for the reader. Need some hints?

OK, now gotta clean that sensor.