Guest User

Untitled

a guest
Jun 11th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. template<typename MatType, typename CubeType>
  2. CubeType StructuredForests<MatType, CubeType>::
  3. RGB2LUV(CubeType const &InImage)
  4. {
  5.   //assert type is double or float.
  6.   double a, y0, maxi;
  7.   a = std::pow(29.0, 3) / 27.0;
  8.   y0 = 8.0 / a;
  9.   maxi = 1.0 / 270.0;
  10.  
  11.   arma::vec table(1064);
  12.   for (size_t i = 0; i <= 1024; ++i)
  13.   {
  14.     table(i) = i / 1024.0;
  15.    
  16.     if (table(i) > y0)
  17.       table(i) = 116 * pow(table(i), 1.0/3.0) - 16.0;
  18.     else
  19.       table(i) = table(i) * a;
  20.  
  21.     table(i) = table(i) * maxi;
  22.   }
  23.   for(size_t i = 1025; i < table.n_elem; ++i)
  24.   {
  25.     table(i) = table(i - 1);
  26.   }
  27.  
  28.   MatType rgb2xyz;
  29.   rgb2xyz << 0.430574 << 0.222015 << 0.020183 << arma::endr
  30.           << 0.341550 << 0.706655 << 0.129553 << arma::endr
  31.           << 0.178325 << 0.071330 << 0.939180;
  32.  
  33.   //see how to calculate this efficiently. numpy.dot does this.
  34.   CubeType xyz(InImage.n_rows, InImage.n_cols, rgb2xyz.n_cols);
  35.   /*
  36.   for(size_t i = 0; i < InImage.n_rows; ++i)
  37.   {
  38.     for(size_t j = 0; j < InImage.n_cols; ++j)
  39.     {
  40.       for(size_t k = 0; k < rgb2xyz.n_cols; ++k)
  41.       {
  42.         double val = 0;
  43.         for(size_t l = 0; l < InImage.n_slices; ++l)
  44.         {
  45.           val = val + InImage(i, j, l) *\
  46.               rgb2xyz(l, k);
  47.         }
  48.         xyz(i, j, k) = val;
  49.       }
  50.     }
  51.   }
  52.   */
  53.   for (size_t i = 0; i < InImage.slice(0).n_elem; ++i)
  54.   {
  55.     double r = InImage.slice(0)(i);
  56.     double g = InImage.slice(1)(i);
  57.     double b = InImage.slice(2)(i);
  58.  
  59.     xyz.slice(0)(i) = 0.430574 * r + 0.341550 * g + 0.178325 * b;
  60.     xyz.slice(1)(i) = 0.222015 * r + 0.706655 * g + 0.129553 * b;
  61.     xyz.slice(2)(i) = 0.020183 * r + 0.071330 * g + 0.939180 * b;
  62.   }
  63.  
  64.   std::cout << "printing xyz" << std::endl;
  65.   xyz.print();
  66.   MatType nz(InImage.n_rows, InImage.n_cols);
  67.  
  68.   nz = 1.0 / ( xyz.slice(0) + (15 * xyz.slice(1) ) +
  69.        (3 * xyz.slice(2) + 1e-35));
  70.   std::cout << arma::size(nz) << std::endl;
  71.   nz.print();
  72.   CubeType OutImage(InImage.n_rows, InImage.n_cols, InImage.n_slices);
  73.  
  74.   for(size_t j = 0; j < xyz.n_cols; ++j)
  75.   {
  76.     for(size_t i = 0; i < xyz.n_rows; ++i)
  77.     {
  78.       OutImage(i, j, 0) = table( static_cast<size_t>( (1024 * xyz(i, j, 1) ) ) );
  79.     }
  80.   }
  81.  
  82.   OutImage.slice(1) = OutImage.slice(0) % (13 * 4 * (xyz.slice(0) % nz) \
  83.                         - 13 * 0.197833) + 88 * maxi;
  84.   OutImage.slice(2) = OutImage.slice(0) % (13 * 9 * (xyz.slice(1) % nz) \
  85.                         - 13 * 0.468331) + 134 * maxi;
  86.  
  87.   return OutImage;
  88. }
Add Comment
Please, Sign In to add comment