类型错误:不支持 mat 数据类型 = 0

Red*_*wan 4 python opencv image-processing python-2.7 imshow

我想用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()
Run Code Online (Sandbox Code Playgroud)

如何修复这个错误?

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

Ash*_*nJP 5

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

binary = np.asarray(binary, dtype="uint8")
Run Code Online (Sandbox Code Playgroud)

或使用以下命令更改二进制文件的类型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()
Run Code Online (Sandbox Code Playgroud)