cv2_convert.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #ifndef CV2_CONVERT_HPP
  2. #define CV2_CONVERT_HPP
  3. #include "cv2.hpp"
  4. #include "cv2_util.hpp"
  5. #include "cv2_numpy.hpp"
  6. #include <vector>
  7. #include <string>
  8. #include <type_traits> // std::enable_if
  9. extern PyTypeObject* pyopencv_Mat_TypePtr;
  10. #define CV_HAS_CONVERSION_ERROR(x) (((x) == -1) && PyErr_Occurred())
  11. inline bool isBool(PyObject* obj) CV_NOEXCEPT
  12. {
  13. return PyArray_IsScalar(obj, Bool) || PyBool_Check(obj);
  14. }
  15. //======================================================================================================================
  16. // exception-safe pyopencv_to
  17. template<typename _Tp> static
  18. bool pyopencv_to_safe(PyObject* obj, _Tp& value, const ArgInfo& info)
  19. {
  20. try
  21. {
  22. return pyopencv_to(obj, value, info);
  23. }
  24. catch (const std::exception &e)
  25. {
  26. PyErr_SetString(opencv_error, cv::format("Conversion error: %s, what: %s", info.name, e.what()).c_str());
  27. return false;
  28. }
  29. catch (...)
  30. {
  31. PyErr_SetString(opencv_error, cv::format("Conversion error: %s", info.name).c_str());
  32. return false;
  33. }
  34. }
  35. //======================================================================================================================
  36. template<typename T, class TEnable = void> // TEnable is used for SFINAE checks
  37. struct PyOpenCV_Converter
  38. {
  39. //static inline bool to(PyObject* obj, T& p, const ArgInfo& info);
  40. //static inline PyObject* from(const T& src);
  41. };
  42. // --- Generic
  43. template<typename T>
  44. bool pyopencv_to(PyObject* obj, T& p, const ArgInfo& info) { return PyOpenCV_Converter<T>::to(obj, p, info); }
  45. template<typename T>
  46. PyObject* pyopencv_from(const T& src) { return PyOpenCV_Converter<T>::from(src); }
  47. // --- Matx
  48. template<typename _Tp, int m, int n>
  49. bool pyopencv_to(PyObject* o, cv::Matx<_Tp, m, n>& mx, const ArgInfo& info)
  50. {
  51. cv::Mat tmp;
  52. if (!pyopencv_to(o, tmp, info)) {
  53. return false;
  54. }
  55. tmp.copyTo(mx);
  56. return true;
  57. }
  58. template<typename _Tp, int m, int n>
  59. PyObject* pyopencv_from(const cv::Matx<_Tp, m, n>& matx)
  60. {
  61. return pyopencv_from(cv::Mat(matx));
  62. }
  63. // --- bool
  64. template<> bool pyopencv_to(PyObject* obj, bool& value, const ArgInfo& info);
  65. template<> PyObject* pyopencv_from(const bool& value);
  66. // --- Mat
  67. template<> bool pyopencv_to(PyObject* o, cv::Mat& m, const ArgInfo& info);
  68. template<> PyObject* pyopencv_from(const cv::Mat& m);
  69. // --- Ptr
  70. template<typename T>
  71. struct PyOpenCV_Converter< cv::Ptr<T> >
  72. {
  73. static PyObject* from(const cv::Ptr<T>& p)
  74. {
  75. if (!p)
  76. Py_RETURN_NONE;
  77. return pyopencv_from(*p);
  78. }
  79. static bool to(PyObject *o, cv::Ptr<T>& p, const ArgInfo& info)
  80. {
  81. if (!o || o == Py_None)
  82. return true;
  83. p = cv::makePtr<T>();
  84. return pyopencv_to(o, *p, info);
  85. }
  86. };
  87. // --- ptr
  88. template<> bool pyopencv_to(PyObject* obj, void*& ptr, const ArgInfo& info);
  89. PyObject* pyopencv_from(void*& ptr);
  90. // --- Scalar
  91. template<> bool pyopencv_to(PyObject *o, cv::Scalar& s, const ArgInfo& info);
  92. template<> PyObject* pyopencv_from(const cv::Scalar& src);
  93. // --- size_t
  94. template<> bool pyopencv_to(PyObject* obj, size_t& value, const ArgInfo& info);
  95. template<> PyObject* pyopencv_from(const size_t& value);
  96. // --- int
  97. template<> bool pyopencv_to(PyObject* obj, int& value, const ArgInfo& info);
  98. template<> PyObject* pyopencv_from(const int& value);
  99. // --- int64
  100. template<> PyObject* pyopencv_from(const int64& value);
  101. // There is conflict between "size_t" and "unsigned int".
  102. // They are the same type on some 32-bit platforms.
  103. template<typename T>
  104. struct PyOpenCV_Converter
  105. < T, typename std::enable_if< std::is_same<unsigned int, T>::value && !std::is_same<unsigned int, size_t>::value >::type >
  106. {
  107. static inline PyObject* from(const unsigned int& value)
  108. {
  109. return PyLong_FromUnsignedLong(value);
  110. }
  111. static inline bool to(PyObject* obj, unsigned int& value, const ArgInfo& info)
  112. {
  113. CV_UNUSED(info);
  114. if(!obj || obj == Py_None)
  115. return true;
  116. if(PyInt_Check(obj))
  117. value = (unsigned int)PyInt_AsLong(obj);
  118. else if(PyLong_Check(obj))
  119. value = (unsigned int)PyLong_AsLong(obj);
  120. else
  121. return false;
  122. return value != (unsigned int)-1 || !PyErr_Occurred();
  123. }
  124. };
  125. // --- uchar
  126. template<> bool pyopencv_to(PyObject* obj, uchar& value, const ArgInfo& info);
  127. template<> PyObject* pyopencv_from(const uchar& value);
  128. // --- char
  129. template<> bool pyopencv_to(PyObject* obj, char& value, const ArgInfo& info);
  130. // --- double
  131. template<> bool pyopencv_to(PyObject* obj, double& value, const ArgInfo& info);
  132. template<> PyObject* pyopencv_from(const double& value);
  133. // --- float
  134. template<> bool pyopencv_to(PyObject* obj, float& value, const ArgInfo& info);
  135. template<> PyObject* pyopencv_from(const float& value);
  136. // --- string
  137. template<> bool pyopencv_to(PyObject* obj, cv::String &value, const ArgInfo& info);
  138. template<> PyObject* pyopencv_from(const cv::String& value);
  139. #if CV_VERSION_MAJOR == 3
  140. template<> PyObject* pyopencv_from(const std::string& value);
  141. #endif
  142. // --- Size
  143. template<> bool pyopencv_to(PyObject* obj, cv::Size& sz, const ArgInfo& info);
  144. template<> PyObject* pyopencv_from(const cv::Size& sz);
  145. template<> bool pyopencv_to(PyObject* obj, cv::Size_<float>& sz, const ArgInfo& info);
  146. template<> PyObject* pyopencv_from(const cv::Size_<float>& sz);
  147. // --- Rect
  148. template<> bool pyopencv_to(PyObject* obj, cv::Rect& r, const ArgInfo& info);
  149. template<> PyObject* pyopencv_from(const cv::Rect& r);
  150. template<> bool pyopencv_to(PyObject* obj, cv::Rect2d& r, const ArgInfo& info);
  151. template<> PyObject* pyopencv_from(const cv::Rect2d& r);
  152. // --- RotatedRect
  153. template<> bool pyopencv_to(PyObject* obj, cv::RotatedRect& dst, const ArgInfo& info);
  154. template<> PyObject* pyopencv_from(const cv::RotatedRect& src);
  155. // --- Range
  156. template<> bool pyopencv_to(PyObject* obj, cv::Range& r, const ArgInfo& info);
  157. template<> PyObject* pyopencv_from(const cv::Range& r);
  158. // --- Point
  159. template<> bool pyopencv_to(PyObject* obj, cv::Point& p, const ArgInfo& info);
  160. template<> PyObject* pyopencv_from(const cv::Point& p);
  161. template<> bool pyopencv_to(PyObject* obj, cv::Point2f& p, const ArgInfo& info);
  162. template<> PyObject* pyopencv_from(const cv::Point2f& p);
  163. template<> bool pyopencv_to(PyObject* obj, cv::Point2d& p, const ArgInfo& info);
  164. template<> PyObject* pyopencv_from(const cv::Point2d& p);
  165. template<> bool pyopencv_to(PyObject* obj, cv::Point3f& p, const ArgInfo& info);
  166. template<> PyObject* pyopencv_from(const cv::Point3f& p);
  167. template<> bool pyopencv_to(PyObject* obj, cv::Point3d& p, const ArgInfo& info);
  168. template<> PyObject* pyopencv_from(const cv::Point3d& p);
  169. // --- Vec
  170. template<typename _Tp, int cn>
  171. bool pyopencv_to(PyObject* o, cv::Vec<_Tp, cn>& vec, const ArgInfo& info)
  172. {
  173. return pyopencv_to(o, (cv::Matx<_Tp, cn, 1>&)vec, info);
  174. }
  175. bool pyopencv_to(PyObject* obj, cv::Vec4d& v, ArgInfo& info);
  176. PyObject* pyopencv_from(const cv::Vec4d& v);
  177. bool pyopencv_to(PyObject* obj, cv::Vec4f& v, ArgInfo& info);
  178. PyObject* pyopencv_from(const cv::Vec4f& v);
  179. bool pyopencv_to(PyObject* obj, cv::Vec4i& v, ArgInfo& info);
  180. PyObject* pyopencv_from(const cv::Vec4i& v);
  181. bool pyopencv_to(PyObject* obj, cv::Vec3d& v, ArgInfo& info);
  182. PyObject* pyopencv_from(const cv::Vec3d& v);
  183. bool pyopencv_to(PyObject* obj, cv::Vec3f& v, ArgInfo& info);
  184. PyObject* pyopencv_from(const cv::Vec3f& v);
  185. bool pyopencv_to(PyObject* obj, cv::Vec3i& v, ArgInfo& info);
  186. PyObject* pyopencv_from(const cv::Vec3i& v);
  187. bool pyopencv_to(PyObject* obj, cv::Vec2d& v, ArgInfo& info);
  188. PyObject* pyopencv_from(const cv::Vec2d& v);
  189. bool pyopencv_to(PyObject* obj, cv::Vec2f& v, ArgInfo& info);
  190. PyObject* pyopencv_from(const cv::Vec2f& v);
  191. bool pyopencv_to(PyObject* obj, cv::Vec2i& v, ArgInfo& info);
  192. PyObject* pyopencv_from(const cv::Vec2i& v);
  193. // --- TermCriteria
  194. template<> bool pyopencv_to(PyObject* obj, cv::TermCriteria& dst, const ArgInfo& info);
  195. template<> PyObject* pyopencv_from(const cv::TermCriteria& src);
  196. // --- Moments
  197. template<> PyObject* pyopencv_from(const cv::Moments& m);
  198. // --- pair
  199. template<> PyObject* pyopencv_from(const std::pair<int, double>& src);
  200. // --- vector
  201. template <typename Tp>
  202. struct pyopencvVecConverter;
  203. template <typename Tp>
  204. bool pyopencv_to(PyObject* obj, std::vector<Tp>& value, const ArgInfo& info)
  205. {
  206. if (!obj || obj == Py_None)
  207. {
  208. return true;
  209. }
  210. return pyopencvVecConverter<Tp>::to(obj, value, info);
  211. }
  212. template <typename Tp>
  213. PyObject* pyopencv_from(const std::vector<Tp>& value)
  214. {
  215. return pyopencvVecConverter<Tp>::from(value);
  216. }
  217. template <typename Tp>
  218. static bool pyopencv_to_generic_vec(PyObject* obj, std::vector<Tp>& value, const ArgInfo& info)
  219. {
  220. if (!obj || obj == Py_None)
  221. {
  222. return true;
  223. }
  224. if (!PySequence_Check(obj))
  225. {
  226. failmsg("Can't parse '%s'. Input argument doesn't provide sequence protocol", info.name);
  227. return false;
  228. }
  229. const size_t n = static_cast<size_t>(PySequence_Size(obj));
  230. value.resize(n);
  231. for (size_t i = 0; i < n; i++)
  232. {
  233. SafeSeqItem item_wrap(obj, i);
  234. if (!pyopencv_to(item_wrap.item, value[i], info))
  235. {
  236. failmsg("Can't parse '%s'. Sequence item with index %lu has a wrong type", info.name, i);
  237. return false;
  238. }
  239. }
  240. return true;
  241. }
  242. template<> inline bool pyopencv_to_generic_vec(PyObject* obj, std::vector<bool>& value, const ArgInfo& info)
  243. {
  244. if (!obj || obj == Py_None)
  245. {
  246. return true;
  247. }
  248. if (!PySequence_Check(obj))
  249. {
  250. failmsg("Can't parse '%s'. Input argument doesn't provide sequence protocol", info.name);
  251. return false;
  252. }
  253. const size_t n = static_cast<size_t>(PySequence_Size(obj));
  254. value.resize(n);
  255. for (size_t i = 0; i < n; i++)
  256. {
  257. SafeSeqItem item_wrap(obj, i);
  258. bool elem{};
  259. if (!pyopencv_to(item_wrap.item, elem, info))
  260. {
  261. failmsg("Can't parse '%s'. Sequence item with index %lu has a wrong type", info.name, i);
  262. return false;
  263. }
  264. value[i] = elem;
  265. }
  266. return true;
  267. }
  268. template <typename Tp>
  269. static PyObject* pyopencv_from_generic_vec(const std::vector<Tp>& value)
  270. {
  271. Py_ssize_t n = static_cast<Py_ssize_t>(value.size());
  272. PySafeObject seq(PyTuple_New(n));
  273. for (Py_ssize_t i = 0; i < n; i++)
  274. {
  275. PyObject* item = pyopencv_from(value[i]);
  276. // If item can't be assigned - PyTuple_SetItem raises exception and returns -1.
  277. if (!item || PyTuple_SetItem(seq, i, item) == -1)
  278. {
  279. return NULL;
  280. }
  281. }
  282. return seq.release();
  283. }
  284. template<> inline PyObject* pyopencv_from_generic_vec(const std::vector<bool>& value)
  285. {
  286. Py_ssize_t n = static_cast<Py_ssize_t>(value.size());
  287. PySafeObject seq(PyTuple_New(n));
  288. for (Py_ssize_t i = 0; i < n; i++)
  289. {
  290. bool elem = value[i];
  291. PyObject* item = pyopencv_from(elem);
  292. // If item can't be assigned - PyTuple_SetItem raises exception and returns -1.
  293. if (!item || PyTuple_SetItem(seq, i, item) == -1)
  294. {
  295. return NULL;
  296. }
  297. }
  298. return seq.release();
  299. }
  300. namespace traits {
  301. template <bool Value>
  302. struct BooleanConstant
  303. {
  304. static const bool value = Value;
  305. typedef BooleanConstant<Value> type;
  306. };
  307. typedef BooleanConstant<true> TrueType;
  308. typedef BooleanConstant<false> FalseType;
  309. template <class T>
  310. struct VoidType {
  311. typedef void type;
  312. };
  313. template <class T, class DType = void>
  314. struct IsRepresentableAsMatDataType : FalseType
  315. {
  316. };
  317. template <class T>
  318. struct IsRepresentableAsMatDataType<T, typename VoidType<typename cv::DataType<T>::channel_type>::type> : TrueType
  319. {
  320. };
  321. // https://github.com/opencv/opencv/issues/20930
  322. template <> struct IsRepresentableAsMatDataType<cv::RotatedRect, void> : FalseType {};
  323. } // namespace traits
  324. template <typename Tp>
  325. struct pyopencvVecConverter
  326. {
  327. typedef typename std::vector<Tp>::iterator VecIt;
  328. static bool to(PyObject* obj, std::vector<Tp>& value, const ArgInfo& info)
  329. {
  330. if (!PyArray_Check(obj))
  331. {
  332. return pyopencv_to_generic_vec(obj, value, info);
  333. }
  334. // If user passed an array it is possible to make faster conversions in several cases
  335. PyArrayObject* array_obj = reinterpret_cast<PyArrayObject*>(obj);
  336. const NPY_TYPES target_type = asNumpyType<Tp>();
  337. const NPY_TYPES source_type = static_cast<NPY_TYPES>(PyArray_TYPE(array_obj));
  338. if (target_type == NPY_OBJECT)
  339. {
  340. // Non-planar arrays representing objects (e.g. array of N Rect is an array of shape Nx4) have NPY_OBJECT
  341. // as their target type.
  342. return pyopencv_to_generic_vec(obj, value, info);
  343. }
  344. if (PyArray_NDIM(array_obj) > 1)
  345. {
  346. failmsg("Can't parse %dD array as '%s' vector argument", PyArray_NDIM(array_obj), info.name);
  347. return false;
  348. }
  349. if (target_type != source_type)
  350. {
  351. // Source type requires conversion
  352. // Allowed conversions for target type is handled in the corresponding pyopencv_to function
  353. return pyopencv_to_generic_vec(obj, value, info);
  354. }
  355. // For all other cases, all array data can be directly copied to std::vector data
  356. // Simple `memcpy` is not possible because NumPy array can reference a slice of the bigger array:
  357. // ```
  358. // arr = np.ones((8, 4, 5), dtype=np.int32)
  359. // convertible_to_vector_of_int = arr[:, 0, 1]
  360. // ```
  361. value.resize(static_cast<size_t>(PyArray_SIZE(array_obj)));
  362. const npy_intp item_step = PyArray_STRIDE(array_obj, 0) / PyArray_ITEMSIZE(array_obj);
  363. const Tp* data_ptr = static_cast<Tp*>(PyArray_DATA(array_obj));
  364. for (VecIt it = value.begin(); it != value.end(); ++it, data_ptr += item_step) {
  365. *it = *data_ptr;
  366. }
  367. return true;
  368. }
  369. static PyObject* from(const std::vector<Tp>& value)
  370. {
  371. if (value.empty())
  372. {
  373. return PyTuple_New(0);
  374. }
  375. return from(value, ::traits::IsRepresentableAsMatDataType<Tp>());
  376. }
  377. private:
  378. static PyObject* from(const std::vector<Tp>& value, ::traits::FalseType)
  379. {
  380. // Underlying type is not representable as Mat Data Type
  381. return pyopencv_from_generic_vec(value);
  382. }
  383. static PyObject* from(const std::vector<Tp>& value, ::traits::TrueType)
  384. {
  385. // Underlying type is representable as Mat Data Type, so faster return type is available
  386. typedef cv::DataType<Tp> DType;
  387. typedef typename DType::channel_type UnderlyingArrayType;
  388. // If Mat is always exposed as NumPy array this code path can be reduced to the following snipped:
  389. // Mat src(value);
  390. // PyObject* array = pyopencv_from(src);
  391. // return PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(array));
  392. // This puts unnecessary restrictions on Mat object those might be avoided without losing the performance.
  393. // Moreover, this version is a bit faster, because it doesn't create temporary objects with reference counting.
  394. const NPY_TYPES target_type = asNumpyType<UnderlyingArrayType>();
  395. const int cols = DType::channels;
  396. PyObject* array = NULL;
  397. if (cols == 1)
  398. {
  399. npy_intp dims = static_cast<npy_intp>(value.size());
  400. array = PyArray_SimpleNew(1, &dims, target_type);
  401. }
  402. else
  403. {
  404. npy_intp dims[2] = {static_cast<npy_intp>(value.size()), cols};
  405. array = PyArray_SimpleNew(2, dims, target_type);
  406. }
  407. if(!array)
  408. {
  409. // NumPy arrays with shape (N, 1) and (N) are not equal, so correct error message should distinguish
  410. // them too.
  411. cv::String shape;
  412. if (cols > 1)
  413. {
  414. shape = cv::format("(%d x %d)", static_cast<int>(value.size()), cols);
  415. }
  416. else
  417. {
  418. shape = cv::format("(%d)", static_cast<int>(value.size()));
  419. }
  420. const cv::String error_message = cv::format("Can't allocate NumPy array for vector with dtype=%d and shape=%s",
  421. static_cast<int>(target_type), shape.c_str());
  422. emit_failmsg(PyExc_MemoryError, error_message.c_str());
  423. return array;
  424. }
  425. // Fill the array
  426. PyArrayObject* array_obj = reinterpret_cast<PyArrayObject*>(array);
  427. UnderlyingArrayType* array_data = static_cast<UnderlyingArrayType*>(PyArray_DATA(array_obj));
  428. // if Tp is representable as Mat DataType, so the following cast is pretty safe...
  429. const UnderlyingArrayType* value_data = reinterpret_cast<const UnderlyingArrayType*>(value.data());
  430. memcpy(array_data, value_data, sizeof(UnderlyingArrayType) * value.size() * static_cast<size_t>(cols));
  431. return array;
  432. }
  433. };
  434. // --- tuple
  435. template<std::size_t I = 0, typename... Tp>
  436. inline typename std::enable_if<I == sizeof...(Tp), void>::type
  437. convert_to_python_tuple(const std::tuple<Tp...>&, PyObject*) { }
  438. template<std::size_t I = 0, typename... Tp>
  439. inline typename std::enable_if<I < sizeof...(Tp), void>::type
  440. convert_to_python_tuple(const std::tuple<Tp...>& cpp_tuple, PyObject* py_tuple)
  441. {
  442. PyObject* item = pyopencv_from(std::get<I>(cpp_tuple));
  443. if (!item)
  444. return;
  445. PyTuple_SetItem(py_tuple, I, item);
  446. convert_to_python_tuple<I + 1, Tp...>(cpp_tuple, py_tuple);
  447. }
  448. template<typename... Ts>
  449. PyObject* pyopencv_from(const std::tuple<Ts...>& cpp_tuple)
  450. {
  451. size_t size = sizeof...(Ts);
  452. PyObject* py_tuple = PyTuple_New(size);
  453. convert_to_python_tuple(cpp_tuple, py_tuple);
  454. size_t actual_size = PyTuple_Size(py_tuple);
  455. if (actual_size < size)
  456. {
  457. Py_DECREF(py_tuple);
  458. return NULL;
  459. }
  460. return py_tuple;
  461. }
  462. #endif // CV2_CONVERT_HPP