'cv2.cuda.GpuMat' object has no attribute 'cols' when compiled from source

When I try to get cols and rows from CUDA GpuMat,

pixel_number = gray.cols * gray.rows

it results in error:

AttributeError: 'cv2.cuda.GpuMat' object has no attribute 'cols'

What is interesting is when I run this script using precompiled opencv-python & opencv-contrib-python 4.7.0, it never throws error. Only when I run it with OpenCV I compiled from source. Did I miss some options?

My CMake command:

CUDA_PATH="/usr/local/cuda-11.4" CFLAGS="-I/usr/local/cuda-11.4/include -I/opt/intel/compilers_and_libraries/linux/mkl/include" LDFLAGS="-L/usr/local/cuda-11.4/lib64" \
  cmake .. -D CMAKE_BUILD_TYPE=RELEASE \
  -D BUILD_opencv_world=ON \
  -D BUILD_opencv_apps=ON \
  -D OPENCV_EXTRA_MODULES_PATH=/usr/local/opencv_contrib/modules \
  -D OpenBLAS_INCLUDE_DIR=/usr/include/x86_64-linux-gnu \
  -D OpenBLAS_LIB=/usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblas.so \
  -D WITH_PYTHON=ON \
  -D BUILD_opencv_python2=OFF \
  -D BUILD_opencv_python3=ON \
  -D PYTHON_DEFAULT_EXECUTABLE=python3 \
  -D WITH_MKL=OFF \
  -D WITH_CUDA=ON \
  -D CUDA_FAST_MATH=ON \
  -D WITH_CUBLAS=ON \
  -D WITH_CUDNN=ON \
  -D WITH_NVCUVID=OFF \
  -D OPENCV_DNN_CUDA=ON \
  -D CUDA_ARCH_BIN=7.5,8.6 \
  -D BUILD_opencv_cudaimgproc=ON \
  -D BUILD_EXAMPLES=ON \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D INSTALL_TESTS=ON \
  -D INSTALL_C_EXAMPLES=ON \
  -D INSTALL_BIN_EXAMPLES=ON \
  -D INSTALL_PYTHON_EXAMPLES=ON \
  -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages

configuration:

-- General configuration for OpenCV 4.7.0 =====================================
--   Version control:               4.7.0
--
--   Extra modules:
--     Location (extra):            /usr/local/opencv_contrib/modules
--     Version control (extra):     4.7.0-1-gc578eb13
--
--   Platform:
--     Timestamp:                   2023-03-31T02:31:06Z
--     Host:                        Linux 5.4.0-144-generic x86_64
--     CMake:                       3.16.3
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
--
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (18 files):         + SSSE3 SSE4_1
--       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (34 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (8 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ standard:                11
--     C++ Compiler:                /usr/bin/c++  (ver 9.4.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat
--     C++ flags (Debug):           -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -I/usr/local/cuda-11.4/include -I/opt/intel/compilers_and_libraries/linux/mkl/include   -fsigned-cha
--     C flags (Debug):             -I/usr/local/cuda-11.4/include -I/opt/intel/compilers_and_libraries/linux/mkl/include   -fsigned-cha
--     Linker flags (Release):      -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -L/usr/local/cuda-11.4/lib64  -Wl,--gc-
--     Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -L/usr/local/cuda-11.4/lib64  -Wl,--gc-
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          m pthread cudart_static -lpthread dl rt nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu n
--     3rdparty dependencies:
--
--   OpenCV modules:
--     To be built:                 alphamat aruco barcode bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafe
--     Disabled:                    -
--     Disabled by dependency:      -
--     Unavailable:                 cvv java julia matlab ovis python2
--     Applications:                tests perf_tests examples apps
--     Documentation:               NO
--     Non-free algorithms:         NO
--
--   GUI:
--     GTK+:                        YES (ver 3.24.20)
--       GThread :                  YES (ver 2.64.6)
--       GtkGlExt:                  NO
--     VTK support:                 YES (ver 7.1.1)
--
--   Media I/O:
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   OpenJPEG (ver 2.3.1)
--     OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-g
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
--
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (58.54.100)
--       avformat:                  YES (58.29.100)
--       avutil:                    YES (56.31.100)
--       swscale:                   YES (5.5.100)
--       avresample:                YES (4.0.0)
--     GStreamer:                   YES (1.16.3)
--     v4l/v4l2:                    YES (linux/videodev2.h)
--
--   Parallel framework:            pthreads
--
--   Trace:                         YES (with Intel ITT)
--
--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   /usr/local/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /usr/local/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
--     VA:                          NO
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.7)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.19.1)
--
--   NVIDIA CUDA:                   YES (ver 11.4, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             75 86
--     NVIDIA PTX archs:
--
--   cuDNN:                         YES (ver 8.8.1)
--
--   OpenCL:                        YES (no extra features)
--     Include path:                /usr/local/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
--
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.8.10)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.10)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.17.4)
--     install path:                /usr/lib/python3/dist-packages/cv2/python-3.8
--
--   Python (for build):            python3
--
--   Java:
--     ant:                         NO
--     JNI:                         /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
--     Java wrappers:               NO
--     Java tests:                  NO
--
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/opencv/build

I’m not sure why but rows and cols are not exposed to python bindings, see modules/core/include/opencv2/core/cuda.hpp.

You have to use the GpuMat.size() method instead. Just be aware when using this that a GpuMat.size()[0] is the number of cols and GpuMat.size()[1]is the number of rows. I know this sounds obvious but it can be confusing when the constructor signature is GpuMat(rows,cols,type).