Skip to content

Commit

Permalink
Merge pull request opencv#11350 from alalek:fix_11348
Browse files Browse the repository at this point in the history
  • Loading branch information
vpisarev committed Apr 23, 2018
2 parents a312380 + 21026bf commit b8a6bfb
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions modules/python/src2/cv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "pycompat.hpp"

#include <map>

static PyObject* opencv_error = 0;

Expand Down Expand Up @@ -1621,13 +1622,19 @@ static PyObject *pycvSetMouseCallback(PyObject*, PyObject *args, PyObject *kw)
if (param == NULL) {
param = Py_None;
}
static PyObject* last_param = NULL;
if (last_param) {
Py_DECREF(last_param);
last_param = NULL;
PyObject* py_callback_info = Py_BuildValue("OO", on_mouse, param);
static std::map<std::string, PyObject*> registered_callbacks;
std::map<std::string, PyObject*>::iterator i = registered_callbacks.find(name);
if (i != registered_callbacks.end())
{
Py_DECREF(i->second);
i->second = py_callback_info;
}
else
{
registered_callbacks.insert(std::pair<std::string, PyObject*>(std::string(name), py_callback_info));
}
last_param = Py_BuildValue("OO", on_mouse, param);
ERRWRAP2(setMouseCallback(name, OnMouse, last_param));
ERRWRAP2(setMouseCallback(name, OnMouse, py_callback_info));
Py_RETURN_NONE;
}
#endif
Expand Down Expand Up @@ -1663,13 +1670,20 @@ static PyObject *pycvCreateTrackbar(PyObject*, PyObject *args)
PyErr_SetString(PyExc_TypeError, "on_change must be callable");
return NULL;
}
static PyObject* last_param = NULL;
if (last_param) {
Py_DECREF(last_param);
last_param = NULL;
PyObject* py_callback_info = Py_BuildValue("OO", on_change, Py_None);
std::string name = std::string(window_name) + ":" + std::string(trackbar_name);
static std::map<std::string, PyObject*> registered_callbacks;
std::map<std::string, PyObject*>::iterator i = registered_callbacks.find(name);
if (i != registered_callbacks.end())
{
Py_DECREF(i->second);
i->second = py_callback_info;
}
last_param = Py_BuildValue("OO", on_change, Py_None);
ERRWRAP2(createTrackbar(trackbar_name, window_name, value, count, OnChange, last_param));
else
{
registered_callbacks.insert(std::pair<std::string, PyObject*>(name, py_callback_info));
}
ERRWRAP2(createTrackbar(trackbar_name, window_name, value, count, OnChange, py_callback_info));
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -1717,13 +1731,21 @@ static PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw)
userdata = Py_None;
}

static PyObject* last_param = NULL;
if (last_param) {
Py_DECREF(last_param);
last_param = NULL;
PyObject* py_callback_info = Py_BuildValue("OO", on_change, userdata);
std::string name(button_name);

static std::map<std::string, PyObject*> registered_callbacks;
std::map<std::string, PyObject*>::iterator i = registered_callbacks.find(name);
if (i != registered_callbacks.end())
{
Py_DECREF(i->second);
i->second = py_callback_info;
}
else
{
registered_callbacks.insert(std::pair<std::string, PyObject*>(name, py_callback_info));
}
last_param = Py_BuildValue("OO", on_change, userdata);
ERRWRAP2(createButton(button_name, OnButtonChange, last_param, button_type, initial_button_state != 0));
ERRWRAP2(createButton(button_name, OnButtonChange, py_callback_info, button_type, initial_button_state != 0));
Py_RETURN_NONE;
}
#endif
Expand Down

0 comments on commit b8a6bfb

Please sign in to comment.