python - Opencv:错误:(-210)在函数阈值中

标签 python numpy opencv

我正在尝试估计基于简单numpy数组的图像的边界框

ret,thresh = cv.threshold(img1[:,:,244],10,255,0)

输出如下:

The thresholded image

接下来我做这个
im2,contours,hierarchy = cv.findContours(thresh, 1, 2)

我得到一个错误
FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function cvStartFindContours_Impl

所以我转换为
im2,contours,hierarchy = cv.findContours(thresh.astype(np.int8), 1, 2)

现在我得到了错误
error: (-210)  in function threshold

我该如何解决错误?他们的替代品是我该如何获得示例的倾斜边界框?

转换为np.uint8之前的数组:
array([[0., 0., 0., ..., 0., 0., 0.],
   [0., 0., 0., ..., 0., 0., 0.],
   [0., 0., 0., ..., 0., 0., 0.],
   ...,
   [0., 0., 0., ..., 0., 0., 0.],
   [0., 0., 0., ..., 0., 0., 0.],
   [0., 0., 0., ..., 0., 0., 0.]])

转换后的数组:
array([[0, 0, 0, ..., 0, 0, 0],
   [0, 0, 0, ..., 0, 0, 0],
   [0, 0, 0, ..., 0, 0, 0],
   ...,
   [0, 0, 0, ..., 0, 0, 0],
   [0, 0, 0, ..., 0, 0, 0],
   [0, 0, 0, ..., 0, 0, 0]], dtype=int8)

最佳答案

转换:

thresh = np.array([255, 0])
thresh.astype(np.int8) #array([-1,  0], dtype=int8)

由于溢出而给您一个错误的答案。

您应该改用:
thresh.astype(np.uint8) #array([255,   0], dtype=uint8)

这样结果才是正确的。

关于python - Opencv:错误:(-210)在函数阈值中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54651859/

相关文章:

python - OpenCV python 重叠粒子大小和数量

python - : "A conda package of Intel' s optimized Tensorflow comes with the new 2018 Intel Python distribution on Linux. 不是真的”

opencv - 对OpenCV的FaceRecognizer有什么信心?

python - 相机校准基本疑点

python - 在 python 中创建自签名证书

python - 从python中的多值元素数组打印特定值

python - 如何在 MacOS 上正确卸载 numpy?

python - 一些数据的Scipy拟合多项式模型

python - 原始数据中有400*1个单元格,每个单元格有64*64个矩阵,如何将数据拉伸(stretch)成列向量,形成一个4096*400的矩阵?

python - 使用pip安装Dlib时出现错误