I read an image, and converted it to gray-scale using this function:
def rgb2gray(img):
if len(img.shape)==3 & img.shape[-1] == 3: # img is RGB
return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return img
now, I try to convert my image to binary using this:
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
but I get this annoying error:
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
I can't understand why since gray_img
is for sure gray-scale!
I looked at this question, and the top answer by salvador daly
proposed that the input picture is not gray-scale, but I checked it multiple times and it for sure is.
Any help will be appreciated!