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

Python 类型错误:不支持mat数据类型=0,python,python-2.7,opencv,image-processing,imshow,Python,Python 2.7,Opencv,Image Processing,Imshow,我想用cv2.imshow(“大津img”,二进制)代替plt.imshow(二进制) 我弄错了 完整代码: 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 i

我想用
cv2.imshow(“大津img”,二进制)
代替
plt.imshow(二进制)

我弄错了

完整代码:

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

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

binary = np.asarray(binary, dtype="uint8")
或者使用
astype(np.uint8)

但在原始海报之间进一步讨论后,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()
导入matplotlib.pyplot作为plt
从撇渣进口io
从skimage.filters.rank导入熵
从skimage.com导入磁盘
将numpy作为np导入
从skimage.filters导入阈值\u otsu
进口cv2
img=cv2.imread(“scratch.jpg”,0)
熵\u img=熵(img,磁盘(10))
#打印类型(熵\u img),熵\u img
阈值=阈值(熵)
#印花脱粒

#谢谢你的帮助。当我转换变量二进制(输出不同于plt.imshow(binary))时,它不会给出错误,而是显示一个黑色窗口。但在调用熵之前,我无法使用它。请尝试打印二进制文件并检查是否有所需的值。一个很好的方法是在每一步打印出一个(相同的)数组子集,并检查是否得到了预期的结果。如果你想,你可以编辑我的答案。在转换相同的答案之前和之后键入(二进制),而不仅仅是二进制,打印出其他变量,如熵和阈值。一旦它们被计算出来,打印这些变量,并探索这是否会导致全黑窗口?我假设在脚本中的某一点上有一个0矩阵。我可能错了,我做了,同样的结果。
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()