我读取了一张图像,并使用此函数将其转换为灰度:
def rgb2gray(img):
if len(img.shape)==3 & img.shape[-1] == 3: # img is RGB
return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return img
现在,我尝试使用以下方法将我的图像转换为二进制:
def apply_threshold(img):
if len(np.unique(img))==2: #img is already binary
return img
gray_img=rgb2gray(img)
_,binary_img=cv2.threshold(gray_img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
return binary_img
但我收到了这个烦人的错误:
cv2.error: OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\imgproc\src\thresh.cpp:1406: error: (-215) src.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function cv::threshold
我不明白为什么因为gray_img
肯定是灰度的!我看了这个问题,上面的答案是salvador daly
提出输入图片不是灰度的,但是我检查了很多次,肯定是。
任何帮助将不胜感激!