np::ndarray cleanVOC( const np::ndarray& lbl ) { unsigned char * plbl = (unsigned char *)lbl.get_data(); np::ndarray r = np::zeros( lbl.get_nd(), lbl.get_shape(), np::dtype::get_builtin<short>() ); short * pr = (short *)r.get_data(); for( int i=0; i<lbl.shape(0)*lbl.shape(1); i++ ) pr[i] = (plbl[i]>250)?-2:(short)plbl[i]-1; return r; }
/**************** other ****************/ static np::ndarray extractPatch( const np::ndarray &a, const RMatrixXi & l, const VectorXi & s ) { // Check the input if( !(a.get_flags() & np::ndarray::C_CONTIGUOUS) ) throw std::invalid_argument( "Contiguous array required!" ); if( l.cols() != a.get_nd() ) throw std::invalid_argument("location dimension does not match matrix"); if( l.cols() != s.size() ) throw std::invalid_argument("patch size and locations have different dimension"); for( int i=0; i<l.rows(); i++ ) for( int j=0; j<s.size(); j++ ) if( l(i,j)<0 || l(i,j)+s[j] > a.shape(j) ) throw std::invalid_argument("location out of range! Dimension "+std::to_string(j)+" : "+std::to_string(l(i,j))+" + "+std::to_string(s[j])+" ( 0 .. "+std::to_string(a.shape(j))+" )"); const int itemsize = a.get_dtype().get_itemsize(); // Create the output array std::vector<Py_intptr_t> out_size; out_size.push_back( l.rows() ); for( int i=0; i<s.size(); i++ ) if( s[i]>1 ) out_size.push_back( s[i] ); np::ndarray res = np::empty( out_size.size(), out_size.data(), a.get_dtype() ); // Prepare the patches const int ls = s[s.size()-1]; // Radius of the last dimension std::vector<int> patch_offsets(1,0); for( int d=0; d<s.size()-1; d++ ) { std::vector<int> opo = patch_offsets; patch_offsets.clear(); for( int i=0; i<s[d]; i++ ) for( int o: opo ) patch_offsets.push_back( a.shape(d+1)*(o+i) ); } const char * pa = a.get_data(); char * pres = res.get_data(); for( int i=0,k=0; i<l.rows(); i++ ) { int o=0; for( int d=0; d<s.size(); d++ ) o = a.shape(d)*o + l(i,d); // Extract the patches for( int po: patch_offsets ) { memcpy( pres+k*itemsize, pa+(po+o)*itemsize, ls*itemsize ); k += ls; } } return res; }
vcl::tools::shared_ptr<vcl::matrix<SCALARTYPE, F> > matrix_init_ndarray(const np::ndarray& array, const vcl::context& ctx) { int d = array.get_nd(); if (d != 2) { PyErr_SetString(PyExc_TypeError, "Can only create a matrix from a 2-D array!"); bp::throw_error_already_set(); } ndarray_wrapper<SCALARTYPE> wrapper(array); vcl::matrix<SCALARTYPE, F>* mat = new vcl::matrix<SCALARTYPE, F>(wrapper.size1(), wrapper.size2(), ctx); vcl::copy(wrapper, (*mat)); return vcl::tools::shared_ptr<vcl::matrix<SCALARTYPE, F> >(mat); }
vcl::tools::shared_ptr<std::vector<SCALARTYPE> > std_vector_init_ndarray(const np::ndarray& array) { int d = array.get_nd(); if (d != 1) { PyErr_SetString(PyExc_TypeError, "Can only create a vector from a 1-D array!"); bp::throw_error_already_set(); } vcl::vcl_size_t s = (vcl::vcl_size_t) array.shape(0); std::vector<SCALARTYPE> *v = new std::vector<SCALARTYPE>(s); for (vcl::vcl_size_t i=0; i < s; ++i) (*v)[i] = bp::extract<SCALARTYPE>(array[i]); return vcl::tools::shared_ptr<std::vector<SCALARTYPE> >(v); }
vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> > vcl_vector_init_ndarray(const np::ndarray& array, const vcl::context& ctx) { int d = array.get_nd(); if (d != 1) { PyErr_SetString(PyExc_TypeError, "Can only create a vector from a 1-D array!"); bp::throw_error_already_set(); } vcl::vcl_size_t s = (vcl::vcl_size_t) array.shape(0); std::vector<SCALARTYPE> cpu_vector(s); vcl::vector<SCALARTYPE> *v = new vcl::vector<SCALARTYPE>(s, ctx); for (vcl::vcl_size_t i=0; i < s; ++i) cpu_vector[i] = bp::extract<SCALARTYPE>(array[i]); vcl::copy(cpu_vector.begin(), cpu_vector.end(), v->begin()); return vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >(v); }