JeVois  1.17
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
PythonOpenCV.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 // This file is derived from https://github.com/Algomorph/pyboostcvconverter
19 
20 /*
21  * CVBoostConverter.hpp
22  *
23  * Created on: Mar 20, 2014
24  * Author: Gregory Kramida
25  * Copyright: (c) 2014 Gregory Kramida
26  * License: MIT
27 
28  The MIT License (MIT)
29 
30  Copyright (c) 2014 Gregory Kramida
31 
32  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
33  documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
34  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
35  persons to whom the Software is furnished to do so, subject to the following conditions:
36 
37  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
38  Software.
39 
40  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
41  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
42  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
43  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 
45  */
46 
47 #pragma once
48 
49 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
50 #include <Python.h>
51 #include <numpy/ndarrayobject.h>
52 #include <opencv2/core/core.hpp>
53 #include <boost/python.hpp>
54 #include <cstdio>
55 
56 namespace pbcvt
57 {
58  using namespace cv;
59 
60  extern PyObject * opencv_error;
61 
62 
63  //=================== MACROS =================================================================
64 #define ERRWRAP2(expr) \
65  try \
66  { \
67  PyAllowThreads allowThreads; \
68  expr; \
69  } \
70  catch (const cv::Exception &e) \
71  { \
72  PyErr_SetString(opencv_error, e.what()); \
73  return 0; \
74  }
75 
76  //=================== THREADING ==============================================================
77  class PyAllowThreads;
78  class PyEnsureGIL;
79 
80  static size_t REFCOUNT_OFFSET = (size_t)&(((PyObject*)0)->ob_refcnt) +
81  (0x12345678 != *(const size_t*)"\x78\x56\x34\x12\0\0\0\0\0")*sizeof(int);
82 
83  static inline PyObject* pyObjectFromRefcount(const int* refcount)
84  {
85  return (PyObject*)((size_t)refcount - REFCOUNT_OFFSET);
86  }
87 
88  static inline int* refcountFromPyObject(const PyObject* obj)
89  {
90  return (int*)((size_t)obj + REFCOUNT_OFFSET);
91  }
92 
93  //=================== NUMPY ALLOCATOR FOR OPENCV =============================================
94  class NumpyAllocator;
95 
96  //=================== STANDALONE CONVERTER FUNCTIONS =========================================
97  PyObject* fromMatToNDArray(const Mat& m);
98  Mat fromNDArrayToMat(PyObject* o);
99 
100  //=================== BOOST CONVERTERS =======================================================
102  {
103  static PyObject* convert(Mat const& m);
104  };
105 
106  //! Converter from Python numpy NDarray to cv::Mat
108  {
110 
111  /// @brief Check if PyObject is an array and can be converted to OpenCV matrix.
112  static void* convertible(PyObject* object);
113 
114  /// @brief Construct a Mat from an NDArray object.
115  static void construct(PyObject* object,
116  boost::python::converter::rvalue_from_python_stage1_data* data);
117  };
118 } // end namespace pbcvt
pbcvt::matFromNDArrayBoostConverter
Converter from Python numpy NDarray to cv::Mat.
Definition: PythonOpenCV.H:107
pbcvt::fromMatToNDArray
PyObject * fromMatToNDArray(const Mat &m)
Definition: PythonOpenCV.C:164
o
#define o
Definition: Font10x20.C:6
pbcvt::fromNDArrayToMat
Mat fromNDArrayToMat(PyObject *o)
Definition: PythonOpenCV.C:179
pbcvt::opencv_error
PyObject * opencv_error
Definition: PythonOpenCV.C:52
pbcvt
Definition: PythonOpenCV.H:56
pbcvt::matToNDArrayBoostConverter
Definition: PythonOpenCV.H:101