Adaptive Threshold error: (-215:Assertion failed) src.type() == CV_8UC1 in function ‘adaptiveThreshold’

Tags: , , ,



I am working on pre-trained vgg16 model, for that I need to have input size of image file to be (224,224,3).

The code I am working on is:

from tensorflow.keras.preprocessing import image
import cv2
import matplotlib.pyplot as plt

img = image.load_img('abc.jpg',target_size=(224,224))
img = image.img_to_array(img)

print(img.shape)
## output : (224,224,3)
img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#plt.imshow(img_grey)

th3 = cv2.adaptiveThreshold(img_grey,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
plt.figure(figsize=(20,10))
plt.imshow(th3)
error                                     Traceback (most recent call last)
<ipython-input-88-2a8a27b965ed> in <module>
     17 #plt.imshow(img_grey)
     18 
---> 19 th3 = cv2.adaptiveThreshold(img_grey,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
     20 plt.figure(figsize=(20,10))
     21 plt.imshow(th3)

error: OpenCV(4.1.0) /io/opencv/modules/imgproc/src/thresh.cpp:1627: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'adaptiveThreshold'

Help me in resolving the issue.

Answer

The error says the solution: src.type() == CV_8UC1 meaning you need to set your image type to the uint8 source

So if you redefine your img variable:

img = image.img_to_array(img, dtype='uint8')

Problem will be solved but I have a question.

Why do you define the below statement?

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

How do you know load_img loads the image in BGR fashion?

We know opencv loads the image cv2.imread in BGR fashion.

The statement is wrong, since load_img loads the image in RGB format source

Therefore the correct statement will be:

img_grey = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

or you can do:

img = image.load_img('15f8U.png', grayscale=True, target_size=(224, 224))

Correct Code:

from keras.preprocessing import image
import cv2
import matplotlib.pyplot as plt

img = image.load_img('15f8U.png', grayscale=True, target_size=(224, 224))
img = image.img_to_array(img, dtype='uint8')

print(img.shape)
## output : (224,224,3)
#plt.imshow(img_grey)

th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
plt.figure(figsize=(20,10))
plt.imshow(th3, cmap="gray")
plt.show()


Source: stackoverflow