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

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

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).

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

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' 

回答1:

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

  • check if the images exist in the path you give

  • check the count variable if he have valid number



回答2:

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



回答3:

In my case, the image was incorrectly named. Check if the image exists and try

import numpy as np import cv2  img = cv2.imread('image.png', 0) cv2.imshow('image', img) 


回答4:

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

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

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') 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!