如何在 Python OpenCV 中读取图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46540831/
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
How to read an image in Python OpenCV
提问by Nagaraj
I am trying to read and display an image in Python OpenCV.
我正在尝试在 Python OpenCV 中读取和显示图像。
Executing the following code:
执行以下代码:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('dumb.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Results in the following error:
导致以下错误:
cv2.error: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\highgui\src\window.cpp:325: error: (-215) size.width>0 && size.height>0 in function cv::imshow
cv2.error: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\highgui\src\window.cpp:325: 错误: (-215) size.width>0 && size.height>0在函数 cv::imshow 中
How to solve this?
如何解决这个问题?
NOTE: I have all the prerequisites needed to execute this (python 2.7, opencv 3.3 matplotlib, numpy)
注意:我拥有执行此操作所需的所有先决条件(python 2.7、opencv 3.3 matplotlib、numpy)
回答by ksp585
@Nagaraj - If you are trying to display openCV image using matplotlib, use the code below.
@Nagaraj - 如果您尝试使用 matplotlib 显示 openCV 图像,请使用以下代码。
import numpy as np
import cv2
import matplotlib.pyplot as plt
%matplotlib inline # if you are running this code in jupyter notebook
img = cv2.imread('/path_to_image/opencv-logo.png',0) # reads image 'opencv-logo.png' as grayscale
plt.imshow(img, cmap='gray')
回答by Muhammad Faizan Khan
Here is a short postto learn image reading with OpenCV in python. You can see the below code snippet with the description.
这是一篇关于在 Python 中使用 OpenCV 学习图像阅读的简短帖子。您可以看到以下带有说明的代码片段。
import cv2 #Import openCV
import sys #import Sys. Sys will be used for reading from the command line. We give Image name parameter with extension when we will run python script
#Read the image. The first Command line argument is the image
image = cv2.imread(sys.argv[1]) #The function to read from an image into OpenCv is imread()
#imshow() is the function that displays the image on the screen.
#The first value is the title of the window, the second is the image file we have previously read.
cv2.imshow("OpenCV Image Reading", image)
cv2.waitKey(0) #is required so that the image doesn't close immediately. It will Wait for a key press before closing the image.
回答by ralf htp
there is a tutorial on http://docs.opencv.org/3.1.0/dc/d2e/tutorial_py_image_display.html
http://docs.opencv.org/3.1.0/dc/d2e/tutorial_py_image_display.html上有一个教程
import numpy as np
import cv2
# Load an color image in grayscale
img = cv2.imread('/path_to_image/messi5.jpg',0)
# show image
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
use an absolutepath to the image then you have no path problems
使用图像的绝对路径,那么你就没有路径问题
https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths
https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths
OpenCV Error: (-215)size.width>0 && size.height>0 in function imshow
OpenCV 错误:(-215)size.width>0 && size.height>0 in function imshow
回答by Link
To read an image with OpenCV you have to use the following synthax. If it doesn't work, there is a problem with the installation.
要使用 OpenCV 读取图像,您必须使用以下合成器。如果它不起作用,则安装有问题。
import cv2
image = cv2.imread('path_of_the_image.png')
cv2.imshow('img', image)
cv2.waitKey(0)
You didn't post the error it gives..
你没有发布它给出的错误..
EDIT: I don't understand the negative points...for what ??
编辑:我不明白消极的观点......为了什么??
回答by George Deeley
Use 0 rather than cv2.IMREAD_GRAYSCALE
and I would hard code the location of the file rather than refer to it like that for example if it was on the C drive put 'C:\\Filename.jpg'
使用 0 而不是cv2.IMREAD_GRAYSCALE
,我会硬编码文件的位置,而不是像那样引用它,例如,如果它在 C 驱动器上 put'C:\\Filename.jpg'
回答by Faizi
It looks like your code is fine. The problem seems to be in the dimension of image that is being provided in the parameters. The error code says: size > 0 && width > 0
. This condition is not meeting properly. Either dimension size
or width
is less than zero. You may want to check any other image and try with that.
看起来你的代码没问题。问题似乎出在参数中提供的图像维度上。错误代码说:size > 0 && width > 0
。此条件未正确满足。维度size
或width
小于零。您可能想要检查任何其他图像并尝试使用它。
回答by Totoro
The reason for this error message is that cv2.imread() was unable to find the image where it was looking for the image. This should work if you add the full path to the image, like
出现此错误消息的原因是 cv2.imread() 无法在它寻找图像的地方找到图像。如果您将完整路径添加到图像,这应该可以工作,例如
img = cv2.imread('/home/foo/images/dumb.jpg',cv2.IMREAD_GRAYSCALE)
回答by Phani Kumar Bollapragada
Try this one :
试试这个:
import cv2 as cv #openCV-3.4.1
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread('image path and name .file type ',0)
cv.imshow('img',img)
cv.waitKey(0)
cv.destroyAllWindows()
回答by Rahul solanki
Sometime the error comes because the path is incorrect,you can check this by printing the value return by imread if none is output then file is not found
有时错误是因为路径不正确,如果没有输出则没有找到文件,您可以通过打印 imread 返回的值来检查这一点
回答by Amr Shaheen
You may find a path above the error that shown to you.
您可能会在显示给您的错误上方找到一条路径。
It's like:- File "PATH", line ... , in
它就像:-文件“PATH”,行...,在
Copy the image into this path and try again.
将图像复制到此路径并重试。