[英]error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4045: error: (-215:Assertion failed)
[英]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:错误:(-215)src.type()==((((0)&(( 1 << 3)-1))+((((1)-1)<< 3))在函数cv :: threshold中
我不清楚这意味着什么以及如何解决它
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()
您将可以通过以下方式解决您的错误。
首先检查您的输入图像是否只有一个通道。 您可以通过运行print img.shape
进行检查。 如果结果是(height, width, 3)
,则图像不是单通道。 您可以通过以下方法将图像转换为一个通道:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
然后检查图像类型是否浮动。 您可以通过运行print img.dtype
进行检查。 如果结果与float
相关,则还需要通过以下方法进行更改:
img = img.astype('uint8')
还有最后一件事,在这种情况下,这实际上不是错误。 但是,如果您继续练习这种组合多个标志的方法,将来可能会出错。 当您使用多个标志时,请记住不要将加号 ()和|组合在一起。 标志 。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.