ColorSpace.cpp 5.1 KB

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