00001
00002
00003
00004
00005
00006
00007 #include "Platform/GTAssert.h"
00008 #include "Tools/Streams/InOut.h"
00009 #include "Image.h"
00010 #include "Tools/Math/Common.h"
00011
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
00039
00040
00041 void Image::setColorTable(const ColorTable* ct)
00042 {
00043 colorTable = ct;
00044 }
00045
00046
00047 char Image::getClassifiedColor(int x, int y)
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::convertFromYUVToRGB(const Image& yuvImage)
00055 {
00056 Image::convertFromYCbCrToRGB(yuvImage);
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 }
00081
00082 void Image::convertFromYCbCrToRGB(const Image& ycbcrImage)
00083 {
00084 for(int y=0; y < ycbcrImage.cameraInfo.resolutionHeight; y++)
00085 for(int x=0; x < ycbcrImage.cameraInfo.resolutionWidth; x++)
00086 convertFromYCbCrToRGB(ycbcrImage.image[y][0][x],
00087 ycbcrImage.image[y][1][x],
00088 ycbcrImage.image[y][2][x],
00089 image[y][0][x],
00090 image[y][1][x],
00091 image[y][2][x]);
00092 this->cameraInfo = ycbcrImage.cameraInfo;
00093 }
00094
00095 void Image::convertFromRGBToYUV(const Image& rgbImage)
00096 {
00097 int Y,U,V;
00098 int R,G,B;
00099
00100 for(int y=0; y < rgbImage.cameraInfo.resolutionHeight; y++)
00101 {
00102 for(int x=0; x < rgbImage.cameraInfo.resolutionWidth; x++)
00103 {
00104 R = rgbImage.image[y][0][x];
00105 G = rgbImage.image[y][1][x];
00106 B = rgbImage.image[y][2][x];
00107
00108
00109
00110
00111
00112
00113
00114 Y = (int)( 0.2990 * R + 0.5870 * G + 0.1140 * B);
00115 V = 127 + (int)(-0.1687 * R - 0.3313 * G + 0.5000 * B);
00116 U = 127 + (int)( 0.5000 * R - 0.4187 * G - 0.0813 * B);
00117
00118
00119
00120
00121 if(Y < 0) Y = 0; if(Y > 255) Y = 255;
00122 if(U < 0) U = 0; if(U > 255) U = 255;
00123 if(V < 0) V = 0; if(V > 255) V = 255;
00124
00125 image[y][0][x] = (unsigned char)Y;
00126 image[y][1][x] = (unsigned char)U;
00127 image[y][2][x] = (unsigned char)V;
00128 }
00129 }
00130 this->cameraInfo = rgbImage.cameraInfo;
00131 }
00132
00133 void Image::convertFromYCbCrToHSI(const Image& ycbcrImage)
00134 {
00135 for(int y=0; y < ycbcrImage.cameraInfo.resolutionHeight; y++)
00136 for(int x=0; x < ycbcrImage.cameraInfo.resolutionWidth; x++)
00137 convertFromYCbCrToHSI(ycbcrImage.image[y][0][x],
00138 ycbcrImage.image[y][1][x],
00139 ycbcrImage.image[y][2][x],
00140 image[y][0][x],
00141 image[y][1][x],
00142 image[y][2][x]);
00143 this->cameraInfo = ycbcrImage.cameraInfo;
00144 }
00145
00146 void Image::convertFromYUVToTSL(const Image& yuvImage)
00147 {
00148 int Y,U,V;
00149 int T,S,L;
00150 double y_in, u_in, v_in, tmp, tmp_r, tmp_g, tmp_b, t_out, s_out;
00151
00152 for(int y = 0; y < yuvImage.cameraInfo.resolutionHeight; y++)
00153 {
00154 for(int x = 0; x < yuvImage.cameraInfo.resolutionWidth; x++)
00155 {
00156 Y = yuvImage.image[y][0][x];
00157 U = yuvImage.image[y][1][x];
00158 V = yuvImage.image[y][2][x];
00159
00160
00161 const double pi2_div = 0.15915494309189533576888376337251;
00162
00163 y_in = (double) Y;
00164 u_in = (double) U;
00165 v_in = (double) V;
00166 u_in -= 128.0;
00167 v_in -= 128.0;
00168 tmp = 1.0 / (4.3403 * y_in + 2.0 * u_in + v_in);
00169 tmp_r = (-0.6697 * u_in + 1.6959 * v_in) * tmp;
00170 tmp_g = (-1.168 * u_in - 1.3626 * v_in) * tmp;
00171 tmp_b = ( 1.8613 * u_in - 0.331 * v_in) * tmp;
00172 if (tmp_g > 0.0)
00173 {
00174 t_out = (atan2(tmp_r, tmp_g) * pi2_div + 0.25) * 255.0;
00175 }
00176 else if (tmp_g < 0.0)
00177 {
00178 t_out = (atan2(-tmp_r, -tmp_g) * pi2_div + 0.75) * 255.0;
00179 }
00180 else
00181 {
00182 t_out = 0.0;
00183 }
00184 s_out = sqrt(1.8 * (tmp_r * tmp_r + tmp_g * tmp_g + tmp_b * tmp_b)) * 255.0;
00185
00186
00187 if (t_out < 0.0)
00188 {
00189 t_out = 0.0;
00190 }
00191 else if (t_out > 255.0)
00192 {
00193 t_out = 255.0;
00194 }
00195 if (s_out < 0.0)
00196 {
00197 s_out = 0.0;
00198 }
00199 else if (s_out > 255.0)
00200 {
00201 s_out = 255.0;
00202 }
00203
00204 T = (unsigned char) t_out;
00205 S = (unsigned char) s_out;
00206 L = Y;
00207
00208 image[y][0][x] = (unsigned char)T;
00209 image[y][1][x] = (unsigned char)S;
00210 image[y][2][x] = (unsigned char)L;
00211 }
00212 }
00213 this->cameraInfo = yuvImage.cameraInfo;
00214 }
00215
00216 unsigned char Image::getHighResY(int x, int y) const
00217 {
00218 if ((x & 1) == 0)
00219 if ((y & 1) == 0)
00220 {
00221
00222 y=y>>1;x=x>>1;
00223 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);
00224 }
00225 else
00226 {
00227
00228 y=y>>1;x=x>>1;
00229 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);
00230 }
00231 else
00232 if ((y & 1) == 0)
00233 {
00234
00235 y=y>>1;x=x>>1;
00236 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);
00237 }
00238 else
00239 {
00240
00241 y=y>>1;x=x>>1;
00242 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);
00243 }
00244 }
00245
00246 void Image::setHighResY(int x, int y, unsigned char tl, unsigned char bl, unsigned char tr, unsigned char br)
00247 {
00248 image[y][0][x] = ((int) tl + (int)bl + (int)tr + (int)br) / 4;
00249 image[y][3][x] = ((int)-tl + (int)bl - (int)tr + (int)br) / 4 + 128;
00250 image[y][4][x] = ((int)-tl - (int)bl + (int)tr + (int)br) / 4 + 128;
00251 image[y][5][x] = ((int) tl - (int)bl - (int)tr + (int)br) / 4 + 128;
00252 }
00253
00254 Out& operator<<(Out& stream, const Image& image)
00255 {
00256 stream
00257 << image.cameraInfo.resolutionWidth
00258 << image.cameraInfo.resolutionHeight
00259 << image.frameNumber;
00260
00261 for(int y = 0; y < image.cameraInfo.resolutionHeight; ++y)
00262 for(int c = 0; c < 6; ++c)
00263 stream.write(&image.image[y][c][0], image.cameraInfo.resolutionWidth);
00264
00265 return stream;
00266 }
00267
00268 void Image::setCameraInfo()
00269 {
00270 if(cameraInfo.resolutionWidth == cameraResolutionWidth_ERS210)
00271 {
00272 cameraInfo.openingAngleWidth = openingAngleWidth_ERS210;
00273 cameraInfo.openingAngleHeight = openingAngleHeight_ERS210;
00274 cameraInfo.focalLength = focalLength_ERS210;
00275 cameraInfo.opticalCenter.x = opticalCenterX_ERS210;
00276 cameraInfo.opticalCenter.y = opticalCenterY_ERS210;
00277 cameraInfo.secondOrderRadialDistortion = secondOrderRadialDistortion_ERS210;
00278 cameraInfo.fourthOrderRadialDistortion = fourthOrderRadialDistortion_ERS210;
00279 }
00280 else if(cameraInfo.resolutionWidth == cameraResolutionWidth_ERS7)
00281 {
00282 cameraInfo.openingAngleWidth = openingAngleWidth_ERS7;
00283 cameraInfo.openingAngleHeight = openingAngleHeight_ERS7;
00284 cameraInfo.focalLength = focalLength_ERS7;
00285 cameraInfo.opticalCenter.x = opticalCenterX_ERS7;
00286 cameraInfo.opticalCenter.y = opticalCenterY_ERS7;
00287 cameraInfo.secondOrderRadialDistortion = secondOrderRadialDistortion_ERS7;
00288 cameraInfo.fourthOrderRadialDistortion = fourthOrderRadialDistortion_ERS7;
00289 }
00290 else
00291 {
00292 ASSERT(false);
00293 }
00294 #ifdef _WIN32
00295 if(frameNumber > 0x10000000)
00296 {
00297 frameNumber -= 0x10000000;
00298 cameraInfo.focalLength = cameraInfo.resolutionHeight / tan(cameraInfo.openingAngleHeight / 2.0) / 2.0;
00299 cameraInfo.opticalCenter = Vector2<double>(cameraInfo.resolutionWidth / 2, cameraInfo.resolutionHeight / 2);
00300 cameraInfo.secondOrderRadialDistortion = cameraInfo.fourthOrderRadialDistortion = 0;
00301 cameraInfo.simulated = true;
00302 }
00303 #endif
00304 cameraInfo.focalLengthInv = 1/cameraInfo.focalLength;
00305 cameraInfo.focalLenPow2 = cameraInfo.focalLength*cameraInfo.focalLength;
00306 cameraInfo.focalLenPow4 = cameraInfo.focalLenPow2*cameraInfo.focalLenPow2;
00307 }
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453