17
17
#ifndef WITHOUT_NUMPY
18
18
# define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
19
19
# include < numpy/arrayobject.h>
20
+
21
+ # ifdef WITH_OPENCV
22
+ # include < opencv2/opencv.hpp>
23
+ # endif // WITH_OPENCV
20
24
#endif // WITHOUT_NUMPY
21
25
22
26
#if PY_MAJOR_VERSION >= 3
@@ -472,7 +476,7 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b", dou
472
476
// construct args
473
477
npy_intp dims[3 ] = { rows, columns, colors };
474
478
PyObject *args = PyTuple_New (1 );
475
- PyTuple_SetItem (args, 0 , PyArray_SimpleNewFromData (3 , dims, type, ptr));
479
+ PyTuple_SetItem (args, 0 , PyArray_SimpleNewFromData (colors == 1 ? 2 : 3 , dims, type, ptr));
476
480
477
481
// construct keyword args
478
482
PyObject* kwargs = PyDict_New ();
@@ -499,6 +503,37 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b", dou
499
503
{
500
504
internal::imshow ((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords);
501
505
}
506
+
507
+ #ifdef WITH_OPENCV
508
+ void imshow (const cv::Mat &image, const std::map<std::string, std::string> &keywords = {})
509
+ {
510
+ // Convert underlying type of matrix, if needed
511
+ cv::Mat image2;
512
+ NPY_TYPES npy_type = NPY_UINT8;
513
+ switch (image.type () & CV_MAT_DEPTH_MASK) {
514
+ case CV_8U:
515
+ image2 = image;
516
+ break ;
517
+ case CV_32F:
518
+ image2 = image;
519
+ npy_type = NPY_FLOAT;
520
+ break ;
521
+ default :
522
+ image.convertTo (image2, CV_MAKETYPE (CV_8U, image.channels ()));
523
+ }
524
+
525
+ // If color image, convert from BGR to RGB
526
+ switch (image2.channels ()) {
527
+ case 3 :
528
+ cv::cvtColor (image2, image2, CV_BGR2RGB);
529
+ break ;
530
+ case 4 :
531
+ cv::cvtColor (image2, image2, CV_BGRA2RGBA);
532
+ }
533
+
534
+ internal::imshow (image2.data , npy_type, image2.rows , image2.cols , image2.channels (), keywords);
535
+ }
536
+ #endif
502
537
#endif
503
538
504
539
template < typename Numeric>
0 commit comments