From 6c650bc7821ee7d5343d0a18fe6d8f4fabbeaa29 Mon Sep 17 00:00:00 2001 From: "raphael.viards@magellium.fr" Date: Fri, 27 Nov 2020 17:21:41 +0100 Subject: [PATCH 1/2] Adding option to support extent param in imshow --- matplotlibcpp.h | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 93a72be5..d8e2af6b 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -804,7 +804,7 @@ bool hist(const std::vector& y, long bins=10,std::string color="b", #ifndef WITHOUT_NUMPY namespace detail { -inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int columns, const int colors, const std::map &keywords, PyObject** out) +inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int columns, const int colors, const std::map &keywords, PyObject** out, const std::vector& extent = {}) { assert(type == NPY_UINT8 || type == NPY_FLOAT); assert(colors == 1 || colors == 3 || colors == 4); @@ -818,6 +818,18 @@ inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int co // construct keyword args PyObject* kwargs = PyDict_New(); + + // kwargs needs the extent + if (!extent.empty() && extent.size() == 4) { + PyObject* extentList = PyList_New(extent.size()); + for(size_t i = 0; i < extent.size(); ++i) { + PyList_SetItem(extentList, i, PyFloat_FromDouble(extent.at(i))); + } + + PyDict_SetItemString(kwargs, "extent", extentList); + } + + // take care of the remaining keywords for(std::map::const_iterator it = keywords.begin(); it != keywords.end(); ++it) { PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str())); @@ -836,18 +848,18 @@ inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int co } // namespace detail -inline void imshow(const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map &keywords = {}, PyObject** out = nullptr) +inline void imshow(const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map &keywords = {}, PyObject** out = nullptr, const std::vector& extent = {}) { - detail::imshow((void *) ptr, NPY_UINT8, rows, columns, colors, keywords, out); + detail::imshow((void *) ptr, NPY_UINT8, rows, columns, colors, keywords, out, extent); } -inline void imshow(const float *ptr, const int rows, const int columns, const int colors, const std::map &keywords = {}, PyObject** out = nullptr) +inline void imshow(const float *ptr, const int rows, const int columns, const int colors, const std::map &keywords = {}, PyObject** out = nullptr, const std::vector& extent = {}) { - detail::imshow((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords, out); + detail::imshow((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords, out, extent); } #ifdef WITH_OPENCV -void imshow(const cv::Mat &image, const std::map &keywords = {}) +inline void imshow(const cv::Mat &image, const std::map &keywords = {}, PyObject** out = nullptr, const std::vector& extent = {}) { // Convert underlying type of matrix, if needed cv::Mat image2; @@ -873,7 +885,7 @@ void imshow(const cv::Mat &image, const std::map &keyw cv::cvtColor(image2, image2, CV_BGRA2RGBA); } - detail::imshow(image2.data, npy_type, image2.rows, image2.cols, image2.channels(), keywords); + detail::imshow(image2.data, npy_type, image2.rows, image2.cols, image2.channels(), keywords, out, extent); } #endif // WITH_OPENCV #endif // WITHOUT_NUMPY From 6e81ea72c8d5ab06a6888d7e01f906d21a0d883c Mon Sep 17 00:00:00 2001 From: "raphael.viards@magellium.fr" Date: Fri, 27 Nov 2020 17:31:43 +0100 Subject: [PATCH 2/2] [FIX] Add missing Py_DECREF --- matplotlibcpp.h | 1 + 1 file changed, 1 insertion(+) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index d8e2af6b..1766c11a 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -827,6 +827,7 @@ inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int co } PyDict_SetItemString(kwargs, "extent", extentList); + Py_DECREF(extentList); } // take care of the remaining keywords