LArOpenCV  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
NDArrayConverter.h
Go to the documentation of this file.
1 // Author: Sudeep Pillai (spillai@csail.mit.edu)
2 // Modified for OpenCV 3.0: Kazuhiro Terao (kazuhiro@nevis.columbia.edu)
3 // Note: Stripped from Opencv (opencv/modules/python/src2/cv2.cpp)
4 
5 # ifndef __OPECV_IMUTIL_CONVERT_H__
6 # define __OPECV_IMUTIL_CONVERT_H__
7 
8 #include <Python.h>
9 #include <opencv2/opencv.hpp>
10 #include <opencv2/core/core.hpp>
11 
12 //#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
13 
14 #include <numpy/ndarrayobject.h>
15 #include "Core/Image2D.h"
16 #include "Core/Point2DArray.h"
17 static PyObject* opencv_error = 0;
18 
19 namespace larcv {
20 
25  namespace convert {
26 
27  static int failmsg(const char *fmt, ...);
28 
29  class PyAllowThreads;
30 
31  class PyEnsureGIL;
32 
33 #define ERRWRAP2(expr) \
34  try \
35  { \
36  PyAllowThreads allowThreads; \
37  expr; \
38  } \
39  catch (const cv::Exception &e) \
40  { \
41  PyErr_SetString(opencv_error, e.what()); \
42  return 0; \
43  }
44 
45  //static PyObject* failmsgp(const char *fmt, ...);
46 
47  static size_t REFCOUNT_OFFSET = (size_t)&(((PyObject*)0)->ob_refcnt) +
48  (0x12345678 != *(const size_t*)"\x78\x56\x34\x12\0\0\0\0\0")*sizeof(int);
49 
50  static inline PyObject* pyObjectFromRefcount(const int* refcount)
51  {
52  return (PyObject*)((size_t)refcount - REFCOUNT_OFFSET);
53  }
54 
55  static inline int* refcountFromPyObject(const PyObject* obj)
56  {
57  return (int*)((size_t)obj + REFCOUNT_OFFSET);
58  }
59 
60 
61  class NumpyAllocator;
62 
63  enum { ARG_NONE = 0, ARG_MAT = 1, ARG_SCALAR = 2 };
64 
66  {
67  private:
68  void init();
69  public:
71  ::cv::Mat toMat(const PyObject* o);
72  PyObject* toNDArray(const ::cv::Mat& mat);
74  PyObject* toNDArray(const ::larcv::Point2DArray& pt_v);
75  };
76  }
77 }
78 # endif
79