📄 convolve.h
字号:
typename blitz::promote_trait<TB,TC>::T_promote operator()( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& C, const blitz::TinyVector<int,2>& i) const {// {{{ using namespace blitz; typedef typename blitz::promote_trait<TB,TC>::T_promote TA; Range all = Range::all(); Array<TA,1> tmp (C.lbound(),C.extent()); const int Bl = B.lbound(1), Bh = B.ubound(1); const int Cl = C.lbound(0), Ch = C.ubound(0); tmp =0; int tj=0; for (int j=i[1]-Ch, k=Ch; j <= i[1]-Cl; ++j, --k) { if (j<Bl) tj = Bl; else if (j>Bh) tj = Bh; else tj =j; tmp(k) = (*this)( B(all,tj), C, i[0] ); } return sum(tmp*C); }// }}} ///@brief Convolve 2D array with 1D array at a point. /** * Use CX along X-axis and CY along y-axis for convolving. */ template<class TB, class TC> inline typename blitz::promote_trait<TB,TC>::T_promote operator()( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& CX, const blitz::Array<TC,1>& CY, const blitz::TinyVector<int,2>& i) const {// {{{ using namespace blitz; typedef typename blitz::promote_trait<TB,TC>::T_promote TA; Range all = Range::all(); Array<TA,1> tmp (CY.lbound(),CY.extent()); const int Bl = B.lbound(1), Bh = B.ubound(1); const int Cl = CY.lbound(0), Ch = CY.ubound(0); tmp =0; int tj=0; for (int j=i[1]-Ch, k=Ch; j <= i[1]-Cl; ++j, --k) { if (j<Bl) tj = Bl; else if (j>Bh) tj = Bh; else tj =j; tmp(k) = (*this)( B(all,tj), CX, i[0] ); } return sum(tmp*CY); }// }}}};//}}}// {{{ CPolicy_Loop/** * @defgroup overloaded convolveLoopExact functions * @brief Convolution operationeds limited to array boundaries only * (for example Quite handy for convolving images) * Array is Looped accross boundaries. * * Define convolution of two arrays. * Array B and C are convolved with result in A. * Array B is Looped accross boundaries. Return array has same size as array B. * * @param Array B (input Array) * @param Array C (convolution kernel) * @param Array A (convolution result) * * @warning Array C is assumed to be centered around 0. Also, size of B must be * greater than half the size of C. */struct CPolicy_Loop { /// @brief 1D convolution. template<class TB, class TC> inline blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 1> operator()( const blitz::Array<TB,1>& B, const blitz::Array<TC,1>& C, blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote,1>& A ) const {// {{{ using namespace blitz; typedef typename blitz::promote_trait<TB,TC>::T_promote TA; const int Bl = B.lbound(0), Bh = B.ubound(0); const int Cl = C.lbound(0), Ch = C.ubound(0); for (int i=Bl; i <= Bh; ++i) { TA result = 0; int tj=0; for (int j=i-Ch; j <= i-Cl; ++j) { if (j<Bl) tj = Bh + j +1; else if (j>Bh) tj = Bl - Bh + j -1; else tj =j; result += B(tj) * C(i-j); } A(i) = result; } return A; }// }}} /// @brief 1D convolution at a point. template<class TB, class TC> inline typename blitz::promote_trait<TB,TC>::T_promote operator()( const blitz::Array<TB,1>& B, const blitz::Array<TC,1>& C, const int i) const {// {{{ using namespace blitz; typedef typename blitz::promote_trait<TB,TC>::T_promote TA; const int Bl = B.lbound(0), Bh = B.ubound(0); const int Cl = C.lbound(0), Ch = C.ubound(0); TA result = 0; int tj=0; for (int j=i-Ch; j <= i-Cl; ++j) { if (j<Bl) tj = Bh + j +1; else if (j>Bh) tj = Bl - Bh + j -1; else tj =j; result += B(tj) * C(i-j); } return result; }// }}} /// @brief Convolve 2D array with 1D array at a point. template<class TB, class TC> inline typename blitz::promote_trait<TB,TC>::T_promote operator() ( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& C, const blitz::TinyVector<int,2>& i) const {// {{{ using namespace blitz; typedef typename blitz::promote_trait<TB,TC>::T_promote TA; Range all = Range::all(); Array<TA,1> tmp (C.lbound(),C.extent()); const int Bl = B.lbound(1), Bh = B.ubound(1); const int Cl = C.lbound(0), Ch = C.ubound(0); tmp =0; int tj=0; for (int j=i[1]-Ch, k=Ch; j <= i[1]-Cl; ++j, --k) { if (j<Bl) tj = Bh + j +1; else if (j>Bh) tj = Bl - Bh + j -1; else tj =j; tmp(k) = (*this)( B(all,tj), C, i[0] ); } return sum(tmp*C); }// }}} /// @brief Convolve 2D array with 1D array at a point. template<class TB, class TC> inline typename blitz::promote_trait<TB,TC>::T_promote operator() ( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& CX, const blitz::Array<TC,1>& CY, const blitz::TinyVector<int,2>& i) const {// {{{ using namespace blitz; typedef typename blitz::promote_trait<TB,TC>::T_promote TA; Range all = Range::all(); Array<TA,1> tmp (CY.lbound(),CY.extent()); const int Bl = B.lbound(1), Bh = B.ubound(1); const int Cl = CY.lbound(0), Ch = CY.ubound(0); tmp =0; int tj=0; for (int j=i[1]-Ch, k=Ch; j <= i[1]-Cl; ++j, --k) { if (j<Bl) tj = Bh + j +1; else if (j>Bh) tj = Bl - Bh + j -1; else tj =j; tmp(k) = (*this)( B(all,tj), CX, i[0] ); } return sum(tmp*CY); }// }}}};// }}}template<class Policy>struct ConvolveExact { /** @brief 1D convolution. */ template<class TB, class TC> inline blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 1> operator()( const blitz::Array<TB,1>& B, const blitz::Array<TC,1>& C, blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote,1>& A) const { Policy method; return method(B,C,A); } /** * @brief Convolve 2D array with 1D array at a point. */ template<class TB, class TC, int N> inline typename blitz::promote_trait<TB,TC>::T_promote operator()( const blitz::Array<TB,N>& B, const blitz::Array<TC,1>& C, const blitz::TinyVector<int,N>& i) const { Policy method; return method(B,C,i); } template<class TB, class TC, int N> inline typename blitz::promote_trait<TB,TC>::T_promote operator()( const blitz::Array<TB,N>& B, const blitz::Array<TC,1>& CX, const blitz::Array<TC,1>& CY, const blitz::TinyVector<int,N>& i) const { Policy method; return method(B,CX,CY,i); } /** * @brief Convolve 2D array with 1D array along X-axis. */ template<class TB, class TC> inline blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 2>& dim1( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& C, blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 2>& A) const {// {{{ using namespace blitz; Range all = Range::all(); for (int i=B.lbound(1); i <= B.ubound(1); ++i) { Array<typename promote_trait<TB,TC>::T_promote, 1> tmp = A(all,i); (*this)( B(all,i), C,tmp); } return A; }// }}} /** * @brief Convolve 2D array with 1D array along X-axis. * Overloaded function. Creates new array for result. */ template<class TB, class TC> inline blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 2> dim1( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& C) const {// {{{ typedef typename blitz::promote_trait<TB,TC>::T_promote TA; blitz::Array<TA,2> A (B.lbound(),B.extent()) ; return dim1(B,C,A); }// }}} /** * @brief Convolve 2D array with 1D array along X-axis at a point. */ template<class TB, class TC> inline typename blitz::promote_trait<TB,TC>::T_promote dim1( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& C, const blitz::TinyVector<int,2>& i) const {// {{{ blitz::Range all = blitz::Range::all(); return (*this)( B(all,i[1]), C, i[0]); }// }}} /** * @brief Convolve 2D array with 1D array along Y-axis. */ template<class TB, class TC> inline blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 2>& dim2( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& C, blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 2>& A) const {// {{{ using namespace blitz; Range all = Range::all(); for (int i=B.lbound(0); i <= B.ubound(0); ++i) { Array<typename promote_trait<TB,TC>::T_promote, 1> tmp = A(i,all); (*this)( B(i,all), C, tmp); } return A; }// }}} /** * @brief Convolve 2D array with 1D array along Y-axis. * Overloaded function. Creates new array for result. */ template<class TB, class TC> inline blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 2> dim2( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& C) const {// {{{ typedef typename blitz::promote_trait<TB,TC>::T_promote TA; blitz::Array<TA,2> A (B.lbound(),B.extent()) ; return (*this)(B,C,A); }// }}} /** * @brief Convolve 2D array with 1D array along Y-axis at a point. */ template<class TB, class TC> inline typename blitz::promote_trait<TB,TC>::T_promote dim2( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& C, const blitz::TinyVector<int,2>& i) {// {{{ blitz::Range all = blitz::Range::all(); return (*this)( B(i[0],all), C, i[1]); }// }}} /** * @brief Convolve 2D array with 1D array. */ template<class TB, class TC> inline blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 2>& operator()( const blitz::Array<TB,2>& B, const blitz::Array<TC,1>& C, blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 2>& A) const {// {{{ typedef typename blitz::promote_trait<TB,TC>::T_promote TA; blitz::Array<TA,2> tmp (B.lbound(),B.extent()) ; dim1(B, C, tmp ); dim2(tmp, C, A ); return A; }// }}} /** * @brief Convolve 3D array with 1D array. */ template<class TB, class TC> inline blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 3>& operator()( const blitz::Array<TB,3>& B, const blitz::Array<TC,1>& C, blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 3>& A) const {// {{{ using namespace blitz; typedef typename blitz::promote_trait<TB,TC>::T_promote TA; blitz::Array<TA,3> tmp (B.lbound(),B.extent()) ; Range all = Range::all(); Array<TA,3> tmp3D (B.lbound(), B.extent()); { Array<TA,2> tmp ( TinyVector<int,2> (B.lbound(1), B.lbound(2)), TinyVector<int,2> (B.extent(1), B.extent(2)) ); for (int i=B.lbound(0); i <= B.ubound(0); ++i)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -