diff --git a/dlib/matrix/matrix_utilities.h b/dlib/matrix/matrix_utilities.h index 7aabf0c0b..63c579294 100644 --- a/dlib/matrix/matrix_utilities.h +++ b/dlib/matrix/matrix_utilities.h @@ -2872,6 +2872,80 @@ namespace dlib return matrix_op(op(m.ref(),lower, upper)); } +// ---------------------------------------------------------------------------------------- + + template + struct op_lowerbound : basic_op_m + { + typedef typename M::type type; + + op_lowerbound( const M& m_, const type& thresh_) : + basic_op_m(m_), thresh(thresh_){} + + const type& thresh; + + typedef const typename M::type const_ret_type; + const static long cost = M::cost + 2; + + const_ret_type apply ( long r, long c) const + { + const type temp = this->m(r,c); + if (temp >= thresh) + return temp; + else + return thresh; + } + }; + + template < + typename EXP + > + const matrix_op > lowerbound ( + const matrix_exp& m, + const typename EXP::type& thresh + ) + { + typedef op_lowerbound op; + return matrix_op(op(m.ref(), thresh)); + } + +// ---------------------------------------------------------------------------------------- + + template + struct op_upperbound : basic_op_m + { + typedef typename M::type type; + + op_upperbound( const M& m_, const type& thresh_) : + basic_op_m(m_), thresh(thresh_){} + + const type& thresh; + + typedef const typename M::type const_ret_type; + const static long cost = M::cost + 2; + + const_ret_type apply ( long r, long c) const + { + const type temp = this->m(r,c); + if (temp <= thresh) + return temp; + else + return thresh; + } + }; + + template < + typename EXP + > + const matrix_op > upperbound ( + const matrix_exp& m, + const typename EXP::type& thresh + ) + { + typedef op_upperbound op; + return matrix_op(op(m.ref(), thresh)); + } + // ---------------------------------------------------------------------------------------- template diff --git a/dlib/matrix/matrix_utilities_abstract.h b/dlib/matrix/matrix_utilities_abstract.h index 596d6c98d..b04d46601 100644 --- a/dlib/matrix/matrix_utilities_abstract.h +++ b/dlib/matrix/matrix_utilities_abstract.h @@ -1707,6 +1707,42 @@ namespace dlib - R(r,c) == m(r,c) !*/ +// ---------------------------------------------------------------------------------------- + + const matrix_exp lowerbound ( + const matrix_exp& m, + const matrix_exp::type& thresh + ); + /*! + ensures + - returns a matrix R such that: + - R::type == the same type that was in m + - R has the same dimensions as m + - for all valid r and c: + - if (m(r,c) >= thresh) then + - R(r,c) == m(r,c) + - else + - R(r,c) == thresh + !*/ + +// ---------------------------------------------------------------------------------------- + + const matrix_exp upperbound ( + const matrix_exp& m, + const matrix_exp::type& thresh + ); + /*! + ensures + - returns a matrix R such that: + - R::type == the same type that was in m + - R has the same dimensions as m + - for all valid r and c: + - if (m(r,c) <= thresh) then + - R(r,c) == m(r,c) + - else + - R(r,c) == thresh + !*/ + // ---------------------------------------------------------------------------------------- } diff --git a/dlib/test/matrix4.cpp b/dlib/test/matrix4.cpp index 64f3fe5a6..5bbe6ee17 100644 --- a/dlib/test/matrix4.cpp +++ b/dlib/test/matrix4.cpp @@ -607,6 +607,31 @@ namespace DLIB_TEST(equal(0.0/m , zeros_matrix(3,4))); } } + + { + matrix m(2,3); + m = 1,2,3, + 4,5,6; + matrix M(2,3); + M = m; + + DLIB_TEST(upperbound(m,6) == M); + DLIB_TEST(upperbound(m,60) == M); + DLIB_TEST(lowerbound(m,-2) == M); + DLIB_TEST(lowerbound(m,0) == M); + + M = 2,2,3, + 4,5,6; + DLIB_TEST(lowerbound(m,2) == M); + + M = 0,0,0, + 0,0,0; + DLIB_TEST(upperbound(m,0) == M); + + M = 1,2,3, + 3,3,3; + DLIB_TEST(upperbound(m,3) == M); + } }