cv2.connectedComponents 在打印结果后再次调用时崩溃

cv2.connectedComponents crashes when called again after printing its result

当调用 cv2.connectedComponents 时,打印其结果然后再次调用它,我在第二次调用时遇到分段错误。我设法将其缩小到以下示例:

#!/usr/bin/python
import cv2
import numpy as np

if __name__ == "__main__":
    img = np.array([
        [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
        [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
        [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255, 255],
        [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255, 255],
        [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255, 255],
        [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255, 255],
        [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255, 255],
        [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
        [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
        ], dtype=np.uint8)

    res1 = cv2.connectedComponents(img)
    print(res1)
    cv2.connectedComponents(img)  # < here it is crashing
    print("done")

执行时,我得到以下输出:

(2, array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
       [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
       [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
       [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
       [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int32))
Segmentation fault

特别奇怪的是:当我删除 print(res1) 时,它毫无错误地到达结尾(打印 "done")。 而且当我缩小图像的尺寸时,也没有错误。

这是在使用 OpenCV 3.3.0 的 Raspberry Pi 上发生的。 我无法在笔记本电脑上重现错误(Ubuntu 16.04,OpenCV 3.3.1)。

这是一个(也许已知?)错误还是我做错了什么?

经过一番调试,发现是TBB库的问题,好像和this issue一样的问题。

在没有 TBB 的情况下重新编译 OpenCV 后,它现在可以工作了。根据链接的问题,使用较新版本的 TBB 进行编译也可能有所帮助,但我们尚未对此进行测试。