cv2 imread returns None [duplicate]

荒凉一梦 提交于 2020-02-25 05:38:27

问题


I installed the cv2 library via pip install opencv-contrib-python-headless.

Whenever i try to read an image it it returns None. I tried using / instead \ in the path, storing the image in the project directory so no / in the path, using r' before the path, re-installing the package. Am i missing something ? woth noting that when i use other libraries to read an image using the same path it works fine.

import cv2
 ## I tried all tweaks in the path
img = cv2.imread(path)
print(img)

here are the paths i tried

img = cv2.imread('image.jpg')
print(img)

img = cv2.imread(r'D:\project\image.jpg')
print(img)

img = cv2.imread(r'D:/project/image.jpg')
print(img)

img = cv2.imread('D:/project/image.jpg')
print(img)

all the above returned None


回答1:


I recommend you to try again installing the CV2 or try to check the image addresses.

If you still have problems you can use other methods for reading the jpeg images.

As stated in this previous answer cv2.imread does not read jpg files you can use the matplotlib instead.

import cv2
import matplotlib.pyplot as plt
img1 = plt.imread('image.jpg')

Note that colour channels are different in matplotlib and it is BGR.

If you need to work with the colors you need to swap third and first channel.

img1 = img1[..., ::-1]  # RGB --> BGR

you can find more information about matplotlib here https://matplotlib.org/

Hope it solves your problem.



来源:https://stackoverflow.com/questions/54265107/cv2-imread-returns-none

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