-217 opencv type_error ai_generated true

cv::error: (-217:Unknown error code 217) GpuMat::upload() failed: the input matrix is empty or has an unsupported type in function 'upload'

ID: opencv/gpu-mat-upload-failed

Also available as: JSON · Markdown · 中文
88%Fix Rate
85%Confidence
1Evidence
2024-01-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.0 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

Attempting to upload an empty cv::Mat or a Mat with a data type not supported by CUDA (e.g., CV_8UC3) to a GpuMat.

generic

中文

尝试将空的cv::Mat或数据类型不受CUDA支持(例如CV_8UC3)的Mat上传到GpuMat。

Official Documentation

https://docs.opencv.org/4.x/d0/d3f/classcv_1_1cuda_1_1GpuMat.html#a7b8c5b9e6f9e8d0a1b2c3d4e5f6g7h8

Workarounds

  1. 90% success Convert the Mat to a supported type before upload. For CV_8UC3, split into three CV_8UC1 channels and upload each separately, or convert to CV_8UC4 using cvtColor with COLOR_BGR2BGRA. Example: cv::Mat bgra; cv::cvtColor(bgr, bgra, cv::COLOR_BGR2BGRA); cv::cuda::GpuMat gpuMat; gpuMat.upload(bgra);
    Convert the Mat to a supported type before upload. For CV_8UC3, split into three CV_8UC1 channels and upload each separately, or convert to CV_8UC4 using cvtColor with COLOR_BGR2BGRA. Example: cv::Mat bgra; cv::cvtColor(bgr, bgra, cv::COLOR_BGR2BGRA); cv::cuda::GpuMat gpuMat; gpuMat.upload(bgra);
  2. 85% success Always verify the Mat is not empty and has a supported type: if (mat.empty() || mat.type() != CV_8UC1 && mat.type() != CV_8UC4 && mat.type() != CV_32FC1) { /* handle error */ }
    Always verify the Mat is not empty and has a supported type: if (mat.empty() || mat.type() != CV_8UC1 && mat.type() != CV_8UC4 && mat.type() != CV_32FC1) { /* handle error */ }

中文步骤

  1. Convert the Mat to a supported type before upload. For CV_8UC3, split into three CV_8UC1 channels and upload each separately, or convert to CV_8UC4 using cvtColor with COLOR_BGR2BGRA. Example: cv::Mat bgra; cv::cvtColor(bgr, bgra, cv::COLOR_BGR2BGRA); cv::cuda::GpuMat gpuMat; gpuMat.upload(bgra);
  2. Always verify the Mat is not empty and has a supported type: if (mat.empty() || mat.type() != CV_8UC1 && mat.type() != CV_8UC4 && mat.type() != CV_32FC1) { /* handle error */ }

Dead Ends

Common approaches that don't work:

  1. 90% fail

    cv::cuda::GpuMat::upload() only supports single-channel or 4-channel types (e.g., CV_8UC1, CV_8UC4, CV_32FC1); CV_8UC3 is not directly supported and must be split.

  2. 70% fail

    An empty Mat can result from failed image loading or processing; the upload function does not handle empty inputs.