From 9b959072a20ccf1af8693db04e200f72713f7971 Mon Sep 17 00:00:00 2001 From: Matthew Skolaut Date: Mon, 20 Jun 2016 16:24:15 -0500 Subject: [PATCH 1/3] added python binding for createButton --- modules/python/src2/cv2.cpp | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 3bb98e4f8c..eb0d4c998c 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -1251,6 +1251,7 @@ static void OnChange(int pos, void *param) } #ifdef HAVE_OPENCV_HIGHGUI + static PyObject *pycvCreateTrackbar(PyObject*, PyObject *args) { PyObject *on_change; @@ -1270,6 +1271,55 @@ static PyObject *pycvCreateTrackbar(PyObject*, PyObject *args) } #endif +static void OnButtonChange(int state, void *param) +{ + PyGILState_STATE gstate; + gstate = PyGILState_Ensure(); + + PyObject *o = (PyObject*)param; + PyObject *args; + if(PyTuple_GetItem(o, 1) != NULL) + { + args = Py_BuildValue("(iO)", state, PyTuple_GetItem(o,1)); + } + else + { + args = Py_BuildValue("(i)", state); + } + + PyObject *r = PyObject_Call(PyTuple_GetItem(o, 0), args, NULL); + if (r == NULL) + PyErr_Print(); + Py_DECREF(args); + PyGILState_Release(gstate); +} + +#ifdef HAVE_OPENCV_HIGHGUI + +static PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw) +{ + const char* keywords[] = {"buttonName", "onChange", "userData", "buttonType", "initialButtonState", NULL}; + PyObject *on_change; + PyObject *userdata = NULL; + char* button_name; + int button_type = 0; + int initial_button_state = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|Oii", (char**)keywords, &button_name, &on_change, &userdata, &button_type, &initial_button_state)) + return NULL; + if (!PyCallable_Check(on_change)) { + PyErr_SetString(PyExc_TypeError, "onChange must be callable"); + return NULL; + } + if (userdata == NULL) { + userdata = Py_None; + } + + ERRWRAP2(createButton(button_name, OnButtonChange, Py_BuildValue("OO", on_change, userdata), button_type, initial_button_state)); + Py_RETURN_NONE; +} +#endif + /////////////////////////////////////////////////////////////////////////////////////// static int convert_to_char(PyObject *o, char *dst, const char *name = "no_name") @@ -1300,6 +1350,7 @@ static int convert_to_char(PyObject *o, char *dst, const char *name = "no_name") static PyMethodDef special_methods[] = { #ifdef HAVE_OPENCV_HIGHGUI {"createTrackbar", pycvCreateTrackbar, METH_VARARGS, "createTrackbar(trackbarName, windowName, value, count, onChange) -> None"}, + {"createButton", (PyCFunction)pycvCreateButton, METH_VARARGS | METH_KEYWORDS, "createButton(buttonName, onChange [, userData, buttonType, initialButtonState]) -> None"}, {"setMouseCallback", (PyCFunction)pycvSetMouseCallback, METH_VARARGS | METH_KEYWORDS, "setMouseCallback(windowName, onMouse [, param]) -> None"}, #endif {NULL, NULL}, From 7284a77cd35c81947a9ce3c8f95ab4fef4952ae6 Mon Sep 17 00:00:00 2001 From: Matthew Skolaut Date: Mon, 20 Jun 2016 21:07:24 -0500 Subject: [PATCH 2/3] fix casting warning in python createButton binding --- modules/python/src2/cv2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index eb0d4c998c..537e85422b 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -1303,7 +1303,7 @@ static PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw) PyObject *userdata = NULL; char* button_name; int button_type = 0; - int initial_button_state = 0; + bool initial_button_state = false; if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|Oii", (char**)keywords, &button_name, &on_change, &userdata, &button_type, &initial_button_state)) return NULL; From f861d0d64377b76a7ebf7d9369c6d40f706578da Mon Sep 17 00:00:00 2001 From: Matthew Skolaut Date: Tue, 21 Jun 2016 17:16:16 -0500 Subject: [PATCH 3/3] merge #ifs in highgui bindings --- modules/python/src2/cv2.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 537e85422b..4b07e60fa4 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -1251,7 +1251,6 @@ static void OnChange(int pos, void *param) } #ifdef HAVE_OPENCV_HIGHGUI - static PyObject *pycvCreateTrackbar(PyObject*, PyObject *args) { PyObject *on_change; @@ -1269,7 +1268,6 @@ static PyObject *pycvCreateTrackbar(PyObject*, PyObject *args) ERRWRAP2(createTrackbar(trackbar_name, window_name, value, count, OnChange, Py_BuildValue("OO", on_change, Py_None))); Py_RETURN_NONE; } -#endif static void OnButtonChange(int state, void *param) { @@ -1294,8 +1292,6 @@ static void OnButtonChange(int state, void *param) PyGILState_Release(gstate); } -#ifdef HAVE_OPENCV_HIGHGUI - static PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw) { const char* keywords[] = {"buttonName", "onChange", "userData", "buttonType", "initialButtonState", NULL};