Packages
evision
1.0.1-rc.0
1.0.1-rc.0
1.0.0
1.0.0-rc.0
0.2.17
0.2.17-rc2
0.2.17-rc1
0.2.16
0.2.16-pre.2
0.2.16-pre
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.2-rc2
0.2.1
0.2.0
0.1.39
0.1.38
0.1.37
0.1.36
0.1.35
0.1.34
0.1.33
0.1.32
retired
0.1.31
0.1.30
0.1.29
0.1.28
0.1.27
0.1.26
0.1.26-rc3
0.1.26-rc2
0.1.26-rc1
0.1.26-rc0
0.1.25
0.1.24
0.1.23
0.1.22
0.1.21
0.1.20
0.1.19
0.1.18
0.1.17
0.1.16
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
OpenCV-Erlang/Elixir binding.
Current section
Files
Jump to
Current section
Files
c_src/modules/evision_backend/unary_math.h
#ifndef EVISION_BACKEND_UNARY_MATH_H
#define EVISION_BACKEND_UNARY_MATH_H
#include <cmath>
#include <erl_nif.h>
#include "../../ArgInfo.hpp"
#include "../evision_mat_utils.hpp"
#include "parallel.h"
// Element-wise unary float math (Nx trig / hyperbolic / erf / etc.) that OpenCV
// has no primitive for. The caller passes a single-channel f32/f64 mat already
// cast to the output type; ops that produce floats from any input. Op codes match
// Evision.Backend's @uop_* constants.
template <typename T>
static void evision_unary_math(cv::Mat &m, int op) {
T *p = (T *)m.data;
size_t n = m.total();
evision_parallel_for((int64_t)n, (int64_t)n, EVISION_PARALLEL_SIMPLE_MIN_WORK, [&](int64_t begin, int64_t end) {
for (int64_t i = begin; i < end; i++) {
T x = p[(size_t)i];
switch (op) {
case 0: p[(size_t)i] = std::sin(x); break;
case 1: p[(size_t)i] = std::cos(x); break;
case 2: p[(size_t)i] = std::tan(x); break;
case 3: p[(size_t)i] = std::asin(x); break;
case 4: p[(size_t)i] = std::acos(x); break;
case 5: p[(size_t)i] = std::atan(x); break;
case 6: p[(size_t)i] = std::sinh(x); break;
case 7: p[(size_t)i] = std::cosh(x); break;
case 8: p[(size_t)i] = std::tanh(x); break;
case 9: p[(size_t)i] = std::asinh(x); break;
case 10: p[(size_t)i] = std::acosh(x); break;
case 11: p[(size_t)i] = std::atanh(x); break;
case 12: p[(size_t)i] = std::erf(x); break;
case 13: p[(size_t)i] = std::erfc(x); break;
case 14: p[(size_t)i] = std::cbrt(x); break;
case 15: p[(size_t)i] = std::log1p(x); break;
case 16: p[(size_t)i] = (T)1 / std::sqrt(x); break;
case 17: p[(size_t)i] = (T)1 / ((T)1 + std::exp(-x)); break;
default: break;
}
}
});
}
// @evision c: mat_unary_math, evision_cv_mat_unary_math, 1
// @evision nif: def mat_unary_math(_opts \\ []), do: :erlang.nif_error(:undefined)
static ERL_NIF_TERM evision_cv_mat_unary_math(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
using namespace cv;
using namespace evision::nif;
std::map<std::string, ERL_NIF_TERM> erl_terms;
int nif_opts_index = 0;
evision::nif::parse_arg(env, nif_opts_index, argv, erl_terms);
{
Mat src;
int op = 0;
if (evision_to_safe(env, evision_get_kw(env, erl_terms, "src"), src, ArgInfo("src", ArgInfo::INPUT_ONLY)) &&
evision_to_safe(env, evision_get_kw(env, erl_terms, "op"), op, ArgInfo("op", 0))) {
Mat dst = src.clone();
switch (dst.depth()) {
case CV_32F: evision_unary_math<float>(dst, op); break;
case CV_64F: evision_unary_math<double>(dst, op); break;
default: return evision::nif::error(env, "mat_unary_math: expected f32 or f64");
}
return evision_from(env, dst);
}
}
return enif_make_badarg(env);
}
#endif // EVISION_BACKEND_UNARY_MATH_H