TypeError:不支持mat数据类型= 0

问题描述 投票:1回答:1

我想用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个回答
1
投票

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

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()
© www.soinside.com 2019 - 2024. All rights reserved.