ffmpeg: adaptively stretch contrast

2

1

Consider a (color) video recorded under poor conditions. Its frames' brightest pixels are well below pure white, its darkest ones well above pure black. Can each frame's contrast be stretched closer to the full brightness range, like an audio compressor/limiter? Can this be done in ffmpeg, using some of its many filters?

Of course, simply slamming each frame's brightest value to white and darkest to black makes the exposure stutter like something shot in 1910. How much to stretch each frame's contrast should depend on the brightest and darkest values of the surrounding frames, probably +-10 seconds.

One could extract all the frames, get each frame's white and black points with imagemagick, store them in an array, smooth the array, stretch each frame's contrast again with imagemagick, and rebuild the video. But that seems positively medieval.

Camille Goudeseune

Posted 2014-03-06T21:20:15.137

Reputation: 1 361

The histeq comes close to what you want. But you can also look at the various flavors of LUT filters and also lut3d.

– Rajib – 2014-03-07T02:41:04.263

The filter 'histeq' looks promising, from the documentation. But when I examine libavfilter/vf_histeq.c filter_frame(), it's just like the filters 'curves', 'lut/lutrgb/lutyuv', and (I think) 'lut3d', which ignore pixels from earlier and later frames. 'histeq' builds histograms only one frame at a time. (But 'histeq' does technically answer the question!) – Camille Goudeseune – 2014-03-07T17:23:43.483

if you're trying to average it out over the length of the video doing something like the imagemagick thing is what needs to be done, whether it's imagemagick or ffmpeg doing it. To get the histogram of the surrounding video all of the surrounding frames will need to be decoded and averaged together. – stib – 2014-03-08T12:05:51.320

@Rajib, please post histeq as an answer, so I can accept it. AFAIK none of ffmpeg's filters use previous or successive frames as input. – Camille Goudeseune – 2014-03-08T16:27:12.297

Answers

2

The histeq comes close to what you want. But you can also look at the various flavors of LUT filters and also lut3d.

The lut filters are more customizable in that you can actually look at a frame and determine what values to use. Moreover, if there is wide variance in the input material viz-a-viz luminance levels, the output might judder when applying a "per frame" correction. Applying a uniform strength of correction to all frames would produce a smoother result.

Rajib

Posted 2014-03-06T21:20:15.137

Reputation: 2 406