1515#ifndef WITHOUT_NUMPY
1616# define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
1717# include < numpy/arrayobject.h>
18+
19+ # ifdef WITH_OPENCV
20+ # include < opencv2/opencv.hpp>
21+ # endif // WITH_OPENCV
1822#endif // WITHOUT_NUMPY
1923
2024#if PY_MAJOR_VERSION >= 3
@@ -645,7 +649,7 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
645649 // construct args
646650 npy_intp dims[3 ] = { rows, columns, colors };
647651 PyObject *args = PyTuple_New (1 );
648- PyTuple_SetItem (args, 0 , PyArray_SimpleNewFromData (3 , dims, type, ptr));
652+ PyTuple_SetItem (args, 0 , PyArray_SimpleNewFromData (colors == 1 ? 2 : 3 , dims, type, ptr));
649653
650654 // construct keyword args
651655 PyObject* kwargs = PyDict_New ();
@@ -672,6 +676,37 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
672676 {
673677 internal::imshow ((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords);
674678 }
679+
680+ #ifdef WITH_OPENCV
681+ void imshow (const cv::Mat &image, const std::map<std::string, std::string> &keywords = {})
682+ {
683+ // Convert underlying type of matrix, if needed
684+ cv::Mat image2;
685+ NPY_TYPES npy_type = NPY_UINT8;
686+ switch (image.type () & CV_MAT_DEPTH_MASK) {
687+ case CV_8U:
688+ image2 = image;
689+ break ;
690+ case CV_32F:
691+ image2 = image;
692+ npy_type = NPY_FLOAT;
693+ break ;
694+ default :
695+ image.convertTo (image2, CV_MAKETYPE (CV_8U, image.channels ()));
696+ }
697+
698+ // If color image, convert from BGR to RGB
699+ switch (image2.channels ()) {
700+ case 3 :
701+ cv::cvtColor (image2, image2, CV_BGR2RGB);
702+ break ;
703+ case 4 :
704+ cv::cvtColor (image2, image2, CV_BGRA2RGBA);
705+ }
706+
707+ internal::imshow (image2.data , npy_type, image2.rows , image2.cols , image2.channels (), keywords);
708+ }
709+ #endif
675710#endif
676711
677712template <typename NumericX, typename NumericY>
0 commit comments