ColorSpace.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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,true);
  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. /*
  36. //TODO test it
  37. int checkRange(int in,int min=0,int max=255)
  38. {
  39. int out=in;
  40. if(in<min)
  41. {
  42. out=min;
  43. }
  44. if(in>max)
  45. {
  46. out=max;
  47. }
  48. return out;
  49. }
  50. NICE::ColorImage ColorSpace::rgbtohsl(const NICE::ColorImage &imgrgb)
  51. {
  52. NICE::ColorImage imghsl (imgrgb.width(), imgrgb.height());
  53. for(int x=0;x<imgrgb.width();x++)
  54. {
  55. for(int y=0;y<imgrgb.height();y++)
  56. {
  57. double R,G,B,H,S,L;
  58. R=(double)imgrgb.getPixel(x,y,0)/255.0;
  59. G=(double)imgrgb.getPixel(x,y,1)/255.0;
  60. B=(double)imgrgb.getPixel(x,y,2)/255.0;
  61. ColorConversion::ccRGBtoHSL(R,G,B,&H,&S,&L);
  62. imghsl.setPixel(x,y,0,(int)round(H*255.0));
  63. imghsl.setPixel(x,y,1,(int)round(S*255.0));
  64. imghsl.setPixel(x,y,2,(int)round(L*255.0));
  65. }
  66. }
  67. return imghsl;
  68. }
  69. NICE::ColorImage ColorSpace::hsltorgb(const NICE::ColorImage &imghsl)
  70. {
  71. NICE::ColorImage imgrgb (imghsl.width(), imghsl.height());
  72. for(int x=0;x<imghsl.width();x++)
  73. {
  74. for(int y=0;y<imghsl.height();y++)
  75. {
  76. double R,G,B,H,S,L;
  77. H=(double)imghsl.getPixel(x,y,0)/255.0;
  78. S=(double)imghsl.getPixel(x,y,1)/255.0;
  79. L=(double)imghsl.getPixel(x,y,2)/255.0;
  80. ColorConversion::ccHSLtoRGB(H,S,L,&R,&G,&B);
  81. imgrgb.setPixel(x,y,0,(int)round(R*255.0));
  82. imgrgb.setPixel(x,y,1,(int)round(G*255.0));
  83. imgrgb.setPixel(x,y,2,(int)round(B*255.0));
  84. }
  85. }
  86. return imgrgb;
  87. }
  88. NICE::ColorImage ColorSpace::rgbtolab(const NICE::ColorImage &imgrgb)
  89. {
  90. NICE::ColorImage imglab (imgrgb.width(), imgrgb.height());
  91. //preprocessing RGB to XYZ
  92. for(int x=0;x<imgrgb.width();x++)
  93. {
  94. for(int y=0;y<imgrgb.height();y++)
  95. {
  96. double R,G,B,X,Y,Z,L,a,b;
  97. R=(double)imgrgb.getPixel(x,y,0)/255.0;
  98. G=(double)imgrgb.getPixel(x,y,1)/255.0;
  99. B=(double)imgrgb.getPixel(x,y,2)/255.0;
  100. ColorConversion::ccRGBtoXYZ(R,G,B,&X,&Y,&Z,0);
  101. ColorConversion::ccXYZtoCIE_Lab(X,Y,Z,&L,&a,&b,0);
  102. imglab.setPixel(x,y,0,(int)round(L*255.0));
  103. imglab.setPixel(x,y,1,(int)round(a*255.0));
  104. imglab.setPixel(x,y,2,(int)round(b*255.0));
  105. }
  106. }
  107. showImage ( *imglab.getChannel(0), "L" );
  108. return imglab;
  109. }
  110. NICE::ColorImage ColorSpace::labtorgb(const NICE::ColorImage &imglab)
  111. {
  112. NICE::ColorImage imgrgb (imglab.width(), imglab.height());
  113. //preprocessing RGB to XYZ
  114. for(int x=0;x<imglab.width();x++)
  115. {
  116. for(int y=0;y<imglab.height();y++)
  117. {
  118. double R,G,B,X,Y,Z,L,a,b;
  119. L=(double)imglab.getPixel(x,y,0)/255.0;
  120. a=(double)imglab.getPixel(x,y,1)/255.0;
  121. b=(double)imglab.getPixel(x,y,2)/255.0;
  122. ColorConversion::ccCIE_LabtoXYZ(L,a,b,&X,&Y,&Z,0);
  123. ColorConversion::ccXYZtoRGB(X,Y,Z,&R,&G,&B,0);
  124. imgrgb.setPixel(x,y,0,(int)round(R*255.0));
  125. imgrgb.setPixel(x,y,1,(int)round(G*255.0));
  126. imgrgb.setPixel(x,y,2,(int)round(B*255.0));
  127. }
  128. }
  129. return imgrgb;
  130. }
  131. NICE::ColorImage ColorSpace::rgbtolms(const NICE::ColorImage &imgrgb)
  132. {
  133. NICE::ColorImage imglms=NICE::ColorImage(imgrgb.width(),imgrgb.height());
  134. //preprocessing RGB to XYZ
  135. for(int x=0;x<imgrgb.width();x++)
  136. {
  137. for(int y=0;y<imgrgb.height();y++)
  138. {
  139. double R,G,B,X,Y,Z,L,M,S;
  140. R=(double)imgrgb.getPixelQuick(x,y,0)/255.0;
  141. G=(double)imgrgb.getPixelQuick(x,y,1)/255.0;
  142. B=(double)imgrgb.getPixelQuick(x,y,2)/255.0;
  143. ColorConversion::ccRGBtoXYZ(R,G,B,&X,&Y,&Z,0);
  144. ColorConversion::ccXYZtoLMS(X,Y,Z,&L,&M,&S);
  145. imglms.setPixelQuick(x,y,0,checkRange((int)round(L*255.0)));
  146. imglms.setPixelQuick(x,y,1,checkRange((int)round(M*255.0)));
  147. imglms.setPixelQuick(x,y,2,checkRange((int)round(S*255.0)));
  148. }
  149. }
  150. return imglms;
  151. }*/