OpenCV 矩阵元素除法给出全零结果

OpenCV matrix element-wise division gives all-zero result

我在 iOS C++ 代码上使用 cocoapod 的 OpenCV。当 运行 我的应用程序时,我发现它运行异常。最后,经过挖掘,我可以给出以下可重现的样本:

    {
        Mat a = (Mat_<uchar>({10, 20, 30, 40, 50, 60, 70, 80})).reshape(1, 1);
        Mat b = (Mat_<uchar>({4, 5, 6, 7, 8, 9, 10, 11})).reshape(1, 1);
        Mat c;
        divide(a, b, c);
        std::cout << "hello naive " << "a=" << a << endl << "b=" << b << endl << "a/b=" << (a / b) << "c=" << c << endl;
    }
    {
        Mat a = (Mat_<uchar>({10, 20, 30, 40, 50, 60, 70})).reshape(1, 1);
        Mat b = (Mat_<uchar>({4, 5, 6, 7, 8, 9, 10})).reshape(1, 1);
        Mat c;
        divide(a, b, c);
        std::cout << "hello naive " << "a=" << a << endl << "b=" << b << endl << "a/b=" << (a / b) << "c=" << c << endl;
    }

结果:

im_sta[a]: {size=1 x 8, type=0, empty=0, min=10.000000, max=80.000000, miu=[45], sigma=[22.9]}
im_sta[b]: {size=1 x 8, type=0, empty=0, min=4.000000, max=11.000000, miu=[7.5], sigma=[2.29]}
im_sta[a / b]: {size=1 x 8, type=0, empty=0, min=0.000000, max=0.000000, miu=[0], sigma=[0]}
hello naive a=[ 10,  20,  30,  40,  50,  60,  70,  80]
b=[  4,   5,   6,   7,   8,   9,  10,  11]
a/b=[  0,   0,   0,   0,   0,   0,   0,   0]c=[  0,   0,   0,   0,   0,   0,   0,   0]
im_sta[a]: {size=1 x 7, type=0, empty=0, min=10.000000, max=70.000000, miu=[40], sigma=[20]}
im_sta[b]: {size=1 x 7, type=0, empty=0, min=4.000000, max=10.000000, miu=[7], sigma=[2]}
im_sta[a / b]: {size=1 x 7, type=0, empty=0, min=2.000000, max=7.000000, miu=[5.29], sigma=[1.67]}
hello naive a=[ 10,  20,  30,  40,  50,  60,  70]
b=[  4,   5,   6,   7,   8,   9,  10]
a/b=[  2,   4,   5,   6,   6,   7,   7]c=[  2,   4,   5,   6,   6,   7,   7]

(其中 im_sta 是我打印的一些额外的统计信息以获取更多详细信息,您也可以忽略它)。

如您所见,在某些情况下(似乎当 >=8 个元素时),除法结果为全零!

我该如何解决?谢谢!

我稍后找到答案。所以我post把它作为一个自我QA,以帮助和我有同样问题的人。

解决方案:将 OpenCV 从 4.1 升级到 4.3!那就ok了!

实际上,正如您从下面的照片中看到的那样,OpenCV >=4.2.0 仅在 3 个月前作为 cocoapod 可用(即使现在它已经是 4.5.0)...所以这是一个相当新修复,如果你不想自己从源代码编译 opencv...(当我开始我的项目时(当然是在 3 个月前)只有 4.1.0)