LArOpenCV  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
Public Member Functions | Private Member Functions | List of all members
larcv::convert::NDArrayConverter Class Reference

#include <NDArrayConverter.h>

Public Member Functions

 NDArrayConverter ()
 
::cv::Mat toMat (const PyObject *o)
 
PyObjecttoNDArray (const ::cv::Mat &mat)
 
PyObjecttoNDArray (const ::larcv::Image2D &img)
 
PyObjecttoNDArray (const ::larcv::Point2DArray &pt_v)
 

Private Member Functions

void init ()
 

Detailed Description

Definition at line 65 of file NDArrayConverter.h.

Constructor & Destructor Documentation

larcv::convert::NDArrayConverter::NDArrayConverter ( )

Definition at line 200 of file NDArrayConverter.cxx.

References init().

200 { init(); }

Member Function Documentation

void larcv::convert::NDArrayConverter::init ( )
private

Definition at line 202 of file NDArrayConverter.cxx.

Referenced by NDArrayConverter().

203  {
204  import_array();
205  }
cv::Mat larcv::convert::NDArrayConverter::toMat ( const PyObject o)

Definition at line 207 of file NDArrayConverter.cxx.

References larcv::convert::failmsg(), larcv::convert::g_numpyAllocator, and larcv::convert::refcountFromPyObject().

208  {
209  ::cv::Mat m;
210 
211  if(!o || o == Py_None)
212  {
213  if( !m.data )
214  m.allocator = &g_numpyAllocator;
215  }
216 
217  if( !PyArray_Check(o) )
218  {
219  failmsg("toMat: Object is not a numpy array");
220  }
221 
222  int typenum = PyArray_TYPE(o);
223  int type = typenum == NPY_UBYTE ? CV_8U : typenum == NPY_BYTE ? CV_8S :
224  typenum == NPY_USHORT ? CV_16U : typenum == NPY_SHORT ? CV_16S :
225  typenum == NPY_INT || typenum == NPY_LONG ? CV_32S :
226  typenum == NPY_FLOAT ? CV_32F :
227  typenum == NPY_DOUBLE ? CV_64F : -1;
228 
229  if( type < 0 )
230  {
231  failmsg("toMat: Data type = %d is not supported", typenum);
232  }
233 
234  int ndims = PyArray_NDIM(o);
235 
236  if(ndims >= CV_MAX_DIM)
237  {
238  failmsg("toMat: Dimensionality (=%d) is too high", ndims);
239  }
240 
241  int size[CV_MAX_DIM+1];
242  size_t step[CV_MAX_DIM+1], elemsize = CV_ELEM_SIZE1(type);
243  const npy_intp* _sizes = PyArray_DIMS(o);
244  const npy_intp* _strides = PyArray_STRIDES(o);
245  bool transposed = false;
246 
247  for(int i = 0; i < ndims; i++)
248  {
249  size[i] = (int)_sizes[i];
250  step[i] = (size_t)_strides[i];
251  }
252 
253  if( ndims == 0 || step[ndims-1] > elemsize ) {
254  size[ndims] = 1;
255  step[ndims] = elemsize;
256  ndims++;
257  }
258 
259  if( ndims >= 2 && step[0] < step[1] )
260  {
261  std::swap(size[0], size[1]);
262  std::swap(step[0], step[1]);
263  transposed = true;
264  }
265 
266  // std::cerr << " ndims: " << ndims
267  // << " size: " << size
268  // << " type: " << type
269  // << " step: " << step
270  // << " size: " << size[2] << std::endl;
271 
272  // TODO: Possible bug in multi-dimensional matrices
273 #if 0
274  if( ndims == 3 && size[2] <= CV_CN_MAX && step[1] == elemsize*size[2] )
275  {
276  ndims--;
277  type |= CV_MAKETYPE(0, size[2]);
278  }
279 #endif
280 
281  if( ndims > 2)
282  {
283  failmsg("toMat: Object has more than 2 dimensions");
284  }
285 
286  m = ::cv::Mat(ndims, size, type, PyArray_DATA(o), step);
287  // m.u = g_numpyAllocator.allocate(o, ndims, size, type, step);
288 
289  if( m.data )
290  {
291 #if OPENCV_3
292  m.addref();
293  Py_INCREF(o);
294 #else
295  m.refcount = refcountFromPyObject(o);
296  m.addref(); // protect the original numpy array from deallocation
297  // (since Mat destructor will decrement the reference counter)
298 #endif
299  };
300  m.allocator = &g_numpyAllocator;
301 
302  if( transposed )
303  {
304  ::cv::Mat tmp;
305  tmp.allocator = &g_numpyAllocator;
306  transpose(m, tmp);
307  m = tmp;
308  }
309  return m;
310  }
PyObject * larcv::convert::NDArrayConverter::toNDArray ( const ::cv::Mat &  mat)

Definition at line 312 of file NDArrayConverter.cxx.

References ERRWRAP2, larcv::convert::g_numpyAllocator, and larcv::convert::pyObjectFromRefcount().

Referenced by larlite::HitImageMaker::GetCanny(), and larlite::HitImageMaker::GetImage().

313  {
314 #if OPENCV_3
315  if( !m.data )
316  Py_RETURN_NONE;
317  ::cv::Mat temp, *p = (::cv::Mat*)&m;
318  if(!p->u || p->allocator != &g_numpyAllocator)
319  {
320  temp.allocator = &g_numpyAllocator;
321  m.copyTo(temp);
322  p = &temp;
323  }
324  PyObject* o = (PyObject*)p->u->userdata;
325  Py_INCREF(o);
326  // p->addref();
327  // pyObjectFromRefcount(p->refcount);
328  return o;
329 #else
330  if( !m.data )
331  Py_RETURN_NONE;
332  ::cv::Mat temp, *p = (::cv::Mat*)&m;
333  if(!p->refcount || p->allocator != &g_numpyAllocator)
334  {
335  temp.allocator = &g_numpyAllocator;
336  ERRWRAP2(m.copyTo(temp));
337  p = &temp;
338  }
339  p->addref();
340  return pyObjectFromRefcount(p->refcount);
341 #endif
342  }
PyObject * larcv::convert::NDArrayConverter::toNDArray ( const ::larcv::Image2D img)

Definition at line 344 of file NDArrayConverter.cxx.

345  {
346  return nullptr;
347  }
PyObject * larcv::convert::NDArrayConverter::toNDArray ( const ::larcv::Point2DArray pt_v)

Definition at line 348 of file NDArrayConverter.cxx.

349  {
350  int* dim_data = new int[2];
351  dim_data[0] = 2;
352  dim_data[1] = (int)(pt_v.size());
353 
354  return PyArray_FromDimsAndData( 2, dim_data, NPY_DOUBLE, (char*) &(pt_v._data[0]) );
355  }

The documentation for this class was generated from the following files: