[英]Adaptive Threshold error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'adaptiveThreshold'
I am working on pre-trained vgg16 model, for that I need to have input size of image file to be (224,224,3).我正在研究预训练的 vgg16 model,因为我需要输入图像文件的大小为(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.帮助我解决问题。
The error says the solution: src.type() == CV_8UC1
meaning you need to set your image type to the uint8
source错误说明了解决方案: src.type() == CV_8UC1
意味着您需要将图像类型设置为uint8
源
So if you redefine your img
variable:因此,如果您重新定义img
变量:
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?你怎么知道load_img
以BGR
方式加载图像?
We know opencv loads the image cv2.imread
in BGR
fashion.我们知道 opencv 以BGR
方式加载图像cv2.imread
。
The statement is wrong, since load_img
loads the image in RGB
format source语句错误,因为load_img
以RGB
格式源加载图像
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()
cv2.adaptive_threshold
needs an input array of dtype uint8
: cv2.adaptive_threshold
需要一个 dtype uint8
的输入数组:
img_grey = img_grey.astype(np.uint8)
th3 = cv2.adaptiveThreshold(img_grey...
@bakuriu thresholding works on grayscaled images only, you need to convert the image to grayscale first and the the adaptiveThreshold @bakuriu 阈值仅适用于灰度图像,您需要先将图像转换为灰度,然后将 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.