可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 :
回答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')