Skip to content

Adding option to support extent parameter in imshow #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions matplotlibcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ bool hist(const std::vector<Numeric>& 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<std::string, std::string> &keywords, PyObject** out)
inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords, PyObject** out, const std::vector<double>& extent = {})
{
assert(type == NPY_UINT8 || type == NPY_FLOAT);
assert(colors == 1 || colors == 3 || colors == 4);
Expand All @@ -818,6 +818,19 @@ 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);
Py_DECREF(extentList);
}

// take care of the remaining keywords
for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
{
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
Expand All @@ -836,18 +849,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<std::string, std::string> &keywords = {}, PyObject** out = nullptr)
inline void imshow(const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {}, PyObject** out = nullptr, const std::vector<double>& 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<std::string, std::string> &keywords = {}, PyObject** out = nullptr)
inline void imshow(const float *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {}, PyObject** out = nullptr, const std::vector<double>& 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<std::string, std::string> &keywords = {})
inline void imshow(const cv::Mat &image, const std::map<std::string, std::string> &keywords = {}, PyObject** out = nullptr, const std::vector<double>& extent = {})
{
// Convert underlying type of matrix, if needed
cv::Mat image2;
Expand All @@ -873,7 +886,7 @@ void imshow(const cv::Mat &image, const std::map<std::string, std::string> &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
Expand Down