python - 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
解决方案
TypeError 可以通过将二进制转换为dtype=uint8
using 来纠正,
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()
推荐阅读
- android - 像素 2 上的 Mopub 横幅
- javascript - 组合框中的 setValue,仍然发送值
- visual-studio-code - 如何在vscode中启用//快捷键ctrl + /或?按钮不适用于 vscode 任何建议
- swift - 无需重新加载的 SwiftUI 视图结构
- angular - Angular 8 ngFor不显示来自对象的任何数据
- python - TensorFlow 2.0 [Condition x == y did not hold element-wise:]
- tfs - 使用 git-tfs 将 GIT 克隆到 TFS (AzureDevOps)
- asp.net - 处理损坏的数据库关系的业务层
- reactjs - 通过自定义 npm 包反应组件。找不到模块
- linux - 在括号前的每行末尾添加数字