当调用 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)
时,它会毫无错误地到达结尾(打印“完成”)。
此外,当我减小图像的大小时,也没有错误。
这是在带有 OpenCV 3.3.0 的 Raspberry Pi 上发生的。 我无法在笔记本电脑(Ubuntu 16.04、OpenCV 3.3.1)上重现错误。
这是一个(也许已知?)错误还是我做错了什么?
最佳答案
经过一番调试,发现是TBB库的问题,好像和this issue一样的问题。 .
在没有 TBB 的情况下重新编译 OpenCV 后,它现在可以工作了。根据链接的问题,使用较新版本的 TBB 进行编译也可能有所帮助,但我们尚未对此进行测试。
关于python - cv2.connectedComponents 在打印结果后再次调用时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52204156/