【问题标题】:TypeError: mat data type = 0 is not supportedTypeError:不支持 mat 数据类型 = 0
【发布时间】:2020-09-05 02:57:34
【问题描述】:

我想使用cv2.imshow("Otsu img", binary) 而不是plt.imshow( binary)

我收到了错误

完整代码:

import matplotlib.pyplot as plt
from skimage import io
from skimage.filters.rank import entropy
from skimage.morphology import disk
import numpy as np
from skimage.filters import threshold_otsu
import cv2

img = io.imread("scratch.jpg")
entropy_img = entropy(img, disk(10))
thresh = threshold_otsu(entropy_img)

binary = entropy_img <= thresh



cv2.imshow("Otsu img", binary)

cv2.waitKey(0)
cv2.destroyAllWindows()

如何解决这个错误?

 cv2.imshow("Otsu img", binary)
TypeError: mat data type = 0 is not supported

【问题讨论】:

    标签: python python-2.7 opencv image-processing imshow


    【解决方案1】:

    TypeError 可以通过使用将二进制转换为dtype=uint8 来纠正,

    binary = np.asarray(binary, dtype="uint8")
    

    或使用astype(np.uint8)更改二进制类型

    但是在原始海报@Redhwan 之间进一步讨论后,OP 发现了问题,并且以下脚本似乎解决了问题:

    import matplotlib.pyplot as plt
    from skimage import io
    from skimage.filters.rank import entropy
    from skimage.morphology import disk
    import numpy as np
    from skimage.filters import threshold_otsu
    import cv2
    
    img = cv2.imread("scratch.jpg", 0)
    entropy_img = entropy(img, disk(10))
    # print type(entropy_img), entropy_img
    thresh = threshold_otsu(entropy_img)
    # print thresh
    # binary = entropy_img <= thresh
    ret1, th1 = cv2.threshold(entropy_img, thresh, 255, cv2.THRESH_BINARY_INV)
    # print type(entropy)
    
    
    cv2.imshow("Otsu img", img)
    cv2.imshow("Otsu th2", th1)
    # cv2.imshow("OTSU Gaussian cleaned", th3)
    # cv2.imshow("OTSU median cleaned", th4)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    【讨论】:

    • 感谢您的帮助。当我转换变量 binary (输出不同于 plt.imshow(binary)) 时,它不会给出错误,而是显示一个黑色窗口。但是在调用熵之前对我不起作用。
    • 尝试打印出二进制文件并检查您是否有所需的值。一种不错的方法是在每一步打印出数组的一个(相同)子集,并检查您是否得到了预期的结果)。如果您愿意,可以编辑我的答案。
    • type(binary) 前后转换相同
    • 不仅仅是二进制,打印出熵和阈值等其他变量。一旦它们被计算出来,打印这些变量并探索这是否会导致全黑窗口?我假设您的脚本中有一个 0 矩阵导致了这一点。我这里可能错了
    • 我做到了,结果一样。
    猜你喜欢
    • 2017-06-03
    • 2015-01-18
    • 1970-01-01
    • 2020-02-29
    • 2011-03-08
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多