-
-
Notifications
You must be signed in to change notification settings - Fork 56.5k
Python cv2.imshow does not work for GpuMat inputs #18553
Description
System information (version)
- OpenCV => 4.4.0
- Operating System / Platform => Linux x86_64 (Ubuntu 18.04)
- Python version: 3.6
Detailed description
According to the documentation, if a window was created with OpenGL support, ogl::Buffer , ogl::Texture2D and cuda::GpuMat are supported as input for imshow. This is not working as expected with the Python bindings when trying to visualize a GpuMat structure:
import cv2
import numpy as np
a = cv2.cuda_GpuMat(np.array([[1, 1], [1, 1]], dtype=np.uint8))
cv2.namedWindow("my window", flags=cv2.WINDOW_OPENGL|cv2.WINDOW_NORMAL)
cv2.imshow("my window", a)The Python snippet above throws an error:
Traceback (most recent call last):
File "imshow_test.py", line 6, in <module>
cv2.imshow("my window", a)
TypeError: Expected Ptr<cv::UMat> for argument 'mat'
After digging a bit into the problem, I got to the conclusion that the functions with GpuMat structures involved don't get properly wrapped in Python unless they are under the C++ 'cuda' namespace (which is not the case for 'imshow').
I created a small wrapper for 'imshow' under the 'cuda' namespace (cuda.cuda_imshow) and this one works as intended in Python when passing a cuda_GpuMat structure as an input:
diff -urN opencv/modules/highgui/include/opencv2/highgui.hpp ../../opencv/opencv-4.4.0/modules/highgui/include/opencv2/highgui.hpp
--- opencv/modules/highgui/include/opencv2/highgui.hpp 2020-09-30 12:02:00.609385852 +0000
+++ ../../opencv/opencv-4.4.0/modules/highgui/include/opencv2/highgui.hpp 2020-09-23 11:15:30.800408600 +0000
@@ -172,6 +172,10 @@
*/
///////////////////////// graphical user interface //////////////////////////
+namespace cv { namespace cuda {
+CV_EXPORTS_W void cuda_imshow(const String& winname, InputArray mat);
+}}
+
namespace cv
{
@@ -389,6 +393,7 @@
*/
CV_EXPORTS_W void imshow(const String& winname, InputArray mat);
+
/** @brief Resizes window to the specified size
@note
diff -urN opencv/modules/highgui/src/window.cpp ../../opencv/opencv-4.4.0/modules/highgui/src/window.cpp
--- opencv/modules/highgui/src/window.cpp 2020-09-30 12:02:00.677385931 +0000
+++ ../../opencv/opencv-4.4.0/modules/highgui/src/window.cpp 2020-09-23 11:15:54.868340916 +0000
@@ -425,6 +425,11 @@
#endif
}
+void cv::cuda::cuda_imshow( const String& winname, InputArray _img )
+{
+ cv::imshow(winname, _img);
+}
+
void cv::imshow(const String& winname, const ogl::Texture2D& _tex)
{
CV_TRACE_FUNCTION();I'm not too familiar with the gen2.py script that generates the Python bindings, but modifying that script to contemplate this special case might be the best solution to this issue.
Steps to reproduce
See the code snippet in the previous section.
Issue submission checklist
- I report the issue, it's not a question
- I checked the problem with documentation, FAQ, open issues,
- I updated to latest OpenCV version and the issue is still there
- There is reproducer code and related data files: videos, images, onnx, etc