• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/AutoWhiteBalance.cpp

00001 #include "FCam/AutoWhiteBalance.h"
00002 #include "FCam/Sensor.h"
00003 #include "FCam/Platform.h"
00004 #include "Debug.h"
00005 
00006 
00007 namespace FCam {
00008 
00009 void autoWhiteBalance(Shot *s, const Frame &f, 
00010                       int minWB,
00011                       int maxWB,
00012                       float smoothness) {
00013     if (!s) return;
00014 
00015     if (!f.histogram().valid()) return;
00016 
00017     // auto-white-balance based on the histogram
00018     int buckets = f.histogram().buckets();
00019     
00020     // Compute the mean brightness in each color channel
00021 
00022     int rawRGB[] = {0, 0, 0};
00023 
00024     for (int b = 0; b < buckets; b++) {
00025         // Assume the color channels are GRBG (should really switch
00026         // based on the sensor instead)
00027         rawRGB[0] += f.histogram()(b, 0)*b;
00028         rawRGB[1] += f.histogram()(b, 1)*b;
00029         rawRGB[2] += f.histogram()(b, 2)*b;
00030     }
00031 
00032     // Solve for the linear interpolation between the RAW to sRGB
00033     // color matrices that makes red = blue. That is, we make the gray
00034     // world assumption.
00035 
00036     float RGB3200[] = {0, 0, 0};
00037     float RGB7000[] = {0, 0, 0};
00038     float d3200[12];
00039     float d7000[12];
00040     f.platform().rawToRGBColorMatrix(3200, d3200);
00041     f.platform().rawToRGBColorMatrix(7000, d7000);
00042 
00043     for (int i = 0; i < 3; i++) {
00044         for (int j = 0; j < 3; j++) {
00045             RGB3200[i] += d3200[i*4+j]*rawRGB[j];
00046             RGB7000[i] += d7000[i*4+j]*rawRGB[j];
00047         }
00048     }
00049 
00050     float alpha = (RGB3200[2] - RGB3200[0])/(RGB7000[0] - RGB3200[0] + RGB3200[2] - RGB7000[2]);
00051 
00052     // inverse wb is used as the interpolant, so there's lots of 1./
00053     // in this formula to make the interpolant equal alpha as desired.
00054     int wb = int(1./(alpha * (1./7000-1./3200) + 1./3200));
00055 
00056     if (wb < minWB) wb = minWB;
00057     if (wb > maxWB) wb = maxWB;
00058 
00059     s->whiteBalance = smoothness * s->whiteBalance + (1-smoothness) * wb;   
00060 }
00061 
00062 
00063 }

Generated on Fri Sep 24 2010 15:53:00 for FCam by  doxygen 1.7.1