Wednesday, September 9, 2020

Cartoonize Video with FFmpeg -- Take 2

 Some time back, I posted a rough attempt to convert a video into a cartoon-style equivalent; here.  That technique converted the video into a series of still images and ran each through ImageMagick sketch filter.  The result looked something like this:




Since then, I play with this time and time again, revising it to something like this:



Again, recently I thought of another technique.  Often, cartoon coloring schemes look 'flattened', meaning red'ish elements are colored red, blue'ish colored blue, so on and so forth.  The 'gex' filter can perform such a conversion pixel-by-pixel and seems promising.  It would require applying a series of filters, for each color so I thought I'd give it a try on simple grayscale first...if it showed promise then expand on a colorized equivalent.  Here is what I got, seems promising:

The technique, in a nutshell, is to convert the video to grayscale, then perform a selective filter on each pixel.  Pixels within the [0,50] luminosity range get assigned 0, pixels within the (50,100) range get assigned 50;
[0,50] = 0
(50,100] = 50
(100,150] = 100
(150,200] = 150
(200,255] = 200

The filter can be a mouthful, taking the form:
$ ffmpeg -i input.mp4 -filter_complex "format=gray,geq=lum_expr='if(lte(lum(X,Y),50),0,if(lte(lum(X,Y),100),50,if(lte(lum(X,Y),150),100,if(lte(lum(X,Y),200),150,if(lte(lum(X,Y),255),200,0)))))'" -acodec copy cartoonX.mp4

Notice, it's a chain of less-than-equal-to conditions of the form if(condition,X,Y) which means if condition set pixel to X, otherwise Y.  To execute the series of else conditions, a nesting of alternative conditions are replaced for Y.  Ugly, but effective and given it doesn't require converting a video to frames and back again a ton faster; still slow'ish, but way faster than the alternative.

Seems to me that this shows a good deal of promise.  The result is dark'ish, but that could be addressed by migrating the pixel assignments to the ceiling of the range, or perhaps applying lightening filter pre-filtering could also address it.  Seems plausible for converting a video to a grayscale cartoon'ish equivalent, using the same technique for color seems like a good next step.




No comments:

Post a Comment