可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
cv2.imread
is always returning NoneType
.
I am using python version 2.7 and OpenCV 2.4.6 on 64 bit Windows 7.
Maybe it's some kind of bug or permissions issue because the exact same installation of python and cv2 packages in another computer works correctly. Here's the code:
im = cv2.imread("D:\testdata\some.tif",CV_LOAD_IMAGE_COLOR)
I downloaded OpenCV from http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv. Any clue would be appreciated.
回答1:
It might indeed be an OpenCV bug that is not resolved yet. cv2.imread
is not working properly under Win32 for me either.
In the meanwhile, use LoadImage, which should work fine.
im = cv2.cv.LoadImage("D:\testdata\some.tif",CV_LOAD_IMAGE_COLOR)
回答2:
Try changing the direction of the slashes
im = cv2.imread("D:/testdata/some.tif",CV_LOAD_IMAGE_COLOR)
or add r to the begining of the string
im = cv2.imread(r"D:\testdata\some.tif",CV_LOAD_IMAGE_COLOR)
回答3:
just stumbled upon this one.
The solution is very simple but not intuitive.
- if you use relative paths, you can use either '\' or '/' as in
test\pic.jpg
or test/pic.jpg
respectively - if you use absolute paths, you should only use '/' as in
/.../test/pic.jpg
for unix or C:/.../test/pic.jpg
for windows - to be on the safe side, just use
for root, _, files in os.walk():
in combination with abs_path = os.path.join(root, file)
. Calling imread afterwards, as in img = ocv.imread(abs_path)
is always going to work.
回答4:
>>> im=cv2.imread("C:\Users\Virgile\Downloads\red.JPG") >>> print im None >>> im=cv2.imread("C:/Users/Virgile/Downloads/red.JPG") >>> print im [[[ 15 36 51] [ 18 34 51] [ 19 33 51] ...,
(tested with opencv 3.0.0)
回答5:
This took a long time to resolve. first make sure that the file is in the directory and check that even though windows explorer says the file is "JPEG" it is actually "JPG". The first print statement is key to making sure that the file actually exists. I am a total beginner, so if the code sucks, so be it. The code, just imports a picture and displays it . If the code finds the file, then True will be printed in the python window.
import cv2 import sys import numpy as np import os image_path= "C:/python27/test_image.jpg" print os.path.exists(image_path) CV_LOAD_IMAGE_COLOR = 1 # set flag to 1 to give colour image CV_LOAD_IMAGE_COLOR = 0 # set flag to 0 to give a grayscale one img = cv2.imread(image_path,CV_LOAD_IMAGE_COLOR) print img.shape cv2.namedWindow('Display Window') ## create window for display cv2.imshow('Display Window', img) ## Show image in the window cv2.waitKey(0) ## Wait for keystroke cv2.destroyAllWindows() ## Destroy all windows
回答6:
I had a similar issue,changing direction of slashes worked
回答7:
I spent some time on this only to find that this error is caused by a broken image file on my case. So please manually check your file to make sure it is valid and can be opened by common image viewers.
回答8:
In my case the problem was the spaces in the path. After I moved the images to a path with no spaces it worked.
回答9:
I've run into this. Turns out the PIL module provides this functionality. Similarly, numpy.imread and scipy.misc.imread both didn't exist until I installed PIL
In my configuration (win7 python2.7), that was done as follows:
cd /c/python27/scripts easy_install PIL