简体   繁体   English

自适应阈值错误:(-215:Assertion failed) src.type() == CV_8UC1 in function 'adaptiveThreshold'

[英]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_imgBGR方式加载图像?

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_imgRGB格式加载图像

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.

相关问题 thresh.cpp:1676: 错误: (-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::adaptiveThreshold' - thresh.cpp:1676: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::adaptiveThreshold' OpenCV: 错误: (-215:Assertion failed) _src.type() == CV_8UC1 in function 'cv::equalizeHist' - OpenCV: error: (-215:Assertion failed) _src.type() == CV_8UC1 in function 'cv::equalizeHist' 转换成灰度后仍然不能在 OpenCV 中使用 cv.equalizeHist()。 给出错误:(-215:断言失败)_src.type()== CV_8UC1 - Still after convertion into grayscale can not use cv.equalizeHist() in OpenCV. Gives error: (-215:Assertion failed) _src.type() == CV_8UC1 错误:(-215) _src.type() == CV_8UC1 在函数 equalizeHist 中尝试均衡 float64 图像时 - error: (-215) _src.type() == CV_8UC1 in function equalizeHist when trying to equalize a float64 image 函数AdaptiveThreshold中的CV_8UC1(Error-215) - CV_8UC1 (Error-215) in function adaptiveThreshold 无法使用adaptiveThreshold:函数adaptiveThreshold 中的CV_8UC1 - Failure to use adaptiveThreshold: CV_8UC1 in function adaptiveThreshold 错误:(-215:Assertion failed)._src:empty() in function 'cv::cvtColor' - error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' 错误:(-215:断言失败)_src.total() &gt; 0 在 function 'cv::warpPerspective' - error: (-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective' 带有Python错误的OpenCV-断言失败((mask.type()== CV_8UC1 || mask.type()== CV_8SC1)) - OpenCV with Python error - Assertion failed ((mask.type() == CV_8UC1 || mask.type() == CV_8SC1)) in binary_op 类型断言失败。 类型 == CV_8UC1 || stype == CV_16SC1 || stype == CV_32SC1 || function 中的 stype == CV_32FC1 - type assertion failed. type == CV_8UC1 || stype == CV_16SC1 || stype == CV_32SC1 || stype == CV_32FC1 in function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM