首页 > 解决方案 > TypeError:不支持 mat 数据类型 = 0

问题描述

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

标签: pythonpython-2.7opencvimage-processingimshow

解决方案


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

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()

推荐阅读