Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Modules/ImageProcessor/ImageProcessorTools/MSH2004ColorCorrector.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file Modules/ImageProcessor/ImageProcessorTools/MSH2004ColorCorrector.cpp
    00003 *
    00004 * This file contains a class that represents a table used for color correction.
    00005 *
    00006 * @author <A href="mailto:walter.nistico@uni-dortmund.de">Walter Nistico</A>
    00007 */
    00008
    00009 #include "MSH2004ColorCorrector.h"
    00010 #include "Tools/Location.h"
    00011 #include "Tools/Streams/InStreams.h"
    00012 #include <math.h>
    00013
    00014
    00015 MSH2004ColorCorrector::MSH2004ColorCorrector()
    00016 {
    00017   bool loaded_y = false, loaded_u = false, loaded_v = false;
    00018   double dummy;
    00019   InBinaryFile stream_y(getLocation().getModelFilename("coeff.cy"));
    00020   if(stream_y.exists())
    00021   {
    00022     stream_y >>  radialOrder;
    00023     stream_y >>  colorOrder;
    00024     int i;
    00025     if (radialOrder < maxRadialOrder)
    00026     { //source array bigger than file
    00027       for (i = 0; i < radialOrder; i++)
    00028         stream_y >>  radialP[i+0*maxRadialOrder];
    00029       for (;i<maxRadialOrder; i++)
    00030         radialP[i] = 0.0;
    00031     }
    00032     else
    00033     {
    00034       for (i = 0; i < maxRadialOrder; i++)
    00035         stream_y >>  radialP[i+0*maxRadialOrder];
    00036       for (;i<radialOrder; i++) //discard higher order coeff that doesnt fit in the array size
    00037         stream_y >>  dummy;
    00038     }
    00039     if (colorOrder < maxColorOrder)
    00040     { //source array bigger than file
    00041       for (i = 0; i < colorOrder; i++)
    00042         stream_y >>  colorP[i+0*maxColorOrder];
    00043       for (;i<maxColorOrder; i++)
    00044         colorP[i] = 0.0;
    00045     }
    00046     else
    00047     {
    00048       for (i = 0; i < maxColorOrder; i++)
    00049         stream_y >>  colorP[i+0*maxColorOrder];
    00050       for (;i<colorOrder; i++) //discard higher order coeff that doesnt fit in the array size
    00051         stream_y >>  dummy;
    00052     }
    00053   }
    00054   else {
    00055     int i;
    00056     for (i = 0; i < radialOrder; i++)
    00057       radialP[i+0*maxRadialOrder] = 0.0;
    00058     for (i=0; i < colorOrder; i++)
    00059       colorP[i+0*maxColorOrder] = 0.0;
    00060   }
    00061   InBinaryFile stream_u(getLocation().getModelFilename("coeff.cu"));
    00062   if(stream_u.exists())
    00063   {
    00064     stream_u >>  radialOrder;
    00065     stream_u >>  colorOrder;
    00066     int i;
    00067     if (radialOrder < maxRadialOrder)
    00068     { //source array bigger than file
    00069       for (i = 0; i < radialOrder; i++)
    00070         stream_u >>  radialP[i+1*maxRadialOrder];
    00071       for (;i<maxRadialOrder; i++)
    00072         radialP[i] = 0.0;
    00073     }
    00074     else
    00075     {
    00076       for (i = 0; i < maxRadialOrder; i++)
    00077         stream_u >>  radialP[i+1*maxRadialOrder];
    00078       for (;i<radialOrder; i++) //discard higher order coeff that doesnt fit in the array size
    00079         stream_u >>  dummy;
    00080     }
    00081     if (colorOrder < maxColorOrder)
    00082     { //source array bigger than file
    00083       for (i = 0; i < colorOrder; i++)
    00084         stream_u >>  colorP[i+1*maxColorOrder];
    00085       for (;i<maxColorOrder; i++)
    00086         colorP[i] = 0.0;
    00087     }
    00088     else
    00089     {
    00090       for (i = 0; i < maxColorOrder; i++)
    00091         stream_u >>  colorP[i+1*maxColorOrder];
    00092       for (;i<colorOrder; i++) //discard higher order coeff that doesnt fit in the array size
    00093         stream_u >>  dummy;
    00094     }
    00095   }
    00096   else {
    00097     int i;
    00098     for (i = 0; i < radialOrder; i++)
    00099       radialP[i+1*maxRadialOrder] = 0.0;
    00100     for (i=0; i < colorOrder; i++)
    00101       colorP[i+1*maxColorOrder] = 0.0;
    00102   }
    00103   InBinaryFile stream_v(getLocation().getModelFilename("coeff.cv"));
    00104   if(stream_v.exists())
    00105   {
    00106     stream_v >>  radialOrder;
    00107     stream_v >>  colorOrder;
    00108     int i;
    00109     if (radialOrder < maxRadialOrder)
    00110     { //source array bigger than file
    00111       for (i = 0; i < radialOrder; i++)
    00112         stream_v >>  radialP[i+2*maxRadialOrder];
    00113       for (;i<maxRadialOrder; i++)
    00114         radialP[i] = 0.0;
    00115     }
    00116     else
    00117     {
    00118       for (i = 0; i < maxRadialOrder; i++)
    00119         stream_v >>  radialP[i+2*maxRadialOrder];
    00120       for (;i<radialOrder; i++) //discard higher order coeff that doesnt fit in the array size
    00121         stream_v >>  dummy;
    00122     }
    00123     if (colorOrder < maxColorOrder)
    00124     { //source array bigger than file
    00125       for (i = 0; i < colorOrder; i++)
    00126         stream_v >>  colorP[i+2*maxColorOrder];
    00127       for (;i<maxColorOrder; i++)
    00128         colorP[i] = 0.0;
    00129     }
    00130     else
    00131     {
    00132       for (i = 0; i < maxColorOrder; i++)
    00133         stream_v >>  colorP[i+2*maxColorOrder];
    00134       for (;i<colorOrder; i++) //discard higher order coeff that doesnt fit in the array size
    00135         stream_v >>  dummy;
    00136     }
    00137   }
    00138   else {
    00139     int i;
    00140     for (i = 0; i < radialOrder; i++)
    00141       radialP[i+2*maxRadialOrder] = 0.0;
    00142     for (i=0; i < colorOrder; i++)
    00143       colorP[i+2*maxColorOrder] = 0.0;
    00144   }
    00145   active = loaded_y||loaded_u||loaded_v;
    00146   //~ if (active)
    00147   //~ {
    00148   setupRadiusLUT();
    00149   setupCorrectionLUT();
    00150   //~ }
    00151 }
    00152
    00153 void MSH2004ColorCorrector::setupRadiusLUT()
    00154 {
    00155   double dx, dy, radius;
    00156   for (int y=0; y<cameraResolutionHeight_ERS7; y++)
    00157     for (int x=0; x<cameraResolutionWidth_ERS7; x++)
    00158     {
    00159       dx = x - centralPointX;
    00160       dy = y - centralPointY;
    00161       radius = sqrt(dx*dx + dy*dy);
    00162       radiusLUT[y][x] = (unsigned char)radius;
    00163     }
    00164 }
    00165
    00166 unsigned char MSH2004ColorCorrector::colorDistortionCorrection(const unsigned char radius_i,
    00167                       const unsigned char color, const unsigned char channel) const
    00168 {
    00169   int radOffsetBase = channel*maxRadialOrder;
    00170   int colOffsetBase = channel*maxColorOrder;
    00171   double radius = radius_i;
    00172   double radial_base;
    00173   double color_base;
    00174   double rad_correction = 0;
    00175   double col_correction = 0;
    00176   int i;
    00177   for (i=0; i<radialOrder; i++){
    00178     radial_base = 1;
    00179     for (int k=0; k<i; k++)
    00180       radial_base *= radius;
    00181     rad_correction += radial_base*radialP[radOffsetBase+i];
    00182   }
    00183   for (i=0; i<colorOrder; i++){
    00184     color_base = 1;
    00185     for (int k=0; k<i; k++)
    00186       color_base *= color;
    00187     col_correction += color_base*colorP[colOffsetBase+i];
    00188   }
    00189   double result = color + rad_correction*col_correction;
    00190   if (result < 0.0)
    00191     result = 0.0;
    00192   if (result > 255.0)
    00193     result = 255.0;
    00194   return (unsigned char) result;
    00195 }
    00196
    00197 void MSH2004ColorCorrector::setupCorrectionLUT()
    00198 {
    00199   for (int radius=0; radius<max_radius; radius++)
    00200     for (int color=0; color<256; color++)
    00201       for (int channel=0; channel<3; channel++)
    00202         colorCorrectionLUT[radius][color][channel] = colorDistortionCorrection(radius, color, channel);
    00203 }
    00204