-217 opencv type_error ai_generated true

cv::error: (-217:未知错误码217) GpuMat::upload() 失败:输入矩阵为空或类型不受支持 在函数 'upload' 中

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

其他格式: JSON · Markdown 中文 · English
88%修复率
85%置信度
1证据数
2024-01-10首次发现

版本兼容性

版本状态引入弃用备注
4.5.0 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

根因分析

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

English

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

官方文档

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

解决方案

  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 */ }

无效尝试

常见但无效的做法:

  1. 90% 失败

    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% 失败

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