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

Representations/Perception/Image.cpp

Go to the documentation of this file.
00001 /**
00002  * @file Image.cpp
00003  *
00004  * Implementation of class Image.
00005  */
00006 
00007 #include "Platform/GTAssert.h"
00008 #include "Tools/Streams/InOut.h"
00009 #include "Image.h"
00010 #include "Tools/Math/Common.h"
00011 #include "Modules/ImageProcessor/ImageProcessorTools/ColorModelConversions.h"
00012 
00013 Image::Image() : frameNumber(0)
00014 {
00015   for(int y = 0; y < cameraInfo.resolutionHeight; ++y)
00016     for(int x = 0; x < cameraInfo.resolutionWidth; ++x)
00017     {
00018       image[y][0][x] = 0;
00019       image[y][1][x] = 128;
00020       image[y][2][x] = 128;
00021       image[y][3][x] = 128;
00022       image[y][4][x] = 128;
00023       image[y][5][x] = 128;
00024     }
00025 }
00026 
00027 Image::~Image()
00028 {
00029 }
00030 
00031 bool Image::hasColorTable(void)
00032 {
00033   if (colorTable) return true;
00034   else return false;
00035 }
00036 
00037 
00038 //int Image::getHeight(void)
00039 //{  return height; }
00040 
00041 void Image::setColorTable(const ColorTable* ct)
00042 {
00043   colorTable = ct;
00044 }
00045 
00046 
00047 char Image::getClassifiedColor(int x, int y) const
00048 {
00049   if (colorTable) 
00050     return colorTable->getColorClass(image[y][0][x], image[y][1][x], image[y][2][x]);
00051   else return -1;
00052 }
00053 
00054 void Image::convertFromYCbCrToRGB(const Image& ycbcrImage)
00055 {
00056   for(int y=0; y < ycbcrImage.cameraInfo.resolutionHeight; y++)
00057     for(int x=0; x < ycbcrImage.cameraInfo.resolutionWidth; x++)
00058       ColorModelConversions::fromYCbCrToRGB(ycbcrImage.image[y][0][x],
00059                                             ycbcrImage.image[y][1][x],
00060                                             ycbcrImage.image[y][2][x],
00061                                             image[y][0][x],
00062                                             image[y][1][x],
00063                                             image[y][2][x]);
00064   this->cameraInfo = ycbcrImage.cameraInfo;
00065 }
00066 
00067 void Image::convertFromRGBToYCbCr(const Image& rgbImage)
00068 {
00069   for(int y=0; y < rgbImage.cameraInfo.resolutionHeight; y++)
00070     for(int x=0; x < rgbImage.cameraInfo.resolutionWidth; x++)
00071       ColorModelConversions::fromRGBToYCbCr(rgbImage.image[y][0][x],
00072                                             rgbImage.image[y][1][x],
00073                                             rgbImage.image[y][2][x],
00074                                             image[y][0][x],
00075                                             image[y][1][x],
00076                                             image[y][2][x]);
00077   this->cameraInfo = rgbImage.cameraInfo;
00078 }
00079 
00080 void Image::convertFromYCbCrToHSI(const Image& ycbcrImage)
00081 {
00082   for(int y=0; y < ycbcrImage.cameraInfo.resolutionHeight; y++)
00083     for(int x=0; x < ycbcrImage.cameraInfo.resolutionWidth; x++)
00084       ColorModelConversions::fromYCbCrToHSI(ycbcrImage.image[y][0][x],
00085                                             ycbcrImage.image[y][1][x],
00086                                             ycbcrImage.image[y][2][x],
00087                                             image[y][0][x],
00088                                             image[y][1][x],
00089                                             image[y][2][x]);
00090   this->cameraInfo = ycbcrImage.cameraInfo;
00091 }
00092 
00093 void Image::convertFromHSIToYCbCr(const Image& hsiImage)
00094 {
00095   for(int y=0; y < hsiImage.cameraInfo.resolutionHeight; y++)
00096     for(int x=0; x < hsiImage.cameraInfo.resolutionWidth; x++)
00097       ColorModelConversions::fromHSIToYCbCr(hsiImage.image[y][0][x],
00098                                             hsiImage.image[y][1][x],
00099                                             hsiImage.image[y][2][x],
00100                                             image[y][0][x],
00101                                             image[y][1][x],
00102                                             image[y][2][x]);
00103   this->cameraInfo = hsiImage.cameraInfo;
00104 }
00105 
00106 void Image::convertFromYCbCrToTSL(const Image& ycbcrImage)
00107 {
00108   for(int y=0; y < ycbcrImage.cameraInfo.resolutionHeight; y++)
00109     for(int x=0; x < ycbcrImage.cameraInfo.resolutionWidth; x++)
00110       ColorModelConversions::fromYCbCrToTSL(ycbcrImage.image[y][0][x],
00111                                             ycbcrImage.image[y][1][x],
00112                                             ycbcrImage.image[y][2][x],
00113                                             image[y][0][x],
00114                                             image[y][1][x],
00115                                             image[y][2][x]);
00116   this->cameraInfo = ycbcrImage.cameraInfo;
00117 }
00118 
00119 unsigned char Image::getHighResY(int x, int y) const
00120 {
00121   if ((x & 1) == 0)
00122     if ((y & 1) == 0)
00123     {
00124       // Pixel Top Left       = LL - LH - HL + HH 
00125       y=y>>1;x=x>>1;
00126       return (unsigned char) ((int)image[y][0][x] - (int)image[y][3][x] - 128 - (int)image[y][4][x] - 128 + (int)image[y][5][x] - 128);
00127     } 
00128     else
00129     {
00130       // Pixel Bottom Left    = LL + LH - HL - HH
00131       y=y>>1;x=x>>1;
00132       return (unsigned char)((int)image[y][0][x] + (int)image[y][3][x] - 128 - (int)image[y][4][x] - 128 - (int)image[y][5][x] - 128);
00133     }
00134   else
00135     if ((y & 1) == 0)
00136     {
00137       // Pixel Top Right      = LL - LH + HL - HH
00138       y=y>>1;x=x>>1;
00139       return (unsigned char)((int)image[y][0][x] - (int)image[y][3][x] - 128 + (int)image[y][4][x] - 128 - (int)image[y][5][x] - 128);
00140     }
00141     else
00142     {
00143       // Pixel Bottom Right   = LL + LH + HL + HH
00144       y=y>>1;x=x>>1;
00145       return (unsigned char)((int)image[y][0][x] + (int)image[y][3][x] - 128 + (int)image[y][4][x] - 128 + (int)image[y][5][x] - 128);
00146     }
00147 }
00148 
00149 void Image::setHighResY(int x, int y, unsigned char tl, unsigned char bl, unsigned char tr, unsigned char br) 
00150 {
00151   image[y][0][x] = ((int) tl + (int)bl + (int)tr + (int)br) / 4;
00152   image[y][3][x] = ((int)-tl + (int)bl - (int)tr + (int)br) / 4 + 128;
00153   image[y][4][x] = ((int)-tl - (int)bl + (int)tr + (int)br) / 4 + 128;
00154   image[y][5][x] = ((int) tl - (int)bl - (int)tr + (int)br) / 4 + 128;
00155 }
00156 
00157 Out& operator<<(Out& stream, const Image& image)
00158 {
00159   stream 
00160     << image.cameraInfo.resolutionWidth 
00161     << image.cameraInfo.resolutionHeight 
00162     << image.frameNumber;
00163 
00164   for(int y = 0; y < image.cameraInfo.resolutionHeight; ++y)
00165     for(int c = 0; c < 6; ++c)
00166       stream.write(&image.image[y][c][0], image.cameraInfo.resolutionWidth);
00167 
00168   return stream;
00169 }
00170 
00171 void Image::setCameraInfo()
00172 {
00173   if(cameraInfo.resolutionWidth == cameraResolutionWidth_ERS210)
00174   {
00175     cameraInfo.openingAngleWidth = openingAngleWidth_ERS210;
00176     cameraInfo.openingAngleHeight = openingAngleHeight_ERS210;
00177     cameraInfo.focalLength = focalLength_ERS210;
00178     cameraInfo.opticalCenter.x = opticalCenterX_ERS210;
00179     cameraInfo.opticalCenter.y = opticalCenterY_ERS210;
00180     cameraInfo.secondOrderRadialDistortion = secondOrderRadialDistortion_ERS210;
00181     cameraInfo.fourthOrderRadialDistortion = fourthOrderRadialDistortion_ERS210;
00182   }
00183   else if(cameraInfo.resolutionWidth == cameraResolutionWidth_ERS7)
00184   {
00185     cameraInfo.openingAngleWidth = openingAngleWidth_ERS7;
00186     cameraInfo.openingAngleHeight = openingAngleHeight_ERS7;
00187     cameraInfo.focalLength = focalLength_ERS7;
00188     cameraInfo.opticalCenter.x = opticalCenterX_ERS7;
00189     cameraInfo.opticalCenter.y = opticalCenterY_ERS7;
00190     cameraInfo.secondOrderRadialDistortion = secondOrderRadialDistortion_ERS7;
00191     cameraInfo.fourthOrderRadialDistortion = fourthOrderRadialDistortion_ERS7;
00192   }
00193   else
00194   {
00195     ASSERT(false); // unknown image size
00196   }
00197 #ifdef _WIN32
00198   if(frameNumber > 0x10000000) // is simulated image
00199   {
00200     frameNumber -= 0x10000000;
00201     cameraInfo.focalLength = cameraInfo.resolutionHeight / tan(cameraInfo.openingAngleHeight / 2.0) / 2.0;
00202     cameraInfo.opticalCenter = Vector2<double>(cameraInfo.resolutionWidth / 2, cameraInfo.resolutionHeight / 2);
00203     cameraInfo.secondOrderRadialDistortion = cameraInfo.fourthOrderRadialDistortion = 0;
00204     cameraInfo.simulated = true;
00205   }
00206 #endif
00207   cameraInfo.focalLengthInv = 1/cameraInfo.focalLength;
00208   cameraInfo.focalLenPow2 = cameraInfo.focalLength*cameraInfo.focalLength;  
00209   cameraInfo.focalLenPow4 = cameraInfo.focalLenPow2*cameraInfo.focalLenPow2;
00210 }

Generated on Mon Mar 20 22:00:03 2006 for GT2005 by doxygen 1.3.6