/** handy method to display a raster * * @param v vector of float * @returns vector of uint8_t (unisgned char) * * distribute as: * min(v) -> 0 * max(v) -> 255 */ inline bytes_t raster2bytes(const raster& v) { auto minmax = std::minmax_element(v.begin(), v.end()); float min = *minmax.first; float max = *minmax.second; float diff = max - min; bytes_t b(v.size()); if (diff == 0) // max == min (useless band) return b; float coef = 255.0 / diff; std::transform(v.begin(), v.end(), b.begin(), // C++11 lambda, capture local varibles by reference [&] [&](float f) -> uint8_t { return std::floor( coef * (f - min) ); }); return b; }
/** * normalize [0, 1.0] in place */ inline raster normalize(raster& v) { auto minmax = std::minmax_element(v.begin(), v.end()); float min = *minmax.first; float max = *minmax.second; float diff = max - min; if (diff == 0) // max == min return v; // normalize in place for (auto& f : v) f = (f - min) / diff; return v; }