Reprint high quality chroma key

Source: Internet
Author: User
Warning:
This is a final report for an older project done for a Berkeley class, so it is a rather low level description.
This project is not currently active, but I might continue it some day.
You may want to skip toResult Section To see if this is of any interest to you.



High quality chroma key

Introduction.

The main goal of this project is to write a high quality chroma key function based on processing of foreground and/or background frames before combining them into an output image. such preprocessing is needed to fight some common chroma key problems: hard edges around foreground objects and poor treatment of colors close to the key color. additionally, a high quality chroma key algorithm is intended to serve as an example of a fairly complex real-world algorithm which wocould be interesting to implement using Intel MMX or other multimedia enhanced architecture. taking into account large amount of effort required for assembly language implementation of any algorithm and little time available for the project, every effort was made to come up with a most simple algorithm which wowould still produce good results.

The algorithm.

A chroma key function is implemented based on a modified version of the algorithm described in [1]. the algorithm works in (Y, CB, Cr) color space which is MPEG's native color space. in this way, incorporation of the chroma key function into an existing system will not require extra color space conversion. schematic description of the algorithm is shown below. the fundamental problem for a chroma key function is, first, to decide which pixels in the foreground image belong to the blue backing and which-to needed foreground objects and, second, how to identify and process boundary pixels. image value at such pixels is due to contributions from both an object and the blue backing: pixel_value = K * object_color + (1-k) * blue_backing_color. one wowould like to solve this equation for two unknowns, K and object_color having only one independent measurement, pixel_value. so, as we can see, this problem is fundamentally underdefined.

If we come up with some reasonable estimate for the contribution of the blue backing to the pixel value, the next step wocould be to subtract this contribution from the pixel value and replace it with a scaled version of the background image. in other words, the overall operation completed MED by Chrome key is ck_result = (fg_pixel_value-blue_backing_contribution) + KBG * bg_pixel_value (EQ 1 ). the problem now is to provide a concrete Algorithm for determining KBG and blue_backing_contribution from the image data.

One such an algorithm is presented below.

First, we rotate (CB, Cr) Coordinate System by an angle defined by the key color to obtain (x, z) coordinate system. second, we use a parameter Alfa (30 to 60 degrees were used for different images) to divide the color space into two regions, one where the processing will be applied and the one where foreground will not be changed (where KBG = 0 and blue_backing_contrubution = 0 in eq.1 abve ). then we "Suppress" pixel value (if it is inside the Working Region) by applying the transformation Z' = z x' = z/TG (Alfa ). this corresponds to projecting the pixel value vector along X axis onto the line dividing the two regions. in this way, we subtract more from a pixel if it is further away from the dividing line. all pixels on the X axis are projected into zero chromaticity point. amount of this suppression, kfg = x-ABS (Z)/TG (Alfa) is used to suppress the third channel, y' = y-y * kfg, where Y is such that y '= 0 for the key color. next, we calculate KBG by interpolating between KBG = 1 on the border of the area shown black on the picture ("Beyond" The Key Color KBG is also set to 1) and KBG = 0 on the line separating the working region from the rest of the color space. lines shown on the picture are lines of constant value of KBG. up to this point, all transformations applied to the image are continuous, so the result of applying eq.1 with blue_backing_contribution and KBG calculated in the described way is guaranteed not to have any hard edged not present in the initial image. unfortunately, real images contain noise which becomes very noticeable after chroma key processing due to the fact that noise in calculated KGB gets multiplied by the pixel value in the background image. probably the simplest way to treat the noise is used here-a circle of a certain radius around the key color is treated as if it is the Key Color: KBG = 1 within this circle. this can introduce Discontinuities on the circle boundary.

Although multiple enhancements to the algorithm described here are possible (more sophisticated noise treatment is an obvious example ), simplicity of the algorithm is of great value both by itself and with regard to MMX implementation which has to be done in assembly language. the algorithm makes use of only two parameters (other that the key color) which need to be adjusted for visual quality of the result-noise level and processing angle and their meaning (what will happen if one increases or decreases parameter value), is clear.

From the implementation point of view, the chroma keyer has the following structure:

Results and discussion.

Typical raw foreground image to be used in chroma key is a set of objects/people shot against very smooth blue backing. quality of the image of this backing, as it will be clear from the examples presented below, is crucial for the quality of the overall process (as it is for any other chroma key implementation ). unfortunately, it's hard to find "real" Images (or frame sequences) intended to be used for Chroma keying, so the testing has been done on images which only very remotely can be treated as a good material. nevertheless, the results are quite encouraging.

In examples below, please click on the image to see a large picture.

Example 1. Noise and color nearness.Foreground is an example of colors in wanted objects being close to the Key Color and some substantial noise in the "backing ".

Original background and foreground images, respectively:

            

Result of applying chroma key with some default parameter settings (assuming no noise). Results of noise in the foreground are apparent:

This image, in fact, represents the worst case-large area of constant color in the background on which even small noise is very noticeable. the same combined scene after adjusting the noise parameter looks nicer but, as noted before, some hard edge appears around the barb wire:

Example 2. fine detail treatment and intermediate results.This is an example of a transparent object in the foreground scene (a cloud) which wocould be lost with any standard chroma key. common processing wowould also have trouble with grass/sky and tree/sky border lines due to some very fine details present there.

Original background and foreground images, respectively:

          

Output of foreground suppresser and backgroung * KBG, respectively:
          

Combined image (sum of the two above ):
 

The image in the last example was processed with noise level set to zero which allowed the cloud abve the tree to stay visible. compare this image with the output of a chroma key function which performs hard switch between foreground and background:
 

As a second part of the project, it was planed to re-implement the chroma key algorithm on MMX architecture as an example of a fairly complex real-world application. one problem with this is that the algorithm used is essential a floating point one. it was not immediately clear to me that its integer re-implementation will be able to provide the same final quality. the algorithm was re-implemented using a combination of regular integer and fixed point arithmetic. images produced with integer version of the chroma key were abstrally identical to those created by a floating point one. the price was to reduce the working angle parameter range so that its tangent fit into reasonable fixed precision representation-this disallowed small and large angles and although did not present a problem for the test images, can be a potential problem. second problem is that the original version uses division and there is no pdiv operation in the MMX instruction set. this was possible to overcome by sacriicing some flexibility of the algorithm-for example, at this stage noise circle was chosen instead of more involved treatment which also was considered. performance of the two versions (integer and floating point) were compared by measuring time required to perform chroma key on several images on a 180 MHz Pentium processor and taking an average of the results. performance measurements is a field of its own and I'm far from claiming that absolute numbers of these very rough measurements have any far reaching meaning, but they do show that integer implementation is about three times faster than the original floating point one. this is not unexpected taking into account larger latency of floating point instructions compared with integer ones and apparent inability of GCC compiler (optimization flag O2) to achieve perfect instruction scheduling. size of the assembly code is also smaller for integer version-346. 396 instructions inside the main loop.

Another implementation is currently being written using MMX emulating functions provided by Intel with "The Complete Guide to MMX technology" Book [2] and software MMX registers. currently, foreground suppresser and key generator (see the chroma keyer structure dimo-abve) are written and tested. this implementation shocould be 1-to-1 convertible into "real" MMX by anyone who wocould like to do it-simply replace a function call by a corresponding assembly language instruction. most annoying problem I encounter so far while implementing the algorithm in MMX is the different representation for CB, Cr and Y components (signed. unsigned) which has to be explicitly addressed. there are certain things which MMX architecture wowould have benefited from if they were present. first, a parallel Division (16-bit number divided by 8-bit number) wowould extent applicability of MMX. in the current study, it was possible to avoid division by changing the algorithm but this might be not an option for other cases. second, images have a tendency to be clustered, which means that chroma key processing shoshould not be applied to large areas. with current architecture, we essentially apply the processing to all points in the image and then mask out of the part which shoshould not have been processed. adding an instruction which does a conditional branch if the content of a 64-bit MMX register is zero wocould avoid applying a several hundred instruction worth of processing to a piece of data in none of the eight (or four) pixels needs it. even if a penalty for such instruction is several cycles per point, it can easily save half of the total processing time for typical images.

Conclusion and future work.

A relatively simple high quality chroma key algorithm was developed based on [1]. it has been implemented using both integer and Floating Point Arithmetic and performance results of the two were roughly compared. because people expressed enough interest to it during the poster session, I intend to finish the implementation with simulation of MMX functions by Intel's regular C routines and software MMX registers. I 'd also like to do "real" chroma-keying (I. e. on video sequences)-video might behave differently from a single image but I suspect that at worst it will be just like a hard-switching chroma key algorithm.

Acknowledgment.

I wowould like to thank processing sor Rowe for suggesting this project to me and for helpful discussion during the midway project progress presentation.

References.

[1]. Keith Jack, "video demystified", independent pub group (Computer), 1996

[2]. Intel Corporation, "The Complete Guide to MMX technology", McGraw Hill text, 1997.

Recent additions:

Promised MMX-ready version of the chroma keyer is now available here along with mdefs. h file. this is a MMX re-implementation of integer only chroma key. please read the comments before using either of these.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.