ColorSpace.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @file ColorSpace.cpp
  3. * @brief __DESC__
  4. * @author Michael Koch
  5. * @date 07/28/2008
  6. */
  7. #include "core/image/ImageT.h"
  8. #include "core/vector/VectorT.h"
  9. #include "core/vector/MatrixT.h"
  10. #include <iostream>
  11. #include "ColorSpace.h"
  12. #include <math.h>
  13. #include "vislearning/baselib/cc.h"
  14. using namespace OBJREC;
  15. using namespace std;
  16. using namespace NICE;
  17. //bad position for this function
  18. void ColorSpace::ColorImagetoMultiChannelImage( const NICE::ColorImage &imgrgb, NICE::MultiChannelImageT<double> &genimg )
  19. {
  20. genimg.reInit( imgrgb.width(), imgrgb.height(), 3);
  21. for ( int y = 0;y < imgrgb.height();y++ )
  22. {
  23. for ( int x = 0;x < imgrgb.width();x++ )
  24. {
  25. double r, g, b;
  26. r = imgrgb.getPixelQuick( x, y, 0 );
  27. g = imgrgb.getPixelQuick( x, y, 1 );
  28. b = imgrgb.getPixelQuick( x, y, 2 );
  29. genimg.set( x, y, r, 0 );
  30. genimg.set( x, y, g, 1 );
  31. genimg.set( x, y, b, 2 );
  32. }
  33. }
  34. }
  35. //TODO test it
  36. int checkRange( int in, int min = 0, int max = 255 )
  37. {
  38. int out = in;
  39. if ( in < min )
  40. {
  41. out = min;
  42. }
  43. if ( in > max )
  44. {
  45. out = max;
  46. }
  47. return out;
  48. }
  49. NICE::MultiChannelImageT<double> ColorSpace::rgbtohsl( const NICE::MultiChannelImageT<double> &imgrgb )
  50. {
  51. NICE::MultiChannelImageT<double> imghsl( imgrgb.width(), imgrgb.height(), 3 );
  52. for ( int x = 0;x < imgrgb.width();x++ )
  53. {
  54. for ( int y = 0;y < imgrgb.height();y++ )
  55. {
  56. double R, G, B, H, S, L;
  57. R = ( double )imgrgb.get( x, y, 0 );
  58. G = ( double )imgrgb.get( x, y, 1 );
  59. B = ( double )imgrgb.get( x, y, 2 );
  60. ColorConversion::ccRGBtoHSL( R, G, B, &H, &S, &L );
  61. imghsl.set( x, y, H, 0 );
  62. imghsl.set( x, y, S, 1 );
  63. imghsl.set( x, y, L, 2 );
  64. }
  65. }
  66. return imghsl;
  67. }
  68. NICE::MultiChannelImageT<double> ColorSpace::hsltorgb( const NICE::MultiChannelImageT<double> &imghsl )
  69. {
  70. NICE::MultiChannelImageT<double> imgrgb( imghsl.width(), imghsl.height(), 3 );
  71. for ( int x = 0;x < imghsl.width();x++ )
  72. {
  73. for ( int y = 0;y < imghsl.height();y++ )
  74. {
  75. double R, G, B, H, S, L;
  76. H = ( double )imghsl.get( x, y, 0 );
  77. S = ( double )imghsl.get( x, y, 1 );
  78. L = ( double )imghsl.get( x, y, 2 );
  79. ColorConversion::ccHSLtoRGB( H, S, L, &R, &G, &B );
  80. imgrgb.set( x, y, ( int )round( R*255.0 ), 0 );
  81. imgrgb.set( x, y, ( int )round( G*255.0 ), 1 );
  82. imgrgb.set( x, y, ( int )round( B*255.0 ), 2 );
  83. }
  84. }
  85. return imgrgb;
  86. }
  87. NICE::MultiChannelImageT<double> ColorSpace::rgbtolab( const NICE::MultiChannelImageT<double> &imgrgb )
  88. {
  89. NICE::MultiChannelImageT<double> imglab( imgrgb.width(), imgrgb.height(), 3 );
  90. //preprocessing RGB to XYZ
  91. for ( int x = 0;x < imgrgb.width();x++ )
  92. {
  93. for ( int y = 0;y < imgrgb.height();y++ )
  94. {
  95. double R, G, B, X, Y, Z, L, a, b;
  96. R = ( double )imgrgb.get( x, y, 0 ) / 255.0;
  97. G = ( double )imgrgb.get( x, y, 1 ) / 255.0;
  98. B = ( double )imgrgb.get( x, y, 2 ) / 255.0;
  99. ColorConversion::ccRGBtoXYZ( R, G, B, &X, &Y, &Z, 0 );
  100. ColorConversion::ccXYZtoCIE_Lab( X, Y, Z, &L, &a, &b, 0 );
  101. imglab.set( x, y, L, 0 );
  102. imglab.set( x, y, a, 1 );
  103. imglab.set( x, y, b, 2 );
  104. }
  105. }
  106. return imglab;
  107. }
  108. NICE::MultiChannelImageT<double> ColorSpace::labtorgb( const NICE::MultiChannelImageT<double> &imglab )
  109. {
  110. NICE::MultiChannelImageT<double> imgrgb( imglab.width(), imglab.height(), 3 );
  111. //preprocessing RGB to XYZ
  112. for ( int x = 0;x < imglab.width();x++ )
  113. {
  114. for ( int y = 0;y < imglab.height();y++ )
  115. {
  116. double R, G, B, X, Y, Z, L, a, b;
  117. L = imglab.get( x, y, 0 );
  118. a = imglab.get( x, y, 1 );
  119. b = imglab.get( x, y, 2 );
  120. ColorConversion::ccCIE_LabtoXYZ( L, a, b, &X, &Y, &Z, 0 );
  121. ColorConversion::ccXYZtoRGB( X, Y, Z, &R, &G, &B, 0 );
  122. imgrgb.set( x, y, round( R*255.0 ), 0 );
  123. imgrgb.set( x, y, round( G*255.0 ), 1 );
  124. imgrgb.set( x, y, round( B*255.0 ), 2 );
  125. }
  126. }
  127. return imgrgb;
  128. }
  129. NICE::MultiChannelImageT<double> ColorSpace::rgbtolms( const NICE::MultiChannelImageT<double> &imgrgb )
  130. {
  131. NICE::MultiChannelImageT<double> imglms = NICE::MultiChannelImageT<double>( imgrgb.width(), imgrgb.height() );
  132. //preprocessing RGB to XYZ
  133. for ( int x = 0;x < imgrgb.width();x++ )
  134. {
  135. for ( int y = 0;y < imgrgb.height();y++ )
  136. {
  137. double R, G, B, X, Y, Z, L, M, S;
  138. R = imgrgb.get( x, y, 0 ) / 255.0;
  139. G = imgrgb.get( x, y, 1 ) / 255.0;
  140. B = imgrgb.get( x, y, 2 ) / 255.0;
  141. ColorConversion::ccRGBtoXYZ( R, G, B, &X, &Y, &Z, 0 );
  142. ColorConversion::ccXYZtoLMS( X, Y, Z, &L, &M, &S );
  143. imglms.set( x, y, L, 0 );
  144. imglms.set( x, y, M, 1 );
  145. imglms.set( x, y, S, 2 );
  146. }
  147. }
  148. return imglms;
  149. }