Skip to content

Commit 58a83e9

Browse files
committed
Merge pull request opencv#9839 from terfendail:features/python_wrapper/surface_matching_cleanup
2 parents bc93775 + 868ad16 commit 58a83e9

File tree

1 file changed

+182
-9
lines changed

1 file changed

+182
-9
lines changed

modules/python/src2/cv2.cpp

Lines changed: 182 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,24 @@ bool pyopencv_to(PyObject* o, Mat& m, const char* name)
396396
return pyopencv_to(o, m, ArgInfo(name, 0));
397397
}
398398

399+
template<typename _Tp, int m, int n>
400+
bool pyopencv_to(PyObject* o, Matx<_Tp, m, n>& mx, const ArgInfo info)
401+
{
402+
Mat tmp;
403+
if (!pyopencv_to(o, tmp, info)) {
404+
return false;
405+
}
406+
407+
tmp.copyTo(mx);
408+
return true;
409+
}
410+
411+
template<typename _Tp, int m, int n>
412+
bool pyopencv_to(PyObject* o, Matx<_Tp, m, n>& mx, const char* name)
413+
{
414+
return pyopencv_to(o, mx, ArgInfo(name, 0));
415+
}
416+
399417
template <typename T>
400418
bool pyopencv_to(PyObject *o, Ptr<T>& p, const char *name)
401419
{
@@ -683,26 +701,25 @@ PyObject* pyopencv_from(const UMat& m) {
683701
return o;
684702
}
685703

686-
template<>
687-
bool pyopencv_to(PyObject *o, Scalar& s, const char *name)
704+
static bool pyopencv_to(PyObject *o, Scalar& s, const ArgInfo info)
688705
{
689706
if(!o || o == Py_None)
690707
return true;
691708
if (PySequence_Check(o)) {
692-
PyObject *fi = PySequence_Fast(o, name);
709+
PyObject *fi = PySequence_Fast(o, info.name);
693710
if (fi == NULL)
694711
return false;
695712
if (4 < PySequence_Fast_GET_SIZE(fi))
696713
{
697-
failmsg("Scalar value for argument '%s' is longer than 4", name);
714+
failmsg("Scalar value for argument '%s' is longer than 4", info.name);
698715
return false;
699716
}
700717
for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(fi); i++) {
701718
PyObject *item = PySequence_Fast_GET_ITEM(fi, i);
702719
if (PyFloat_Check(item) || PyInt_Check(item)) {
703720
s[(int)i] = PyFloat_AsDouble(item);
704721
} else {
705-
failmsg("Scalar value for argument '%s' is not numeric", name);
722+
failmsg("Scalar value for argument '%s' is not numeric", info.name);
706723
return false;
707724
}
708725
}
@@ -711,13 +728,19 @@ bool pyopencv_to(PyObject *o, Scalar& s, const char *name)
711728
if (PyFloat_Check(o) || PyInt_Check(o)) {
712729
s[0] = PyFloat_AsDouble(o);
713730
} else {
714-
failmsg("Scalar value for argument '%s' is not numeric", name);
731+
failmsg("Scalar value for argument '%s' is not numeric", info.name);
715732
return false;
716733
}
717734
}
718735
return true;
719736
}
720737

738+
template<>
739+
bool pyopencv_to(PyObject *o, Scalar& s, const char *name)
740+
{
741+
return pyopencv_to(o, s, ArgInfo(name, 0));
742+
}
743+
721744
template<>
722745
PyObject* pyopencv_from(const Scalar& src)
723746
{
@@ -1024,27 +1047,177 @@ PyObject* pyopencv_from(const Point3f& p)
10241047
return Py_BuildValue("(ddd)", p.x, p.y, p.z);
10251048
}
10261049

1050+
static bool pyopencv_to(PyObject* obj, Vec4d& v, ArgInfo info)
1051+
{
1052+
(void)info;
1053+
if (!obj)
1054+
return true;
1055+
return PyArg_ParseTuple(obj, "dddd", &v[0], &v[1], &v[2], &v[3]) > 0;
1056+
}
10271057
template<>
1028-
bool pyopencv_to(PyObject* obj, Vec3d& v, const char* name)
1058+
bool pyopencv_to(PyObject* obj, Vec4d& v, const char* name)
10291059
{
1030-
(void)name;
1031-
if(!obj)
1060+
return pyopencv_to(obj, v, ArgInfo(name, 0));
1061+
}
1062+
1063+
static bool pyopencv_to(PyObject* obj, Vec4f& v, ArgInfo info)
1064+
{
1065+
(void)info;
1066+
if (!obj)
1067+
return true;
1068+
return PyArg_ParseTuple(obj, "ffff", &v[0], &v[1], &v[2], &v[3]) > 0;
1069+
}
1070+
template<>
1071+
bool pyopencv_to(PyObject* obj, Vec4f& v, const char* name)
1072+
{
1073+
return pyopencv_to(obj, v, ArgInfo(name, 0));
1074+
}
1075+
1076+
static bool pyopencv_to(PyObject* obj, Vec4i& v, ArgInfo info)
1077+
{
1078+
(void)info;
1079+
if (!obj)
1080+
return true;
1081+
return PyArg_ParseTuple(obj, "iiii", &v[0], &v[1], &v[2], &v[3]) > 0;
1082+
}
1083+
template<>
1084+
bool pyopencv_to(PyObject* obj, Vec4i& v, const char* name)
1085+
{
1086+
return pyopencv_to(obj, v, ArgInfo(name, 0));
1087+
}
1088+
1089+
static bool pyopencv_to(PyObject* obj, Vec3d& v, ArgInfo info)
1090+
{
1091+
(void)info;
1092+
if (!obj)
10321093
return true;
10331094
return PyArg_ParseTuple(obj, "ddd", &v[0], &v[1], &v[2]) > 0;
10341095
}
1096+
template<>
1097+
bool pyopencv_to(PyObject* obj, Vec3d& v, const char* name)
1098+
{
1099+
return pyopencv_to(obj, v, ArgInfo(name, 0));
1100+
}
1101+
1102+
static bool pyopencv_to(PyObject* obj, Vec3f& v, ArgInfo info)
1103+
{
1104+
(void)info;
1105+
if (!obj)
1106+
return true;
1107+
return PyArg_ParseTuple(obj, "fff", &v[0], &v[1], &v[2]) > 0;
1108+
}
1109+
template<>
1110+
bool pyopencv_to(PyObject* obj, Vec3f& v, const char* name)
1111+
{
1112+
return pyopencv_to(obj, v, ArgInfo(name, 0));
1113+
}
1114+
1115+
static bool pyopencv_to(PyObject* obj, Vec3i& v, ArgInfo info)
1116+
{
1117+
(void)info;
1118+
if (!obj)
1119+
return true;
1120+
return PyArg_ParseTuple(obj, "iii", &v[0], &v[1], &v[2]) > 0;
1121+
}
1122+
template<>
1123+
bool pyopencv_to(PyObject* obj, Vec3i& v, const char* name)
1124+
{
1125+
return pyopencv_to(obj, v, ArgInfo(name, 0));
1126+
}
1127+
1128+
static bool pyopencv_to(PyObject* obj, Vec2d& v, ArgInfo info)
1129+
{
1130+
(void)info;
1131+
if (!obj)
1132+
return true;
1133+
return PyArg_ParseTuple(obj, "dd", &v[0], &v[1]) > 0;
1134+
}
1135+
template<>
1136+
bool pyopencv_to(PyObject* obj, Vec2d& v, const char* name)
1137+
{
1138+
return pyopencv_to(obj, v, ArgInfo(name, 0));
1139+
}
1140+
1141+
static bool pyopencv_to(PyObject* obj, Vec2f& v, ArgInfo info)
1142+
{
1143+
(void)info;
1144+
if (!obj)
1145+
return true;
1146+
return PyArg_ParseTuple(obj, "ff", &v[0], &v[1]) > 0;
1147+
}
1148+
template<>
1149+
bool pyopencv_to(PyObject* obj, Vec2f& v, const char* name)
1150+
{
1151+
return pyopencv_to(obj, v, ArgInfo(name, 0));
1152+
}
1153+
1154+
static bool pyopencv_to(PyObject* obj, Vec2i& v, ArgInfo info)
1155+
{
1156+
(void)info;
1157+
if (!obj)
1158+
return true;
1159+
return PyArg_ParseTuple(obj, "ii", &v[0], &v[1]) > 0;
1160+
}
1161+
template<>
1162+
bool pyopencv_to(PyObject* obj, Vec2i& v, const char* name)
1163+
{
1164+
return pyopencv_to(obj, v, ArgInfo(name, 0));
1165+
}
1166+
1167+
template<>
1168+
PyObject* pyopencv_from(const Vec4d& v)
1169+
{
1170+
return Py_BuildValue("(dddd)", v[0], v[1], v[2], v[3]);
1171+
}
1172+
1173+
template<>
1174+
PyObject* pyopencv_from(const Vec4f& v)
1175+
{
1176+
return Py_BuildValue("(ffff)", v[0], v[1], v[2], v[3]);
1177+
}
1178+
1179+
template<>
1180+
PyObject* pyopencv_from(const Vec4i& v)
1181+
{
1182+
return Py_BuildValue("(iiii)", v[0], v[1], v[2], v[3]);
1183+
}
10351184

10361185
template<>
10371186
PyObject* pyopencv_from(const Vec3d& v)
10381187
{
10391188
return Py_BuildValue("(ddd)", v[0], v[1], v[2]);
10401189
}
10411190

1191+
template<>
1192+
PyObject* pyopencv_from(const Vec3f& v)
1193+
{
1194+
return Py_BuildValue("(fff)", v[0], v[1], v[2]);
1195+
}
1196+
1197+
template<>
1198+
PyObject* pyopencv_from(const Vec3i& v)
1199+
{
1200+
return Py_BuildValue("(iii)", v[0], v[1], v[2]);
1201+
}
1202+
10421203
template<>
10431204
PyObject* pyopencv_from(const Vec2d& v)
10441205
{
10451206
return Py_BuildValue("(dd)", v[0], v[1]);
10461207
}
10471208

1209+
template<>
1210+
PyObject* pyopencv_from(const Vec2f& v)
1211+
{
1212+
return Py_BuildValue("(ff)", v[0], v[1]);
1213+
}
1214+
1215+
template<>
1216+
PyObject* pyopencv_from(const Vec2i& v)
1217+
{
1218+
return Py_BuildValue("(ii)", v[0], v[1]);
1219+
}
1220+
10481221
template<>
10491222
PyObject* pyopencv_from(const Point2d& p)
10501223
{

0 commit comments

Comments
 (0)