本文整理汇总了C++中range_t::clamp方法的典型用法代码示例。如果您正苦于以下问题:C++ range_t::clamp方法的具体用法?C++ range_t::clamp怎么用?C++ range_t::clamp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类range_t
的用法示例。
在下文中一共展示了range_t::clamp方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: additive_noise
bool additive_noise(
const range_t<tscalar>& noise_range,
const gauss_kernel_t<tscalar>& kernel,
const range_t<tscalar>& output_range,
tmatrix& src, tgetter getter, tsetter setter)
{
random_t<tscalar> noiser(noise_range.min(), noise_range.max());
// create random noise map
typename tensor::matrix_types_t<tscalar>::tmatrix noisemap(src.rows(), src.cols());
tensor::transform(noisemap, noisemap, [&] (tvalue) { return noiser(); });
// smooth the noise map
inplace_separable_filter(kernel, range_t<tscalar>(noiser.min(), noiser.max()), noisemap,
[] (tscalar v) { return v; },
[] (tscalar, tscalar v) { return v; });
// add the noise map to the input matrix
tensor::transform(src, noisemap, src, [&] (tvalue value, tscalar noise)
{
return setter(value, math::cast<tvalue>(output_range.clamp(noise + getter(value))));
});
// OK
return true;
}
示例2: inplace_separable_filter
bool inplace_separable_filter(const tkernel& kernel, const range_t<tscalar>& range,
tmatrix& src, tgetter getter, tsetter setter)
{
const int rows = static_cast<int>(src.rows());
const int cols = static_cast<int>(src.cols());
const int ksize = static_cast<int>(kernel.size());
const int krad = ksize / 2;
if (ksize != (2 * krad + 1))
{
return false;
}
std::vector<tscalar> buff(std::max(rows, cols));
// horizontal filter
for (int r = 0; r < rows; r ++)
{
for (int c = 0; c < cols; c ++)
{
buff[c] = math::cast<tscalar>(getter(src(r, c)));
}
for (int c = 0; c < cols; c ++)
{
tscalar v = 0;
for (int k = -krad; k <= krad; k ++)
{
const int cc = math::clamp(k + c, 0, cols - 1);
v += kernel[k + krad] * buff[cc];
}
src(r, c) = setter(src(r, c), math::cast<tvalue>(range.clamp(v)));
}
}
// vertical filter
for (int c = 0; c < cols; c ++)
{
for (int r = 0; r < rows; r ++)
{
buff[r] = math::cast<tscalar>(getter(src(r, c)));
}
for (int r = 0; r < rows; r ++)
{
tscalar v = 0;
for (int k = -krad; k <= krad; k ++)
{
const int rr = math::clamp(k + r, 0, rows - 1);
v += kernel[k + krad] * buff[rr];
}
src(r, c) = setter(src(r, c), math::cast<tvalue>(range.clamp(v)));
}
}
// OK
return true;
}