ImageMagick Examples --
Video Handling
- Index
- ImageMagick Examples Preface and Index
- Video to GIF, Optimization Summary
- De-interlace a Video Frame
Video to GIF, Optimization Summary
A software developer who uses IM to create Movie GIFs, Benoit Rouleau, in discussion with me, gave me an AVI video of a plane flying over, to help us mutually explore IM video conversion techniques. However while the AVI itself is quite small, the uncompressed video is a massive bytes in size, and involves colors, over frames.
IM however has no real trouble converting this video into a GIF animation. However be warned that you will probably get some unsupported 'AVI chunk' errors, which can be ignored by using a "-quiet " control setting.
|
Common Global Color Table
Here I Generate a Single Global Color Table for all the frames of the video.
|
Universal Global Color Table
The better way to generate a 'smaller' GIF animation is to just supply a general universal range of colors rather than generate the 'best' global color table for an animation. Use one that should work well regardless of what colors are present in the original video. Another reason for doing this is that you can make you video longer without serious detrimental effects on the color selection, or resorting local color tables for each frame. Each frame is dithered to the same color map, completely independently of what other frames are in the animation.
Here I use a '332' color map which is usually regarded as being a very good standard colormap when no transparency is needed. I have often seen this colormap (or a 219 color 'web-safe' colormap) used often in various video formats.
|
Ordered Dithered Video
The real solution is to use a different color dithering technique, which does not produce a different pattern from one frame to the next.
For example, here I used a Ordered Dither using Posterized Color Levels to dither the same universal '332' colormap.
|
+remap
" operator, to ensure that all the images use the exact same global color map (which the ordered dither already reduced to a maximum of 256 colors). As the number of colors is already optimal, the "+remap
" operator does no dithering, or color reduction. The resulting dither pattern is not random, and does not change greatly from one frame to the next. Thus the 'dither noise' has been remove from the animation resulting in a fixed color pattern from from to frame. The pattern is also very repetitive allowing much better compression. And finally as the color map is fixed, it should work reasonably well regardless of what video is used.
Higher Quality Ordered Dithered Video
This specific video however only uses a small range of colors, mostly various shades of blue, so it doesn't actually use a lot of the colors provided by a general uniform colormap. In fact only colors were used in the last video animation! This is extremely low, and as such also quite visible. But it also means that this particular animation can benefit from using a large number of 'color levels' in the ordered dither operation, so as improve the overall quality. First however we need to determine how many color levels the animation can handle before it reaches the 256 color limit imposed by both the GIF file format and the global colormap re-mapping. The tricky part however is that you must determine these BEFORE you save the animation to the limited GIF format. And here is the command I use... Basically I increased and decreased the number of color levels to use, until I had a figure that was just within the required 256 color limit.
I can then apply the discovered 'color level' choice to the plane animation.
|
Compression (Transparency) Optimization
Because this video uses a panning camera, the background of the video changes from frame to frame. This means the GIF animation will not Frame Optimize very well.
However we can still use a simple Transparency Optimization to further reduce the final size of the GIF animation.
|
Giflossy Compression LZW Optimization
A new tool, GifLossy which is a fork of the original Gifsicle program modifies the colors of each frame so as to allow LZW to compress the image much more.
For example, here I applied it to the original GIF animation, asking it to reduce the colors to a single 256 color table.
|
Emboldened by that above result I decided to use GifLossy on the best ordered dither result we got, to see if it can make it even smaller.
|
De-Interlacing a Video Frame
Not all images are from digital cameras. It is very common to extract images from a digital video feed from a non-CCD video camera. These images are interlaced for direct display on a TV, resulting in every second line being a different frame of the image (interlacing). For two frames where things aren't moving, the interlacing is usually not very noticeable. Perhaps producing only a slight edge blurring of the image. But when a fast moving object is involved, the resulting interlaced image is very disconcerting, as two frames have been merged together.
Wolfgang Hugemann <Auto@Hugemann.de> (Germany), had this problem and sent me a snapshot of a crash test, that Wolfgang took himself. But for demonstration I will use a smaller image cropped from this one. The techniques however will work on the full sized image.
|
Wolfgang Hugemann used a TIFF format for the original video frame, I converted this to PNG for use on IM Examples. Do NOT be tempted to use JPEG for these images, until you have finished processing as it will destroy the low level quality needed for this process. |
Here we just replace one of the interlaced half-frames (every second line) with white. This is the standard de-interlacing method, known as a 'BoB' filter. This was contributed by Wolfgang for IM Examples.
|
pattern:Horizontal2
" built-in image.
That image can then be overlaid with the original, using a 'Screen ' composition method to overlay white lines, or use 'Multiply ' or overlay black lines. For example...
|
Multiply
' to 'Screen
' you can extract frames with a white background.
As an alternative I tried to fill in the missing frame lines by just duplicating the previous line.
|
You can also use a Pixelization Technique to shrink and expand an image so as to double up every second line.
|
And with a slight variation you can combine the lines on both sides to vertically smooth the half-frame image as part of the resize expansion.
|
If you want to extract the other half-frame from the image you can adjust the 'sampling:offset (as of IM v6.8.4-7)
|
-roll
" the image by one pixel, to achieve the same result.