A Practical Bayer Demosaicing Algorithm for Gphoto
Total Page:16
File Type:pdf, Size:1020Kb
A Practical Bayer Demosaicing Algorithm for Gphoto Theodore Kilgore Mathematics and Statistics Auburn University, Alabama 36849 Abstract The Gphoto project exists to provide compression has already taken place. With only few free and open-source software support for USB exceptions, a consumer-grade digital camera pro- and serial digital still cameras which use miscel- duces a color photo by having a grid of color filters laneous proprietary interface protocols and image placed between the microsensors and the lens. Of- data formats. The cameras supported range from ten, the color filters are also microlenses which better very cheap to very expensive, but one thing that capture the intensity of the light, as well as giving many of them need is a good and practical Bayer preference to a certain color. A standard practice demosaicing algorithm. The algorithm should get is to use red (R), green (G), and blue (B). The mi- good results with cheap cameras. The algorithm crosensors with their filters on them are arranged in should not require a memory footprint which is a pattern often called a Bayer array, named after unduly heavy, and the algorithm should not re- its inventor, B. E. Bayer. There are four possible quire excessive time to run, because gphoto should variants called tilings. A typical one looks like run even on fairly small computers. However, the priority is to support still cameras, not pccams row 0 RGRGRG ... (until w is reached) or webcams, which makes the execution time to row 1 GBGBGB ... (until w is reached) be a secondary issue, not a primary one. Re- and then the two-row pattern is repeated down- cently, a practical implementation of the adap- wards, until there are h rows in all. This pattern tive homogeneity-directed Bayer demosaicing al- is called RGGB, because it consists of repetitions of gorithm has been written, which meets the above a basic 2x2 tile which looks like criteria and seems to give very nice results. R G G B Keywords: Visual imaging, Bayer interpolation, demosaicing, Gphoto project Three other patterns, called BGGR and GRBG and GBRG, can achieve similar results. In all of these four Bayer tiling patterns there is twice as much 1 Introduction green data as there is of either red or blue. The reason for this is based upon known visual qualities Behind the lens of a digital camera is a sensor chip. of the human eye. In any event, the raw, uncom- On the surface of the sensor chip, a large number of pressed data from many digital cameras consists of photosensitive microsensors are arranged in a rect- precisely w ∗ h bytes of data arranged in one of the angular grid. For an uncompressed bitmapped im- standard Bayer tiling patterns. age, the rectangular array is w sensors wide and h Now, there is a fundamental problem. To con- sensors high, where w is the pixel width of the fin- struct an image requires data about each of the three ished image and h is its pixel height. Thus, for ex- colors at each pixel. In one of the basic, simple for- ample, a 640x480 image will be produced by an array mats used both in BMP image data and in PPM containing 480 rows of microsensors, in which each (Portable PixMap), one byte of data is required for row is 640 microsensors wide. each of the colors R, G, and B, at each pixel lo- At each of these microsensors, the intensity of cation in the image. Intensity is measured at each light striking it is recorded when a picture is microsensor, then, on a scale from 0 to 0xff for the snapped. The data may then be compressed. How- color which is sampled there. Consequently, we have ever, for the present purpose we will assume either only w∗h pieces of data, and we need to have 3∗w∗h that no such compression has taken place or that de- pieces of data (one byte for each color at the given pixel location) in our finished image. How to do more complicated procedures, such as cubic spline that? The short answer is: Use educated guesswork. interpolation instead of piecewise-linear averaging. We now discuss some possible methods for doing In practice, though, the greater complexity does not that. In fact we have a problem which is known seem always to do a very much better job. from the outset to have no exact solution, which to Some of the more sophisticated methods for do- some extent accounts for the huge proliferation of ing the interpolation involve such things as gradient methods for solving it. searches for the likeliest weighted linear combina- tion of readings at nearby pixels, or involve some basic schemes for edge detection and a method for 2 Methods of doing Bayer in- deciding to use some of the nearby readings and to terpolation. ignore others, in case there is suspicion of an edge. The method currently in use in the Gphoto project The most primitive method for supplying a missing was introduced in [6], in 2007. That method in- color at a pixel location is just to take one of the volves some vertical and horizontal edge detection nearest actual readings of the same color. Suffice and also gives a special role to the green data, as it to say that the crudity of the results thereby ob- there is twice as much green data as red or blue. tained is at least equal to the crudity of the method. Improvement over the previous bilinear interpola- tion method was obvious when this edge detection The most obvious approach is to use an averag- was introduced. Moreover, this improvement in ar- ing of nearest-neighbor data, called bilinear inter- tifact reduction and image clarity was done without polation. This method requires special treatment great increase in processing complexity or memory at edges and corners of the image, obviously, be- footprint. cause the number of nearest neighbors is not the same there. To describe the method with simplicity, therefore, let us assume we are well away from edges and corners. We might, for example, need to supply 3 Adaptive Homogeneity- a missing datum for G in the center of directed Demosaicing G1 Somewhere, while studying about the interpolation G2 C G3 or demosaicing problem, someone stepped back and G4 noticed that, in spite of the difficulties involved in in which C can signify that either R or B is the color re-creating the different colors at their missing loca- actually sampled. Other possible arrangements can tions all over an image, data about the entire image arise if we need to supply a missing datum for ei- is present nevertheless. For, at every pixel location ther R or B, giving minor but irritating variants on one piece of data has been gathered. However, for a the required procedure. However, in the situation long time it seems not to have been clear at all how just described the way to do bilinear interpolation this fact might actually be helpful toward a better is simply to take the average of G1, G2, G3, G4. Ob- reconstruction of the image. Indeed, it could be said viously, this procedure can give very nice results if without exaggerating very much, that the interpola- done in a region of an image where things are fairly tion methods described above have lost sight of the homogeneous, but the procedure can also act quite fact that data is present for the whole image, while badly, causing fuzziness, jaggedness, or other arti- concentrating on reconstructing the missing data for facting at edges in an image. One of the ugliest of the individual colors, viewed in isolation from one these artifacts is called the “zipper” effect, or the another. “checkerboard” effect, in which the edge comes out One method which has put these observations to as alternating pixels of different, contrasting colors good use is given in [1] of Hirakawa and Parks, in or light intensity. An example of the zipper effect 2005. In briefly describing what is done there, we may be seen in the first of the images in Section 5. will assume that the raw data from the camera has Bilinear interpolation was in use in the Gphoto been spread out already to a size of 3 ∗ w ∗ h, so that project for quite some time, the basic algorithm ap- three bytes are allocated for each pixel; the data al- pearing in libgphoto2/libgphoto2/bayer.c, [5] writ- ready present has been put into its proper, respective ten by Lutz M¨uller in 2001. In commercial camera locations, and the “missing” data is represented by software such as OEM camera driver software, bi- filler bytes which are still 0. This then is the input linear interpolation is often replaced by similar but of the procedure, which will fill in the 0 bytes with data and will give back the same data as an output very resource-intensive to implement. The first step but with the blank spaces now filled in. Then, the in the algorithm is to convert image h and image v, AHD algorithm proceeds as follows: each of which has been completely filled out in ac- First, make two additional copies of the image. cord with the previous steps, from RGB colorspace Let us call them image v and image h, because we to CIELAB colorspace, and the choice algorithm is are going to work in one of the copies only in the carried out there. The details have no place here, vertical direction and in the other one only in the and so they are omitted.