Python OpenCV !_src.empty() 函数“cvtColor”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52676020/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:09:43  来源:igfitidea点击:

OpenCV !_src.empty() in function 'cvtColor' error

pythonopencv

提问by Charklewis

I am trying to do a basic colour conversion in python however I can't seem to get past the below error. I have re-installed python, opencv and tried on both python 3.4.3 (latest) and python 2.7 (which is on my Mac).

我正在尝试在 python 中进行基本的颜色转换,但是我似乎无法克服以下错误。我已经重新安装了 python、opencv 并尝试了 python 3.4.3(最新)和 python 2.7(在我的 Mac 上)。

I installed opencv using python's package manager opencv-python.

我使用 python 的包管理器 opencv-python 安装了 opencv。

Here is the code that fails:

这是失败的代码:

frame = cv2.imread('frames/frame%d.tiff' % count)
frame_HSV= cv2.cvtColor(frame,cv2.COLOR_RGB2HSV)

This is the error message:

这是错误消息:

cv2.error: OpenCV(3.4.3) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

回答by shaked litbak

This error happened because the image didn't load properly . So you have problem with the previous line cv2.imreadmy suggestion is :

发生此错误是因为图像未正确加载。所以你对上一行有问题,cv2.imread我的建议是:

  • check if the images exist in the path you give

  • check the count variable if he have valid number

  • 检查图像是否存在于您提供的路径中

  • 如果他有有效数字,请检查计数变量

回答by Ramandeep Singh

Check whether its the jpg, png, bmpfile that you are providing and write the extension accordingly.

检查它是否是您提供的jpgpngbmp文件并相应地编写扩展名。

回答by D Jacobson

I kept getting this error too:

我也一直收到这个错误:

Traceback (most recent call last):
  File "face_detector.py", line 6, in <module>
    gray_img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor

My cv2.cvtColor(...)was working fine with \photo.jpgbut not with \news.jpg. For me, I finally realized that when working on Windows with python, those escape characters will get you every time!! So my "bad" photo was being escaped because of the file name beginning with "n". Python took the \nas an escape character and OpenCV couldn't find the file!

我的cv2.cvtColor(...)工作正常,\photo.jpg但与\news.jpg. 对我来说,我终于意识到,当使用 python 在 Windows 上工作时,这些转义字符每次都会得到你!!所以我的“坏”照片因为文件名以“n”开头而被转义。Python 将\n用作转义字符,而 OpenCV 找不到该文件!

Solution:
Preface file names in Windows python with r"...\...\"as in

解决方案:
在 Windows python 中使用r"...\...\"as in 为文件名做序言

cv2.imread(r".\images\news.jpg")

回答by Leonardo Mariga

If anyone is experiencing this same problem when reading a frame from a webcam:

如果有人在从网络摄像头读取帧时遇到同样的问题:

Verify if your webcam is being used on another task and close it. This wil solve the problem.

验证您的网络摄像头是否正在用于其他任务并关闭它。这将解决问题。

I spent some time with this error when I realized my camera was online in a google hangouts group. Also, Make sure your webcam drivers are up to date

当我意识到我的相机在谷歌环聊组中在线时,我花了一些时间来解决这个错误。另外,请确保您的网络摄像头驱动程序是最新的

回答by Sergey Inshev

If the path is correct and the name of the image is OK, but you are still getting the error

如果路径正确且图像名称正确,但您仍然收到错误

use:

用:

from skimage import io

img = io.imread(file_path)

instead of:

代替:

cv2.imread(file_path)

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).

函数 imread 从指定文件加载图像并返回它。如果无法读取图像(由于文件丢失、权限正确、格式不受支持或无效),该函数将返回一个空矩阵 ( Mat::data==NULL )。

回答by sdh4967

I've been in same situation as well, and My case was because of the Korean letter in the path...

我也遇到过同样的情况,我的情况是因为路径中的韩文字母...

After I remove Korean letters from the folder name, it works.

从文件夹名称中删除韩文字母后,它可以工作。

OR put

或放

[#-*- coding:utf-8 -*-]

(except [ ]at the edge)

[ ]边缘除外)

or something like that in the first line to make python understand Korean or your language or etc. then it will work even if there is some Koreans in the path in my case.

或者在第一行中使用类似的东西让python理解韩语或您的语言等。那么即使在我的情况下路径中有一些韩国人,它也会起作用。

So the things is, it seems like there is something about path or the letter. People who answered are saying similar things. Hope you guys solve it!

所以事情是,似乎有一些关于路径或字母的东西。回答的人都在说类似的话。希望大家解决!

回答by waeiski

Another thing which might be causing this is a 'weird' symbol in your file and directory names. All umlaut (???) and other (éóa etc) characters should be removed from the file and folder names. I've had this same issue sometimes because of these characters.

可能导致这种情况的另一件事是文件和目录名称中的“奇怪”符号。应从文件和文件夹名称中删除所有变音符号 (???) 和其他(éóa 等)字符。由于这些角色,我有时会遇到同样的问题。

回答by user12422526

In my case it was a permission issue. I had to:

就我而言,这是一个许可问题。我不得不:

  • chmod a+wrxthe image,
  • chmod a+wrx图片,

then it worked.

然后它起作用了。

回答by Mayur Gupta

must please see guys that the error is in the cv2.imread() .Give the right path of the image. and firstly, see if your system loads the image or not. this can be checked first by simple load of image using cv2.imread(). after that ,see this code for the face detection

必须请大家看看错误出在 cv2.imread() 中。给出图像的正确路径。首先,看看你的系统是否加载了图像。这可以首先通过使用 cv2.imread() 简单加载图像来检查。之后,请参阅此代码以进行人脸检测

import numpy as np
import cv2

cascPath = "/Users/mayurgupta/opt/anaconda3/lib/python3.7/site-   packages/cv2/data/haarcascade_frontalface_default.xml"

eyePath = "/Users/mayurgupta/opt/anaconda3/lib/python3.7/site-packages/cv2/data/haarcascade_eye.xml"

smilePath = "/Users/mayurgupta/opt/anaconda3/lib/python3.7/site-packages/cv2/data/haarcascade_smile.xml"

face_cascade = cv2.CascadeClassifier(cascPath)
eye_cascade = cv2.CascadeClassifier(eyePath)
smile_cascade = cv2.CascadeClassifier(smilePath)

img = cv2.imread('WhatsApp Image 2020-04-04 at 8.43.18 PM.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
    img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here, cascPath ,eyePath ,smilePath should have the right actual path that's picked up from lib/python3.7/site-packages/cv2/data here this path should be to picked up the haarcascade files

在这里, cascPath ,eyePath ,smilePath 应该具有从 lib/python3.7/site-packages/cv2/data 中获取的正确实际路径,此路径应该用于获取 haarcascade 文件

回答by Peter

I had the same problem and it turned out that my image names included special characters(e.g. chateau.jpg), which could not bet handled by cv2.imread. My solution was to make a temporary copy of the file, renaming it e.g. temp.jpg, which could be loaded by cv2.imreadwithout any problems.

我遇到了同样的问题,结果我的图像名称包含特殊字符(例如 chateau.jpg),无法通过cv2.imread. 我的解决方案是制作文件的临时副本,将其重命名,例如temp.jpg,可以毫无问题地加载它cv2.imread

Note: I did not check the performance of shutil.copy2vice versa other options. So probably there is a better/faster solution to make a temporary copy.

注意:我没有检查shutil.copy2反之亦然其他选项的性能。所以可能有更好/更快的解决方案来制作临时副本。

import shutil, sys, os, dlib, glob, cv2

for f in glob.glob(os.path.join(myfolder_path, "*.jpg")):
    shutil.copy2(f, myfolder_path + 'temp.jpg')
    img = cv2.imread(myfolder_path + 'temp.jpg')
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    os.remove(myfolder_path + 'temp.jpg')

If there are only few files with special characters, renaming can also be done as an exeption, e.g.

如果只有少数带有特殊字符的文件,重命名也可以作为例外,例如

for f in glob.glob(os.path.join(myfolder_path, "*.jpg")):
    try:
        img = cv2.imread(f)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    except:
        shutil.copy2(f, myfolder_path + 'temp.jpg')
        img = cv2.imread(myfolder_path + 'temp.jpg')
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        os.remove(myfolder_path + 'temp.jpg')