@@ -589,77 +589,79 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
589589}
590590
591591#ifndef WITHOUT_NUMPY
592- namespace internal {
593- 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)
594- {
595- assert (type == NPY_UINT8 || type == NPY_FLOAT);
596- assert (colors == 1 || colors == 3 || colors == 4 );
597-
598- detail::_interpreter::get (); // interpreter needs to be initialized for the numpy commands to work
599-
600- // construct args
601- npy_intp dims[3 ] = { rows, columns, colors };
602- PyObject *args = PyTuple_New (1 );
603- PyTuple_SetItem (args, 0 , PyArray_SimpleNewFromData (colors == 1 ? 2 : 3 , dims, type, ptr));
604-
605- // construct keyword args
606- PyObject* kwargs = PyDict_New ();
607- for (std::map<std::string, std::string>::const_iterator it = keywords.begin (); it != keywords.end (); ++it)
608- {
609- PyDict_SetItemString (kwargs, it->first .c_str (), PyUnicode_FromString (it->second .c_str ()));
610- }
611-
612- PyObject *res = PyObject_Call (detail::_interpreter::get ().s_python_function_imshow , args, kwargs);
613- Py_DECREF (args);
614- Py_DECREF (kwargs);
615- if (!res)
7440
616- throw std::runtime_error (" Call to imshow() failed" );
617- if (out)
618- *out = res;
619- else
620- Py_DECREF (res);
621- }
622- }
592+ namespace internal {
623593
624- 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 )
625- {
626- internal::imshow ((void *) ptr, NPY_UINT8, rows, columns, colors, keywords, out);
627- }
594+ 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)
595+ {
596+ assert (type == NPY_UINT8 || type == NPY_FLOAT);
597+ assert (colors == 1 || colors == 3 || colors == 4 );
598+
599+ detail::_interpreter::get (); // interpreter needs to be initialized for the numpy commands to work
628600
629- 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 )
601+ // construct args
602+ npy_intp dims[3 ] = { rows, columns, colors };
603+ PyObject *args = PyTuple_New (1 );
604+ PyTuple_SetItem (args, 0 , PyArray_SimpleNewFromData (colors == 1 ? 2 : 3 , dims, type, ptr));
605+
606+ // construct keyword args
607+ PyObject* kwargs = PyDict_New ();
608+ for (std::map<std::string, std::string>::const_iterator it = keywords.begin (); it != keywords.end (); ++it)
630609 {
631- internal::imshow (( void *) ptr, NPY_FLOAT, rows, columns, colors, keywords, out );
610+ PyDict_SetItemString (kwargs, it-> first . c_str (), PyUnicode_FromString (it-> second . c_str ()) );
632611 }
633612
634- #ifdef WITH_OPENCV
635- void imshow (const cv::Mat &image, const std::map<std::string, std::string> &keywords = {})
636- {
637- // Convert underlying type of matrix, if needed
638- cv::Mat image2;
639- NPY_TYPES npy_type = NPY_UINT8;
640- switch (image.type () & CV_MAT_DEPTH_MASK) {
641- case CV_8U:
642- image2 = image;
643- break ;
644- case CV_32F:
645- image2 = image;
646- npy_type = NPY_FLOAT;
647- break ;
648- default :
649- image.convertTo (image2, CV_MAKETYPE (CV_8U, image.channels ()));
650- }
613+ PyObject *res = PyObject_Call (detail::_interpreter::get ().s_python_function_imshow , args, kwargs);
614+ Py_DECREF (args);
615+ Py_DECREF (kwargs);
616+ if (!res)
617+ throw std::runtime_error (" Call to imshow() failed" );
618+ if (out)
619+ *out = res;
620+ else
621+ Py_DECREF (res);
622+ }
651623
652- // If color image, convert from BGR to RGB
653- switch (image2.channels ()) {
654- case 3 :
655- cv::cvtColor (image2, image2, CV_BGR2RGB);
656- break ;
657- case 4 :
658- cv::cvtColor (image2, image2, CV_BGRA2RGBA);
659- }
624+ } // namespace internal
625+
626+ 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 )
627+ {
628+ internal::imshow ((void *) ptr, NPY_UINT8, rows, columns, colors, keywords, out);
629+ }
660630
661- internal::imshow (image2.data , npy_type, image2.rows , image2.cols , image2.channels (), keywords);
631+ 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 )
632+ {
633+ internal::imshow ((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords, out);
634+ }
635+
636+ #ifdef WITH_OPENCV
637+ void imshow (const cv::Mat &image, const std::map<std::string, std::string> &keywords = {})
638+ {
639+ // Convert underlying type of matrix, if needed
640+ cv::Mat image2;
641+ NPY_TYPES npy_type = NPY_UINT8;
642+ switch (image.type () & CV_MAT_DEPTH_MASK) {
643+ case CV_8U:
644+ image2 = image;
645+ break ;
646+ case CV_32F:
647+ image2 = image;
648+ npy_type = NPY_FLOAT;
649+ break ;
650+ default :
651+ image.convertTo (image2, CV_MAKETYPE (CV_8U, image.channels ()));
662652 }
653+
654+ // If color image, convert from BGR to RGB
655+ switch (image2.channels ()) {
656+ case 3 :
657+ cv::cvtColor (image2, image2, CV_BGR2RGB);
658+ break ;
659+ case 4 :
660+ cv::cvtColor (image2, image2, CV_BGRA2RGBA);
661+ }
662+
663+ internal::imshow (image2.data , npy_type, image2.rows , image2.cols , image2.channels (), keywords);
664+ }
663665#endif // WITH_OPENCV
664666#endif // WITHOUT_NUMPY
665667
0 commit comments