TypeError: mat data type = 0 is not supported

TypeError: mat data type = 0 is not supported

我想使用 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

可以通过使用

将二进制转换为 dtype=uint8 来纠正 TypeError
binary = np.asarray(binary, dtype="uint8")

或使用astype(np.uint8)

更改二进制类型

但在 Original Poster @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()