我在我的项目中使用 OpenCV for android 来处理 YUV420 ( https://wiki.videolan.org/YUV/#NV12 ) 图像。由于项目的其余部分在 C# 中,我正在使用 GCHandles 将字节数组复制到 C++ 代码。通过对 C# 和 C++ 中的所有字节求和并进行比较,我已确保 C++ 中的数据正常。但是,当我尝试这样做时:
int foo(unsigned char * inputPtr, int width, int height) {
cv::InputArray input_image = cv::Mat(height + height /2, width, CV_8UC1, inputPtr);
int res = 0;
for (int y = 0; y < input_image.rows(); y++) {
for (int x = 0; x < input_image.cols(); x++) {
res += (int)input_image.getMat().ptr(x + y * input_image.cols());
}
}
return res;
}
,每次都返回零(C# 计数仍然返回正确的数字)。返回正确值的所有字节的总和如下所示:
int foo(unsigned char * inputPtr, int width, int height) {
int res = 0;
for (int i = 0; i < width * height * 1.5f; i++) {
res += (int)inputPtr[i];
}
return res;
}
根据这个问题,cv::Mat() 参数应该是正确的:Converting from YUV colour space to RGB using OpenCV
OpenCV 有什么理由必须决定不使用看似有效的数据创建矩阵?
编辑:忘记添加 inputPtr 指针指向大小为 (int)(camBytes.Width * camBytes.Height * 1.5f) 的 C# byte[]。
最佳答案
Ptr 返回指针但没有值。使用 at():
res += (int)input_image.getMat().at<uchar>(y, x);
关于c# - OpenCV:cv::Mat 无法从看似有效的数据中创建矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55434261/