【问题标题】:Getting an error OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\imgproc\src\thresh.cpp:1406: error: (-215)收到错误 OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\imgproc\src\thresh.cpp:1406: error: (-215)
【发布时间】:2018-05-31 19:07:00
【问题描述】:

我运行了下面的代码,我得到一个错误

OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\imgproc\src\thresh.cpp:1406: 错误:(-215) src.type() == (((0) & ((1

我不清楚这意味着什么以及如何解决它

import numpy as numpy
from matplotlib import pyplot as matplot
import pandas as pandas
import math
from sklearn import preprocessing
from sklearn import svm
import cv2

blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
image = numpy.invert(th3)
matplot.imshow(image,'gray')
matplot.show()

【问题讨论】:

  • 搜索“OpenCV -215”。

标签: python opencv


【解决方案1】:

您将能够通过以下方式解决您的错误。

首先检查您的输入图像是否只有单通道。您可以通过运行print img.shape 来检查它。如果结果类似于(height, width, 3),则图像不是单通道。您可以通过以下方式将图像转换为单个通道:

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

然后检查图片类型是否不是float。您可以通过运行print img.dtype 来检查它。如果结果与float 相关,您还需要通过以下方式进行更改:

img = img.astype('uint8')

还有最后一件事,在这种情况下它实际上并不是一个错误。但是,如果您继续练习这种组合多个标志的方法,将来可能会出错。当您使用多个标志时,请记住不要将 then 与 加号 组合,而是与 | 组合。签名

 ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)

最后,您可以使用opencv 函数来显示图像。无需依赖其他库。

最终代码如下:

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = img.astype('uint8')
blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
image = numpy.invert(th3)
cv2.show('image_out', image)
cv2.waitKey(0)
cv2.destroyAllWindows() 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 2020-08-11
    • 2021-11-28
    • 1970-01-01
    相关资源
    最近更新 更多