我运行了以下代码,但出现错误
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()
Run Code Online (Sandbox Code Playgroud)
您将可以通过以下方式解决您的错误。
首先检查您的输入图像是否只有一个通道。您可以通过运行进行检查print img.shape。如果结果是(height, width, 3),则图像不是单通道。您可以通过以下方法将图像转换为一个通道:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Run Code Online (Sandbox Code Playgroud)
然后检查图像类型是否浮动。您可以通过运行进行检查print img.dtype。如果结果与相关float,则还需要通过以下方法进行更改:
img = img.astype('uint8')
Run Code Online (Sandbox Code Playgroud)
还有最后一件事,在这种情况下,这实际上不是错误。但是,如果您继续练习这种组合多个标志的方法,将来可能会出错。当您使用多个标志时,请记住不要将加号()和|组合在一起。标志。
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
Run Code Online (Sandbox Code Playgroud)
最后,您可以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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9057 次 |
| 最近记录: |