Advertisement
Guest User

Untitled

a guest
Oct 21st, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include "export_filters.hpp"
  2.  
  3. namespace{
  4.  
  5. arma::mat const bilinearInterpolation(arma::mat const &src,
  6.                                       size_t height, size_t width)
  7. {
  8.     arma::mat dst(height, width);
  9.     double const x_ratio = static_cast<double>((src.n_cols - 1)) / width;
  10.     double const y_ratio = static_cast<double>((src.n_rows - 1)) / height;
  11.     for(size_t row = 0; row != dst.n_rows; ++row)
  12.     {
  13.         size_t y = static_cast<size_t>(row * y_ratio);
  14.         double const y_diff = (row * y_ratio) - y; //distance of the nearest pixel(y axis)
  15.         double const y_diff_2 = 1 - y_diff;
  16.         for(size_t col = 0; col != dst.n_cols; ++col)
  17.         {
  18.             size_t x = static_cast<size_t>(col * x_ratio);
  19.             double const x_diff = (col * x_ratio) - x; //distance of the nearet pixel(x axis)
  20.             double const x_diff_2 = 1 - x_diff;
  21.             double const y2_cross_x2 = y_diff_2 * x_diff_2;
  22.             double const y2_cross_x = y_diff_2 * x_diff;
  23.             double const y_cross_x2 = y_diff * x_diff_2;
  24.             double const y_cross_x = y_diff * x_diff;
  25.             dst(row, col) = y2_cross_x2 * src(y, x) +
  26.                     y2_cross_x * src(y, x + 1) +
  27.                     y_cross_x2 * src(y + 1, x) +
  28.                     y_cross_x * src(y + 1, x + 1);
  29.         }
  30.     }
  31.  
  32.     return dst;
  33. }
  34.  
  35. void copyToExportFilter(arma::mat const &input, arma::mat &output)
  36. {
  37.     int index = 0;
  38.     for(int col = 0; col != output.n_cols; ++col){
  39.         for(int row = 0; row != output.n_rows; ++row){
  40.             output(row, col) = input(index++, 0);
  41.         }
  42.     }
  43. }
  44.  
  45. }
  46.  
  47. arma::mat exportFiltersToPGMGrid(std::string const &fileName, arma::mat const &input,
  48.                                  size_t height, size_t width)
  49. {
  50.     arma::mat inputTemp(input);
  51.     double const mean = arma::mean(arma::mean(inputTemp));
  52.     inputTemp -= mean;
  53.  
  54.     int rows = 0, cols = (int)std::ceil(std::sqrt(inputTemp.n_cols));
  55.     if(std::pow(std::floor(std::sqrt(inputTemp.n_cols)), 2) != inputTemp.n_cols){
  56.         while(inputTemp.n_cols % cols != 0 && cols < 1.2*std::sqrt(inputTemp.n_cols)){
  57.             ++cols;
  58.         }
  59.         rows = (int)std::ceil(inputTemp.n_cols/cols);
  60.     }else{
  61.         cols = (int)std::sqrt(inputTemp.n_cols);
  62.         rows = cols;
  63.     }
  64.  
  65.     int const SquareRows = (int)std::sqrt(inputTemp.n_rows);
  66.     int const Buf = 1;
  67.  
  68.     int const Offset = SquareRows+Buf;
  69.     arma::mat array;
  70.     array.ones(Buf+rows*(Offset),
  71.                Buf+cols*(Offset));
  72.  
  73.     int k = 0;
  74.     for(int i = 0; i != rows; ++i){
  75.         for(int j = 0; j != cols; ++j){
  76.             if(k >= inputTemp.n_cols){
  77.                 continue;
  78.             }
  79.             arma::mat reshapeMat(SquareRows, SquareRows);
  80.             copyToExportFilter(inputTemp.col(k), reshapeMat);
  81.             double const max = arma::abs(inputTemp.col(k)).max();
  82.             if(max != 0.0){
  83.                 reshapeMat /= max;
  84.             }
  85.             array.submat(i*(Offset), j*(Offset),
  86.                          i*(Offset) + SquareRows - 1,
  87.                          j*(Offset) + SquareRows - 1) =
  88.                     reshapeMat;
  89.             ++k;
  90.         }
  91.     }
  92.  
  93.     double const max = array.max();
  94.     double const min = array.min();
  95.     if((max - min) != 0){
  96.         array = (array - min) / (max - min) * 255;
  97.     }
  98.  
  99.     arma::mat result =
  100.             bilinearInterpolation(array, rows * height, cols * width);
  101.     result.save(fileName, arma::pgm_binary);
  102.  
  103.     return result;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement