We Are Going To Discuss About Adaptive Threshold error: (-215:Assertion failed) src.type() == CV_8UC1 in function ‘adaptiveThreshold’. So lets Start this Python Article.
Adaptive Threshold error: (-215:Assertion failed) src.type() == CV_8UC1 in function ‘adaptiveThreshold’
- How to solve Adaptive Threshold error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'adaptiveThreshold'
The error says the solution:
src.type() == CV_8UC1
meaning you need to set your image type to theuint8
source
So if you redefine yourimg
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 knowload_img
loads the image inBGR
fashion?
We know opencv loads the imagecv2.imread
inBGR
fashion.
The statement is wrong, sinceload_img
loads the image inRGB
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()
- Adaptive Threshold error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'adaptiveThreshold'
The error says the solution:
src.type() == CV_8UC1
meaning you need to set your image type to theuint8
source
So if you redefine yourimg
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 knowload_img
loads the image inBGR
fashion?
We know opencv loads the imagecv2.imread
inBGR
fashion.
The statement is wrong, sinceload_img
loads the image inRGB
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()
Solution 1
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()
Original Author Ahx Of This Content
Solution 2
cv2.adaptive_threshold
needs an input array of dtype uint8
:
img_grey = img_grey.astype(np.uint8)
th3 = cv2.adaptiveThreshold(img_grey...
Original Author Nicolas Gervais Of This Content
Solution 3
@bakuriu thresholding works on grayscaled images only, you need to convert the image to grayscale first and the the adaptiveThreshold
img = image.img_to_array(img2, dtype='uint8')
img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th3 = cv2.adaptiveThreshold(img_grey,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
Original Author Swati Pandey Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.