cv2_convert.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. // must be defined before importing numpy headers
  2. // https://numpy.org/doc/1.17/reference/c-api.array.html#importing-the-api
  3. #define NO_IMPORT_ARRAY
  4. #define PY_ARRAY_UNIQUE_SYMBOL opencv_ARRAY_API
  5. #include "cv2_convert.hpp"
  6. #include "cv2_numpy.hpp"
  7. #include "opencv2/core/utils/logger.hpp"
  8. PyTypeObject* pyopencv_Mat_TypePtr = nullptr;
  9. //======================================================================================================================
  10. using namespace cv;
  11. template <typename T>
  12. static std::string pycv_dumpArray(const T* arr, int n)
  13. {
  14. std::ostringstream out;
  15. out << "[";
  16. for (int i = 0; i < n; ++i)
  17. out << " " << arr[i];
  18. out << " ]";
  19. return out.str();
  20. }
  21. //======================================================================================================================
  22. // --- Mat
  23. // special case, when the converter needs full ArgInfo structure
  24. template<>
  25. bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo& info)
  26. {
  27. if(!o || o == Py_None)
  28. {
  29. if( !m.data )
  30. m.allocator = &g_numpyAllocator;
  31. return true;
  32. }
  33. if( PyInt_Check(o) )
  34. {
  35. double v[] = {static_cast<double>(PyInt_AsLong((PyObject*)o)), 0., 0., 0.};
  36. m = Mat(4, 1, CV_64F, v).clone();
  37. return true;
  38. }
  39. if( PyFloat_Check(o) )
  40. {
  41. double v[] = {PyFloat_AsDouble((PyObject*)o), 0., 0., 0.};
  42. m = Mat(4, 1, CV_64F, v).clone();
  43. return true;
  44. }
  45. if( PyTuple_Check(o) )
  46. {
  47. int i, sz = (int)PyTuple_Size((PyObject*)o);
  48. m = Mat(sz, 1, CV_64F);
  49. for( i = 0; i < sz; i++ )
  50. {
  51. PyObject* oi = PyTuple_GetItem(o, i);
  52. if( PyInt_Check(oi) )
  53. m.at<double>(i) = (double)PyInt_AsLong(oi);
  54. else if( PyFloat_Check(oi) )
  55. m.at<double>(i) = (double)PyFloat_AsDouble(oi);
  56. else
  57. {
  58. failmsg("%s is not a numerical tuple", info.name);
  59. m.release();
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. if( !PyArray_Check(o) )
  66. {
  67. failmsg("%s is not a numpy array, neither a scalar", info.name);
  68. return false;
  69. }
  70. PyArrayObject* oarr = (PyArrayObject*) o;
  71. bool needcopy = false, needcast = false;
  72. int typenum = PyArray_TYPE(oarr), new_typenum = typenum;
  73. int type = typenum == NPY_UBYTE ? CV_8U :
  74. typenum == NPY_BYTE ? CV_8S :
  75. typenum == NPY_USHORT ? CV_16U :
  76. typenum == NPY_SHORT ? CV_16S :
  77. typenum == NPY_INT ? CV_32S :
  78. typenum == NPY_INT32 ? CV_32S :
  79. typenum == NPY_FLOAT ? CV_32F :
  80. typenum == NPY_DOUBLE ? CV_64F : -1;
  81. if( type < 0 )
  82. {
  83. if( typenum == NPY_INT64 || typenum == NPY_UINT64 || typenum == NPY_LONG )
  84. {
  85. needcopy = needcast = true;
  86. new_typenum = NPY_INT;
  87. type = CV_32S;
  88. }
  89. else
  90. {
  91. failmsg("%s data type = %d is not supported", info.name, typenum);
  92. return false;
  93. }
  94. }
  95. #ifndef CV_MAX_DIM
  96. const int CV_MAX_DIM = 32;
  97. #endif
  98. int ndims = PyArray_NDIM(oarr);
  99. if(ndims >= CV_MAX_DIM)
  100. {
  101. failmsg("%s dimensionality (=%d) is too high", info.name, ndims);
  102. return false;
  103. }
  104. size_t elemsize = CV_ELEM_SIZE1(type);
  105. const npy_intp* _sizes = PyArray_DIMS(oarr);
  106. const npy_intp* _strides = PyArray_STRIDES(oarr);
  107. CV_LOG_DEBUG(NULL, "Incoming ndarray '" << info.name << "': ndims=" << ndims << " _sizes=" << pycv_dumpArray(_sizes, ndims) << " _strides=" << pycv_dumpArray(_strides, ndims));
  108. bool ismultichannel = ndims == 3 && _sizes[2] <= CV_CN_MAX;
  109. if (pyopencv_Mat_TypePtr && PyObject_TypeCheck(o, pyopencv_Mat_TypePtr))
  110. {
  111. bool wrapChannels = false;
  112. PyObject* pyobj_wrap_channels = PyObject_GetAttrString(o, "wrap_channels");
  113. if (pyobj_wrap_channels)
  114. {
  115. if (!pyopencv_to_safe(pyobj_wrap_channels, wrapChannels, ArgInfo("cv.Mat.wrap_channels", 0)))
  116. {
  117. // TODO extra message
  118. Py_DECREF(pyobj_wrap_channels);
  119. return false;
  120. }
  121. Py_DECREF(pyobj_wrap_channels);
  122. }
  123. ismultichannel = wrapChannels && ndims >= 1;
  124. }
  125. for( int i = ndims-1; i >= 0 && !needcopy; i-- )
  126. {
  127. // these checks handle cases of
  128. // a) multi-dimensional (ndims > 2) arrays, as well as simpler 1- and 2-dimensional cases
  129. // b) transposed arrays, where _strides[] elements go in non-descending order
  130. // c) flipped arrays, where some of _strides[] elements are negative
  131. // the _sizes[i] > 1 is needed to avoid spurious copies when NPY_RELAXED_STRIDES is set
  132. if( (i == ndims-1 && _sizes[i] > 1 && (size_t)_strides[i] != elemsize) ||
  133. (i < ndims-1 && _sizes[i] > 1 && _strides[i] < _strides[i+1]) )
  134. needcopy = true;
  135. }
  136. if (ismultichannel)
  137. {
  138. int channels = ndims >= 1 ? (int)_sizes[ndims - 1] : 1;
  139. if (channels > CV_CN_MAX)
  140. {
  141. failmsg("%s unable to wrap channels, too high (%d > CV_CN_MAX=%d)", info.name, (int)channels, (int)CV_CN_MAX);
  142. return false;
  143. }
  144. ndims--;
  145. type |= CV_MAKETYPE(0, channels);
  146. if (ndims >= 1 && _strides[ndims - 1] != (npy_intp)elemsize*_sizes[ndims])
  147. needcopy = true;
  148. elemsize = CV_ELEM_SIZE(type);
  149. }
  150. if (needcopy)
  151. {
  152. if (info.outputarg)
  153. {
  154. failmsg("Layout of the output array %s is incompatible with cv::Mat", info.name);
  155. return false;
  156. }
  157. if( needcast ) {
  158. o = PyArray_Cast(oarr, new_typenum);
  159. oarr = (PyArrayObject*) o;
  160. }
  161. else {
  162. oarr = PyArray_GETCONTIGUOUS(oarr);
  163. o = (PyObject*) oarr;
  164. }
  165. _strides = PyArray_STRIDES(oarr);
  166. }
  167. int size[CV_MAX_DIM+1] = {};
  168. size_t step[CV_MAX_DIM+1] = {};
  169. // Normalize strides in case NPY_RELAXED_STRIDES is set
  170. size_t default_step = elemsize;
  171. for ( int i = ndims - 1; i >= 0; --i )
  172. {
  173. size[i] = (int)_sizes[i];
  174. if ( size[i] > 1 )
  175. {
  176. step[i] = (size_t)_strides[i];
  177. default_step = step[i] * size[i];
  178. }
  179. else
  180. {
  181. step[i] = default_step;
  182. default_step *= size[i];
  183. }
  184. }
  185. // handle degenerate case
  186. // FIXIT: Don't force 1D for Scalars
  187. if( ndims == 0) {
  188. size[ndims] = 1;
  189. step[ndims] = elemsize;
  190. ndims++;
  191. }
  192. #if 1
  193. CV_LOG_DEBUG(NULL, "Construct Mat: ndims=" << ndims << " size=" << pycv_dumpArray(size, ndims) << " step=" << pycv_dumpArray(step, ndims) << " type=" << cv::typeToString(type));
  194. #endif
  195. m = Mat(ndims, size, type, PyArray_DATA(oarr), step);
  196. m.u = g_numpyAllocator.allocate(o, ndims, size, type, step);
  197. m.addref();
  198. if( !needcopy )
  199. {
  200. Py_INCREF(o);
  201. }
  202. m.allocator = &g_numpyAllocator;
  203. return true;
  204. }
  205. template<>
  206. PyObject* pyopencv_from(const cv::Mat& m)
  207. {
  208. if( !m.data )
  209. Py_RETURN_NONE;
  210. cv::Mat temp, *p = (cv::Mat*)&m;
  211. if(!p->u || p->allocator != &g_numpyAllocator)
  212. {
  213. temp.allocator = &g_numpyAllocator;
  214. ERRWRAP2(m.copyTo(temp));
  215. p = &temp;
  216. }
  217. PyObject* o = (PyObject*)p->u->userdata;
  218. Py_INCREF(o);
  219. return o;
  220. }
  221. // --- bool
  222. template<>
  223. bool pyopencv_to(PyObject* obj, bool& value, const ArgInfo& info)
  224. {
  225. if (!obj || obj == Py_None)
  226. {
  227. return true;
  228. }
  229. if (isBool(obj) || PyArray_IsIntegerScalar(obj))
  230. {
  231. npy_bool npy_value = NPY_FALSE;
  232. const int ret_code = PyArray_BoolConverter(obj, &npy_value);
  233. if (ret_code >= 0)
  234. {
  235. value = (npy_value == NPY_TRUE);
  236. return true;
  237. }
  238. }
  239. failmsg("Argument '%s' is not convertable to bool", info.name);
  240. return false;
  241. }
  242. template<>
  243. PyObject* pyopencv_from(const bool& value)
  244. {
  245. return PyBool_FromLong(value);
  246. }
  247. // --- ptr
  248. template<>
  249. bool pyopencv_to(PyObject* obj, void*& ptr, const ArgInfo& info)
  250. {
  251. CV_UNUSED(info);
  252. if (!obj || obj == Py_None)
  253. return true;
  254. if (!PyLong_Check(obj))
  255. return false;
  256. ptr = PyLong_AsVoidPtr(obj);
  257. return ptr != NULL && !PyErr_Occurred();
  258. }
  259. PyObject* pyopencv_from(void*& ptr)
  260. {
  261. return PyLong_FromVoidPtr(ptr);
  262. }
  263. // -- Scalar
  264. template<>
  265. bool pyopencv_to(PyObject *o, Scalar& s, const ArgInfo& info)
  266. {
  267. if(!o || o == Py_None)
  268. return true;
  269. if (PySequence_Check(o)) {
  270. if (4 < PySequence_Size(o))
  271. {
  272. failmsg("Scalar value for argument '%s' is longer than 4", info.name);
  273. return false;
  274. }
  275. for (Py_ssize_t i = 0; i < PySequence_Size(o); i++) {
  276. SafeSeqItem item_wrap(o, i);
  277. PyObject *item = item_wrap.item;
  278. if (PyFloat_Check(item) || PyInt_Check(item)) {
  279. s[(int)i] = PyFloat_AsDouble(item);
  280. } else {
  281. failmsg("Scalar value for argument '%s' is not numeric", info.name);
  282. return false;
  283. }
  284. }
  285. } else {
  286. if (PyFloat_Check(o) || PyInt_Check(o)) {
  287. s[0] = PyFloat_AsDouble(o);
  288. } else {
  289. failmsg("Scalar value for argument '%s' is not numeric", info.name);
  290. return false;
  291. }
  292. }
  293. return true;
  294. }
  295. template<>
  296. PyObject* pyopencv_from(const Scalar& src)
  297. {
  298. return Py_BuildValue("(dddd)", src[0], src[1], src[2], src[3]);
  299. }
  300. // --- size_t
  301. template<>
  302. bool pyopencv_to(PyObject* obj, size_t& value, const ArgInfo& info)
  303. {
  304. if (!obj || obj == Py_None)
  305. {
  306. return true;
  307. }
  308. if (isBool(obj))
  309. {
  310. failmsg("Argument '%s' must be integer type, not bool", info.name);
  311. return false;
  312. }
  313. if (PyArray_IsIntegerScalar(obj))
  314. {
  315. if (PyLong_Check(obj))
  316. {
  317. #if defined(CV_PYTHON_3)
  318. value = PyLong_AsSize_t(obj);
  319. #else
  320. #if ULONG_MAX == SIZE_MAX
  321. value = PyLong_AsUnsignedLong(obj);
  322. #else
  323. value = PyLong_AsUnsignedLongLong(obj);
  324. #endif
  325. #endif
  326. }
  327. #if !defined(CV_PYTHON_3)
  328. // Python 2.x has PyIntObject which is not a subtype of PyLongObject
  329. // Overflow check here is unnecessary because object will be converted to long on the
  330. // interpreter side
  331. else if (PyInt_Check(obj))
  332. {
  333. const long res = PyInt_AsLong(obj);
  334. if (res < 0) {
  335. failmsg("Argument '%s' can not be safely parsed to 'size_t'", info.name);
  336. return false;
  337. }
  338. #if ULONG_MAX == SIZE_MAX
  339. value = PyInt_AsUnsignedLongMask(obj);
  340. #else
  341. value = PyInt_AsUnsignedLongLongMask(obj);
  342. #endif
  343. }
  344. #endif
  345. else
  346. {
  347. const bool isParsed = parseNumpyScalar<size_t>(obj, value);
  348. if (!isParsed) {
  349. failmsg("Argument '%s' can not be safely parsed to 'size_t'", info.name);
  350. return false;
  351. }
  352. }
  353. }
  354. else
  355. {
  356. failmsg("Argument '%s' is required to be an integer", info.name);
  357. return false;
  358. }
  359. return !PyErr_Occurred();
  360. }
  361. template<>
  362. PyObject* pyopencv_from(const size_t& value)
  363. {
  364. return PyLong_FromSize_t(value);
  365. }
  366. // --- int
  367. template<>
  368. bool pyopencv_to(PyObject* obj, int& value, const ArgInfo& info)
  369. {
  370. if (!obj || obj == Py_None)
  371. {
  372. return true;
  373. }
  374. if (isBool(obj))
  375. {
  376. failmsg("Argument '%s' must be integer, not bool", info.name);
  377. return false;
  378. }
  379. if (PyArray_IsIntegerScalar(obj))
  380. {
  381. value = PyArray_PyIntAsInt(obj);
  382. }
  383. else
  384. {
  385. failmsg("Argument '%s' is required to be an integer", info.name);
  386. return false;
  387. }
  388. return !CV_HAS_CONVERSION_ERROR(value);
  389. }
  390. template<>
  391. PyObject* pyopencv_from(const int& value)
  392. {
  393. return PyInt_FromLong(value);
  394. }
  395. // --- int64
  396. template<>
  397. PyObject* pyopencv_from(const int64& value)
  398. {
  399. return PyLong_FromLongLong(value);
  400. }
  401. // --- uchar
  402. template<>
  403. bool pyopencv_to(PyObject* obj, uchar& value, const ArgInfo& info)
  404. {
  405. CV_UNUSED(info);
  406. if(!obj || obj == Py_None)
  407. return true;
  408. int ivalue = (int)PyInt_AsLong(obj);
  409. value = cv::saturate_cast<uchar>(ivalue);
  410. return ivalue != -1 || !PyErr_Occurred();
  411. }
  412. template<>
  413. PyObject* pyopencv_from(const uchar& value)
  414. {
  415. return PyInt_FromLong(value);
  416. }
  417. // --- char
  418. template<>
  419. bool pyopencv_to(PyObject* obj, char& value, const ArgInfo& info)
  420. {
  421. if (!obj || obj == Py_None)
  422. {
  423. return true;
  424. }
  425. if (isBool(obj))
  426. {
  427. failmsg("Argument '%s' must be an integer, not bool", info.name);
  428. return false;
  429. }
  430. if (PyArray_IsIntegerScalar(obj))
  431. {
  432. value = saturate_cast<char>(PyArray_PyIntAsInt(obj));
  433. }
  434. else
  435. {
  436. failmsg("Argument '%s' is required to be an integer", info.name);
  437. return false;
  438. }
  439. return !CV_HAS_CONVERSION_ERROR(value);
  440. }
  441. // --- double
  442. template<>
  443. bool pyopencv_to(PyObject* obj, double& value, const ArgInfo& info)
  444. {
  445. if (!obj || obj == Py_None)
  446. {
  447. return true;
  448. }
  449. if (isBool(obj))
  450. {
  451. failmsg("Argument '%s' must be double, not bool", info.name);
  452. return false;
  453. }
  454. if (PyArray_IsPythonNumber(obj))
  455. {
  456. if (PyLong_Check(obj))
  457. {
  458. value = PyLong_AsDouble(obj);
  459. }
  460. else
  461. {
  462. value = PyFloat_AsDouble(obj);
  463. }
  464. }
  465. else if (PyArray_CheckScalar(obj))
  466. {
  467. const bool isParsed = parseNumpyScalar<double>(obj, value);
  468. if (!isParsed) {
  469. failmsg("Argument '%s' can not be safely parsed to 'double'", info.name);
  470. return false;
  471. }
  472. }
  473. else
  474. {
  475. failmsg("Argument '%s' can not be treated as a double", info.name);
  476. return false;
  477. }
  478. return !PyErr_Occurred();
  479. }
  480. template<>
  481. PyObject* pyopencv_from(const double& value)
  482. {
  483. return PyFloat_FromDouble(value);
  484. }
  485. // --- float
  486. template<>
  487. bool pyopencv_to(PyObject* obj, float& value, const ArgInfo& info)
  488. {
  489. if (!obj || obj == Py_None)
  490. {
  491. return true;
  492. }
  493. if (isBool(obj))
  494. {
  495. failmsg("Argument '%s' must be float, not bool", info.name);
  496. return false;
  497. }
  498. if (PyArray_IsPythonNumber(obj))
  499. {
  500. if (PyLong_Check(obj))
  501. {
  502. double res = PyLong_AsDouble(obj);
  503. value = static_cast<float>(res);
  504. }
  505. else
  506. {
  507. double res = PyFloat_AsDouble(obj);
  508. value = static_cast<float>(res);
  509. }
  510. }
  511. else if (PyArray_CheckScalar(obj))
  512. {
  513. const bool isParsed = parseNumpyScalar<float>(obj, value);
  514. if (!isParsed) {
  515. failmsg("Argument '%s' can not be safely parsed to 'float'", info.name);
  516. return false;
  517. }
  518. }
  519. else
  520. {
  521. failmsg("Argument '%s' can't be treated as a float", info.name);
  522. return false;
  523. }
  524. return !PyErr_Occurred();
  525. }
  526. template<>
  527. PyObject* pyopencv_from(const float& value)
  528. {
  529. return PyFloat_FromDouble(value);
  530. }
  531. // --- string
  532. template<>
  533. bool pyopencv_to(PyObject* obj, String &value, const ArgInfo& info)
  534. {
  535. if(!obj || obj == Py_None)
  536. {
  537. return true;
  538. }
  539. std::string str;
  540. if (getUnicodeString(obj, str))
  541. {
  542. value = str;
  543. return true;
  544. }
  545. else
  546. {
  547. // If error hasn't been already set by Python conversion functions
  548. if (!PyErr_Occurred())
  549. {
  550. // Direct access to underlying slots of PyObjectType is not allowed
  551. // when limited API is enabled
  552. #ifdef Py_LIMITED_API
  553. failmsg("Can't convert object to 'str' for '%s'", info.name);
  554. #else
  555. failmsg("Can't convert object of type '%s' to 'str' for '%s'",
  556. obj->ob_type->tp_name, info.name);
  557. #endif
  558. }
  559. }
  560. return false;
  561. }
  562. template<>
  563. PyObject* pyopencv_from(const String& value)
  564. {
  565. return PyString_FromString(value.empty() ? "" : value.c_str());
  566. }
  567. #if CV_VERSION_MAJOR == 3
  568. template<>
  569. PyObject* pyopencv_from(const std::string& value)
  570. {
  571. return PyString_FromString(value.empty() ? "" : value.c_str());
  572. }
  573. #endif
  574. // --- Size
  575. template<>
  576. bool pyopencv_to(PyObject* obj, Size& sz, const ArgInfo& info)
  577. {
  578. RefWrapper<int> values[] = {RefWrapper<int>(sz.width),
  579. RefWrapper<int>(sz.height)};
  580. return parseSequence(obj, values, info);
  581. }
  582. template<>
  583. PyObject* pyopencv_from(const Size& sz)
  584. {
  585. return Py_BuildValue("(ii)", sz.width, sz.height);
  586. }
  587. template<>
  588. bool pyopencv_to(PyObject* obj, Size_<float>& sz, const ArgInfo& info)
  589. {
  590. RefWrapper<float> values[] = {RefWrapper<float>(sz.width),
  591. RefWrapper<float>(sz.height)};
  592. return parseSequence(obj, values, info);
  593. }
  594. template<>
  595. PyObject* pyopencv_from(const Size_<float>& sz)
  596. {
  597. return Py_BuildValue("(ff)", sz.width, sz.height);
  598. }
  599. // --- Rect
  600. template<>
  601. bool pyopencv_to(PyObject* obj, Rect& r, const ArgInfo& info)
  602. {
  603. RefWrapper<int> values[] = {RefWrapper<int>(r.x), RefWrapper<int>(r.y),
  604. RefWrapper<int>(r.width),
  605. RefWrapper<int>(r.height)};
  606. return parseSequence(obj, values, info);
  607. }
  608. template<>
  609. PyObject* pyopencv_from(const Rect& r)
  610. {
  611. return Py_BuildValue("(iiii)", r.x, r.y, r.width, r.height);
  612. }
  613. template<>
  614. bool pyopencv_to(PyObject* obj, Rect2d& r, const ArgInfo& info)
  615. {
  616. RefWrapper<double> values[] = {
  617. RefWrapper<double>(r.x), RefWrapper<double>(r.y),
  618. RefWrapper<double>(r.width), RefWrapper<double>(r.height)};
  619. return parseSequence(obj, values, info);
  620. }
  621. template<>
  622. PyObject* pyopencv_from(const Rect2d& r)
  623. {
  624. return Py_BuildValue("(dddd)", r.x, r.y, r.width, r.height);
  625. }
  626. // --- RotatedRect
  627. template<>
  628. bool pyopencv_to(PyObject* obj, RotatedRect& dst, const ArgInfo& info)
  629. {
  630. if (!obj || obj == Py_None)
  631. {
  632. return true;
  633. }
  634. if (!PySequence_Check(obj))
  635. {
  636. failmsg("Can't parse '%s' as RotatedRect."
  637. "Input argument doesn't provide sequence protocol",
  638. info.name);
  639. return false;
  640. }
  641. const std::size_t sequenceSize = PySequence_Size(obj);
  642. if (sequenceSize != 3)
  643. {
  644. failmsg("Can't parse '%s' as RotatedRect. Expected sequence length 3, got %lu",
  645. info.name, sequenceSize);
  646. return false;
  647. }
  648. {
  649. const String centerItemName = format("'%s' center point", info.name);
  650. const ArgInfo centerItemInfo(centerItemName.c_str(), false);
  651. SafeSeqItem centerItem(obj, 0);
  652. if (!pyopencv_to(centerItem.item, dst.center, centerItemInfo))
  653. {
  654. return false;
  655. }
  656. }
  657. {
  658. const String sizeItemName = format("'%s' size", info.name);
  659. const ArgInfo sizeItemInfo(sizeItemName.c_str(), false);
  660. SafeSeqItem sizeItem(obj, 1);
  661. if (!pyopencv_to(sizeItem.item, dst.size, sizeItemInfo))
  662. {
  663. return false;
  664. }
  665. }
  666. {
  667. const String angleItemName = format("'%s' angle", info.name);
  668. const ArgInfo angleItemInfo(angleItemName.c_str(), false);
  669. SafeSeqItem angleItem(obj, 2);
  670. if (!pyopencv_to(angleItem.item, dst.angle, angleItemInfo))
  671. {
  672. return false;
  673. }
  674. }
  675. return true;
  676. }
  677. template<>
  678. PyObject* pyopencv_from(const RotatedRect& src)
  679. {
  680. return Py_BuildValue("((ff)(ff)f)", src.center.x, src.center.y, src.size.width, src.size.height, src.angle);
  681. }
  682. // --- Range
  683. template<>
  684. bool pyopencv_to(PyObject* obj, Range& r, const ArgInfo& info)
  685. {
  686. if (!obj || obj == Py_None)
  687. {
  688. return true;
  689. }
  690. if (PyObject_Size(obj) == 0)
  691. {
  692. r = Range::all();
  693. return true;
  694. }
  695. RefWrapper<int> values[] = {RefWrapper<int>(r.start), RefWrapper<int>(r.end)};
  696. return parseSequence(obj, values, info);
  697. }
  698. template<>
  699. PyObject* pyopencv_from(const Range& r)
  700. {
  701. return Py_BuildValue("(ii)", r.start, r.end);
  702. }
  703. // --- Point
  704. template<>
  705. bool pyopencv_to(PyObject* obj, Point& p, const ArgInfo& info)
  706. {
  707. RefWrapper<int> values[] = {RefWrapper<int>(p.x), RefWrapper<int>(p.y)};
  708. return parseSequence(obj, values, info);
  709. }
  710. template<>
  711. PyObject* pyopencv_from(const Point& p)
  712. {
  713. return Py_BuildValue("(ii)", p.x, p.y);
  714. }
  715. template <>
  716. bool pyopencv_to(PyObject* obj, Point2f& p, const ArgInfo& info)
  717. {
  718. RefWrapper<float> values[] = {RefWrapper<float>(p.x),
  719. RefWrapper<float>(p.y)};
  720. return parseSequence(obj, values, info);
  721. }
  722. template<>
  723. PyObject* pyopencv_from(const Point2f& p)
  724. {
  725. return Py_BuildValue("(dd)", p.x, p.y);
  726. }
  727. template<>
  728. bool pyopencv_to(PyObject* obj, Point2d& p, const ArgInfo& info)
  729. {
  730. RefWrapper<double> values[] = {RefWrapper<double>(p.x),
  731. RefWrapper<double>(p.y)};
  732. return parseSequence(obj, values, info);
  733. }
  734. template<>
  735. PyObject* pyopencv_from(const Point2d& p)
  736. {
  737. return Py_BuildValue("(dd)", p.x, p.y);
  738. }
  739. template<>
  740. bool pyopencv_to(PyObject* obj, Point3f& p, const ArgInfo& info)
  741. {
  742. RefWrapper<float> values[] = {RefWrapper<float>(p.x),
  743. RefWrapper<float>(p.y),
  744. RefWrapper<float>(p.z)};
  745. return parseSequence(obj, values, info);
  746. }
  747. template<>
  748. PyObject* pyopencv_from(const Point3f& p)
  749. {
  750. return Py_BuildValue("(ddd)", p.x, p.y, p.z);
  751. }
  752. template<>
  753. bool pyopencv_to(PyObject* obj, Point3d& p, const ArgInfo& info)
  754. {
  755. RefWrapper<double> values[] = {RefWrapper<double>(p.x),
  756. RefWrapper<double>(p.y),
  757. RefWrapper<double>(p.z)};
  758. return parseSequence(obj, values, info);
  759. }
  760. template<>
  761. PyObject* pyopencv_from(const Point3d& p)
  762. {
  763. return Py_BuildValue("(ddd)", p.x, p.y, p.z);
  764. }
  765. // --- Vec
  766. bool pyopencv_to(PyObject* obj, Vec4d& v, ArgInfo& info)
  767. {
  768. RefWrapper<double> values[] = {RefWrapper<double>(v[0]), RefWrapper<double>(v[1]),
  769. RefWrapper<double>(v[2]), RefWrapper<double>(v[3])};
  770. return parseSequence(obj, values, info);
  771. }
  772. PyObject* pyopencv_from(const Vec4d& v)
  773. {
  774. return Py_BuildValue("(dddd)", v[0], v[1], v[2], v[3]);
  775. }
  776. bool pyopencv_to(PyObject* obj, Vec4f& v, ArgInfo& info)
  777. {
  778. RefWrapper<float> values[] = {RefWrapper<float>(v[0]), RefWrapper<float>(v[1]),
  779. RefWrapper<float>(v[2]), RefWrapper<float>(v[3])};
  780. return parseSequence(obj, values, info);
  781. }
  782. PyObject* pyopencv_from(const Vec4f& v)
  783. {
  784. return Py_BuildValue("(ffff)", v[0], v[1], v[2], v[3]);
  785. }
  786. bool pyopencv_to(PyObject* obj, Vec4i& v, ArgInfo& info)
  787. {
  788. RefWrapper<int> values[] = {RefWrapper<int>(v[0]), RefWrapper<int>(v[1]),
  789. RefWrapper<int>(v[2]), RefWrapper<int>(v[3])};
  790. return parseSequence(obj, values, info);
  791. }
  792. PyObject* pyopencv_from(const Vec4i& v)
  793. {
  794. return Py_BuildValue("(iiii)", v[0], v[1], v[2], v[3]);
  795. }
  796. bool pyopencv_to(PyObject* obj, Vec3d& v, ArgInfo& info)
  797. {
  798. RefWrapper<double> values[] = {RefWrapper<double>(v[0]),
  799. RefWrapper<double>(v[1]),
  800. RefWrapper<double>(v[2])};
  801. return parseSequence(obj, values, info);
  802. }
  803. PyObject* pyopencv_from(const Vec3d& v)
  804. {
  805. return Py_BuildValue("(ddd)", v[0], v[1], v[2]);
  806. }
  807. bool pyopencv_to(PyObject* obj, Vec3f& v, ArgInfo& info)
  808. {
  809. RefWrapper<float> values[] = {RefWrapper<float>(v[0]),
  810. RefWrapper<float>(v[1]),
  811. RefWrapper<float>(v[2])};
  812. return parseSequence(obj, values, info);
  813. }
  814. PyObject* pyopencv_from(const Vec3f& v)
  815. {
  816. return Py_BuildValue("(fff)", v[0], v[1], v[2]);
  817. }
  818. bool pyopencv_to(PyObject* obj, Vec3i& v, ArgInfo& info)
  819. {
  820. RefWrapper<int> values[] = {RefWrapper<int>(v[0]), RefWrapper<int>(v[1]),
  821. RefWrapper<int>(v[2])};
  822. return parseSequence(obj, values, info);
  823. }
  824. PyObject* pyopencv_from(const Vec3i& v)
  825. {
  826. return Py_BuildValue("(iii)", v[0], v[1], v[2]);
  827. }
  828. bool pyopencv_to(PyObject* obj, Vec2d& v, ArgInfo& info)
  829. {
  830. RefWrapper<double> values[] = {RefWrapper<double>(v[0]),
  831. RefWrapper<double>(v[1])};
  832. return parseSequence(obj, values, info);
  833. }
  834. PyObject* pyopencv_from(const Vec2d& v)
  835. {
  836. return Py_BuildValue("(dd)", v[0], v[1]);
  837. }
  838. bool pyopencv_to(PyObject* obj, Vec2f& v, ArgInfo& info)
  839. {
  840. RefWrapper<float> values[] = {RefWrapper<float>(v[0]),
  841. RefWrapper<float>(v[1])};
  842. return parseSequence(obj, values, info);
  843. }
  844. PyObject* pyopencv_from(const Vec2f& v)
  845. {
  846. return Py_BuildValue("(ff)", v[0], v[1]);
  847. }
  848. bool pyopencv_to(PyObject* obj, Vec2i& v, ArgInfo& info)
  849. {
  850. RefWrapper<int> values[] = {RefWrapper<int>(v[0]), RefWrapper<int>(v[1])};
  851. return parseSequence(obj, values, info);
  852. }
  853. PyObject* pyopencv_from(const Vec2i& v)
  854. {
  855. return Py_BuildValue("(ii)", v[0], v[1]);
  856. }
  857. // --- TermCriteria
  858. template<>
  859. bool pyopencv_to(PyObject* obj, TermCriteria& dst, const ArgInfo& info)
  860. {
  861. if (!obj || obj == Py_None)
  862. {
  863. return true;
  864. }
  865. if (!PySequence_Check(obj))
  866. {
  867. failmsg("Can't parse '%s' as TermCriteria."
  868. "Input argument doesn't provide sequence protocol",
  869. info.name);
  870. return false;
  871. }
  872. const std::size_t sequenceSize = PySequence_Size(obj);
  873. if (sequenceSize != 3) {
  874. failmsg("Can't parse '%s' as TermCriteria. Expected sequence length 3, "
  875. "got %lu",
  876. info.name, sequenceSize);
  877. return false;
  878. }
  879. {
  880. const String typeItemName = format("'%s' criteria type", info.name);
  881. const ArgInfo typeItemInfo(typeItemName.c_str(), false);
  882. SafeSeqItem typeItem(obj, 0);
  883. if (!pyopencv_to(typeItem.item, dst.type, typeItemInfo))
  884. {
  885. return false;
  886. }
  887. }
  888. {
  889. const String maxCountItemName = format("'%s' max count", info.name);
  890. const ArgInfo maxCountItemInfo(maxCountItemName.c_str(), false);
  891. SafeSeqItem maxCountItem(obj, 1);
  892. if (!pyopencv_to(maxCountItem.item, dst.maxCount, maxCountItemInfo))
  893. {
  894. return false;
  895. }
  896. }
  897. {
  898. const String epsilonItemName = format("'%s' epsilon", info.name);
  899. const ArgInfo epsilonItemInfo(epsilonItemName.c_str(), false);
  900. SafeSeqItem epsilonItem(obj, 2);
  901. if (!pyopencv_to(epsilonItem.item, dst.epsilon, epsilonItemInfo))
  902. {
  903. return false;
  904. }
  905. }
  906. return true;
  907. }
  908. template<>
  909. PyObject* pyopencv_from(const TermCriteria& src)
  910. {
  911. return Py_BuildValue("(iid)", src.type, src.maxCount, src.epsilon);
  912. }
  913. // --- Moments
  914. template<>
  915. PyObject* pyopencv_from(const Moments& m)
  916. {
  917. return Py_BuildValue("{s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d}",
  918. "m00", m.m00, "m10", m.m10, "m01", m.m01,
  919. "m20", m.m20, "m11", m.m11, "m02", m.m02,
  920. "m30", m.m30, "m21", m.m21, "m12", m.m12, "m03", m.m03,
  921. "mu20", m.mu20, "mu11", m.mu11, "mu02", m.mu02,
  922. "mu30", m.mu30, "mu21", m.mu21, "mu12", m.mu12, "mu03", m.mu03,
  923. "nu20", m.nu20, "nu11", m.nu11, "nu02", m.nu02,
  924. "nu30", m.nu30, "nu21", m.nu21, "nu12", m.nu12, "nu03", m.nu03);
  925. }
  926. // --- pair
  927. template<>
  928. PyObject* pyopencv_from(const std::pair<int, double>& src)
  929. {
  930. return Py_BuildValue("(id)", src.first, src.second);
  931. }