pyopencv_flann.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifdef HAVE_OPENCV_FLANN
  2. typedef cvflann::flann_distance_t cvflann_flann_distance_t;
  3. typedef cvflann::flann_algorithm_t cvflann_flann_algorithm_t;
  4. template<>
  5. PyObject* pyopencv_from(const cvflann_flann_algorithm_t& value)
  6. {
  7. return PyInt_FromLong(int(value));
  8. }
  9. template<>
  10. PyObject* pyopencv_from(const cvflann_flann_distance_t& value)
  11. {
  12. return PyInt_FromLong(int(value));
  13. }
  14. template<>
  15. bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const ArgInfo& info)
  16. {
  17. CV_UNUSED(info);
  18. bool ok = true;
  19. PyObject* key = NULL;
  20. PyObject* item = NULL;
  21. Py_ssize_t pos = 0;
  22. if (!o || o == Py_None)
  23. return true;
  24. if(PyDict_Check(o)) {
  25. while(PyDict_Next(o, &pos, &key, &item))
  26. {
  27. // get key
  28. std::string k;
  29. if (!getUnicodeString(key, k))
  30. {
  31. ok = false;
  32. break;
  33. }
  34. // get value
  35. if( !!PyBool_Check(item) )
  36. {
  37. p.setBool(k, item == Py_True);
  38. }
  39. else if( PyInt_Check(item) )
  40. {
  41. int value = (int)PyInt_AsLong(item);
  42. if( strcmp(k.c_str(), "algorithm") == 0 )
  43. p.setAlgorithm(value);
  44. else
  45. p.setInt(k, value);
  46. }
  47. else if( PyFloat_Check(item) )
  48. {
  49. double value = PyFloat_AsDouble(item);
  50. p.setDouble(k, value);
  51. }
  52. else
  53. {
  54. std::string val_str;
  55. if (!getUnicodeString(item, val_str))
  56. {
  57. ok = false;
  58. break;
  59. }
  60. p.setString(k, val_str);
  61. }
  62. }
  63. }
  64. return ok && !PyErr_Occurred();
  65. }
  66. template<>
  67. bool pyopencv_to(PyObject* obj, cv::flann::SearchParams & value, const ArgInfo& info)
  68. {
  69. return pyopencv_to<cv::flann::IndexParams>(obj, value, info);
  70. }
  71. template<>
  72. bool pyopencv_to(PyObject *o, cvflann::flann_distance_t& dist, const ArgInfo& info)
  73. {
  74. int d = (int)dist;
  75. bool ok = pyopencv_to(o, d, info);
  76. dist = (cvflann::flann_distance_t)d;
  77. return ok;
  78. }
  79. #endif