Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. TiffImage NDWICalculator::generate_ndwi_layer_green_nir_high_performance(
  2.   const TiffImage &green_layer, const TiffImage &nir_layer, unsigned int cores) {
  3.   std::vector<std::thread> thread_pool;
  4.   TiffImage result(green_layer.width(), green_layer.height(), 1, 1);
  5.  
  6.   for (unsigned int i = 0UL; i < cores; ++i) {
  7.     thread_pool.emplace_back(
  8.       std::thread([i, cores, &result, &green_layer, &nir_layer]() {
  9.         unsigned int height = green_layer.height() / cores;
  10.         unsigned int start = i > 0 ? height * i : height * i + 1;
  11.         unsigned int stop = i < cores - 1 ? height * (i + 1) + 1 : (height * (i + 1));
  12.  
  13.         for (unsigned int y = start; y < stop - 1; ++y) {
  14.           for (unsigned int x = 0; x < static_cast<unsigned int>(green_layer.width()); ++x) {
  15.             float green_value = green_layer(x, y);
  16.             float nir_value = nir_layer(x, y);
  17.  
  18.             if (green_value > 1.f && nir_value > 1.f) {
  19.               float ndwi_level = (green_value - nir_value) / (green_value + nir_value);
  20.               result(x, y) = static_cast<unsigned char>(ndwi_level >= 0.33f ? 255 : 0);
  21.             }
  22.           }
  23.         }
  24.       }));
  25.   }
  26.  
  27.   for (auto &&thread : thread_pool) {
  28.     thread.join();
  29.   }
  30.   return result;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement