15
15
#ifndef WITHOUT_NUMPY
16
16
# define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
17
17
# include < numpy/arrayobject.h>
18
+
19
+ # ifdef WITH_OPENCV
20
+ # include < opencv2/opencv.hpp>
21
+ # endif // WITH_OPENCV
18
22
#endif // WITHOUT_NUMPY
19
23
20
24
#if PY_MAJOR_VERSION >= 3
@@ -645,7 +649,7 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
645
649
// construct args
646
650
npy_intp dims[3 ] = { rows, columns, colors };
647
651
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));
649
653
650
654
// construct keyword args
651
655
PyObject* kwargs = PyDict_New ();
@@ -672,6 +676,37 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
672
676
{
673
677
internal::imshow ((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords);
674
678
}
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
675
710
#endif
676
711
677
712
template <typename NumericX, typename NumericY>
0 commit comments