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.
|
|
bytes in size, which while only 1/5th the size, due to color reduction and GIF pixel data compression, it is still rather large. Also if you study the resulting animation further you will find that of the
frames in the image,
frames had their own own separate local color table added. That is, each and every frame in the GIF animation required there own color index table. That is, while each frame has less that 256 colors (due to the GIF format limitations), the whole animation is using a total of
colors. Unfortunately the GIF format does not compress color tables, so all those extra color tables could be using up to: 256 colors * 3 byte per color * 106 frames; or 81,408 bytes of file space. Not a lot for a 1Gbyte video but still an appreciable amount of space, especially as we optimize the video further. Added to this is that the animation will not GIF frame optimize very well. Not only because the background is moving (due to the camera panning upward), but also because IM used a Error Correction Dither (Hilbert Curve Dither), which produces a pseudo-random pattern of colors that is different from frame to frame. A later example will make this 'dither noise' much more visible.
Common Global Color Table
Here I Generate a Single Global Color Table for all the frames of the video.
local color tables, and a file size of bytes.
|
|
of the 'best' colors based on all the frames in the animation. Unfortunately this also resulted in the pixel data not compressing as well as it did before, as a stronger dither was required. The result is a slightly worse looking animation, that is roughly the same size as the previous. For this specific video of limited colors, I could even reduce the number of colors used even further say to only 64 colors without too many problems, producing an even smaller animation file size. This however is very dependent on the video sequence used, and may not look very good. Your own video may have a better result or worse result, especially when dealing with a video that uses a lot more colors and possibly multiple scenes.
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.
|
|
local color tables, and as a result the animation is smaller or
bytes in size. The problem however is that you will often see an obvious and annoying 'noise' in areas of constant color. This noise was also present in ALL the previous video animations. It is only now visible due to the use of a more universal, and thus more widely spread out color mapping. The noise is actually caused by the dithering of the reduced color set when regenerating the image. However, this produces a pseudo-random pattern of colors that changes from frame to frame, resulting in the appearance of background noise in the image. See Problems with E-Dithers for more detail as to why this happens.
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.
local color tables, and is bytes in size.
|
|
+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.
local color tables, is bytes in size, and colors.
|
|
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.
bytes in size, and colors.
|
|
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.
bytes. It isn't nearly as high a quality as what we achieved using ordered dither but it is less than 1/2 the size.
|
|
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.
bytes. Unfortunately we basically lost the high quality ordered dither result we works so hard to achieve before. Which is disappointing.
|
|
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.
![[IM Output]](plane.gif)
![[IM Text]](plane_cgc_ctables.txt.gif)
![[IM Text]](plane_cgc_size.txt.gif)
![[IM Output]](plane_cgc.gif)
![[IM Output]](plane_ugc.gif)
![[IM Text]](plane_ugc_nd_ctables.txt.gif)
![[IM Text]](plane_ugc_nd_size.txt.gif)
![[IM Output]](plane_ugc_nd.gif)
![[IM Text]](plane_od_ctables.txt.gif)
![[IM Text]](plane_od_size.txt.gif)
![[IM Output]](plane_od.gif)
![[IM Text]](plane_od2_find.txt.gif)
![[IM Text]](plane_od2_ctables.txt.gif)
![[IM Text]](plane_od2_size.txt.gif)
![[IM Text]](plane_od2_ncolors.txt.gif)
![[IM Output]](plane_od2.gif)
![[IM Text]](plane_opt_size.txt.gif)
![[IM Text]](plane_opt_ncolors.txt.gif)
![[IM Output]](plane_opt.gif)
![[IM Text]](plane_giflossy_size.txt.gif)
![[IM Output]](plane_giflossy.gif)
![[IM Text]](plane_od2_giflossy_size.txt.gif)
![[IM Output]](plane_od2_giflossy.gif)
![[IM Output]](interlaced.png)
![[IM Output]](deinterlace_1.png)
![[IM Output]](deinterlace_2.png)
![[IM Output]](deinterlace_3.png)
![[IM Output]](deinterlace_4.png)
![[IM Output]](deinterlace_5.png)
![[IM Output]](deinterlace_6.png)