void nlfilter(image& img, std::size_t w, std::size_t h, std::function<image::pixel_t(const ublas::matrix<image::pixel_t>&)> f) { auto iw = img.width(); auto ih = img.height(); // The number of rows above/aside the center element auto top = static_cast<std::size_t>(std::floor(h / 2.0)); auto left = static_cast<std::size_t>(std::floor(w / 2.0)); // We must store the result and set the image to it after we have processed // the image as subsequent regions may need to use the unchanged pixel at // a location we have already modified. ublas::matrix<image::pixel_t> result(iw, ih); for(decltype(iw) x = 0; x < iw; ++x) { for(decltype(ih) y = 0; y < ih; ++y) { // img.region works from the top left of the region so we must // offset it so that (x, y) is at the center. result(x, y) = f(img.region(x - left, y - top, w, h)); } } img.set(0, 0, result); }