Python (-215: 断言失败) !_src.empty() 在函数 'cv::cvtColor' 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53926657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
提问by Benji
I am trying to recognize text from an image to then have the text outputted; however, this error spits out:
我正在尝试从图像中识别文本,然后输出文本;然而,这个错误吐出来了:
Traceback (most recent call last): File "C:/Users/Benji's Beast/AppData/Local/Programs/Python/Python37-32/imageDet.py", line 41, in print(get_string(src_path + "cont.jpg") ) File "C:/Users/Benji's Beast/AppData/Local/Programs/Python/Python37-32/imageDet.py", line 15, in get_string img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
回溯(最近一次通话):文件“C:/Users/Benji's Beast/AppData/Local/Programs/Python/Python37-32/imageDet.py”,第 41 行,打印中(get_string(src_path + "cont.jpg") ) ) 文件“C:/Users/Benji's Beast/AppData/Local/Programs/Python/Python37-32/imageDet.py”,第 15 行,在 get_string img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv:: cvt颜色'
The image resolution is 1371x51. I have tried changing the "/" on src_path to "\" but that didn't work. Any ideas?
图像分辨率为 1371x51。我曾尝试将 src_path 上的“/”更改为“\”,但这没有用。有任何想法吗?
Here is my code:
这是我的代码:
import cv2
import numpy as np
import pytesseract
from PIL import Image
from pytesseract import image_to_string
# Path of working folder on Disk
src_path = "C:/Users/Benji's Beast/Desktop/image.PNG"
def get_string(img_path):
# Read image with opencv
img = cv2.imread(img_path)
# Convert to gray
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply dilation and erosion to remove some noise
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
# Write image after removed noise
cv2.imwrite(src_path + "removed_noise.png", img)
# Apply threshold to get image with only black and white
#img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
# Write the image after apply opencv to do some ...
cv2.imwrite(src_path + "thres.png", img)
# Recognize text with tesseract for python
result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))
# Remove template file
#os.remove(temp)
return result
print('--- Start recognize text from image ---')
print(get_string(src_path + "cont.jpg") )
print("------ Done -------")
I have no idea how to fix this, thanks.
我不知道如何解决这个问题,谢谢。
回答by rahul4data
This means you are passing a Uninitialized variable to
这意味着您将一个未初始化的变量传递给
> cv2.cvtColor()
After this statement:
在此声明之后:
# Read image with opencv
img = cv2.imread(img_path)
Can you try to print the img variable before passing to cv2.cvtColor() function
您可以尝试在传递给 cv2.cvtColor() 函数之前打印 img 变量吗
> print(img) or print(img.shape)
to make sure function call to read the image is successful
确保读取图像的函数调用成功
回答by Ishara Madhawa
I think your source path should be:
我认为您的源路径应该是:
src_path = "C:/Users/Benji's Beast/Desktop/"
Because in here get_string(src_path + "cont.jpg")
you've concatenated the image name.
因为在这里get_string(src_path + "cont.jpg")
您已经连接了图像名称。
回答by gameon67
The problems are this one
问题是这个
src_path = "C:/Users/Benji's Beast/Desktop/image.PNG"
and this one
和这个
print(get_string(src_path + "cont.jpg") )
You are appending the image input file name from image.PNGto image.PNG.cont.jpg
您将图像输入文件名从image.PNG 附加到image.PNG.cont.jpg
If your input image filename is cont.jpg and it is located on your Desktop, then try to replace your code with :
如果您的输入图像文件名是 cont.jpg 并且它位于您的桌面上,请尝试将您的代码替换为:
src_path = "C:\Users\Benji's Beast\Desktop\"
and
和
print(get_string(src_path + "cont.jpg") )
回答by Isaac Patole
if path and image name is verified and correct then just close the jupyter notebook (or whatever platform you are using) and restart it. It worked for me.
如果路径和图像名称已验证且正确,则只需关闭 jupyter notebook(或您使用的任何平台)并重新启动它。它对我有用。