如何在Python OpenCV中读取图像

如何在Python OpenCV中读取图像,python,opencv,numpy,matplotlib,Python,Opencv,Numpy,Matplotlib,我试图在Python OpenCV中读取和显示图像 执行以下代码: 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() 导致以下错误: cv2.1错误: C:\build\master\u wi

我试图在Python OpenCV中读取和显示图像

执行以下代码:

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()
导致以下错误:

cv2.1错误: C:\build\master\u winpack-bindings-win64-vc14-static\opencv\modules\highgui\src\window.cpp:325:错误:(-215)函数cv::imshow中的size.width>0和size.height>0

如何解决这个问题

注意:我有执行此操作所需的所有先决条件(python 2.7、opencv 3.3)
matplotlib,numpy)

要使用OpenCV读取图像,必须使用以下Syntax。如果不起作用,说明安装有问题

import cv2

image = cv2.imread('path_of_the_image.png')

cv2.imshow('img', image)
cv2.waitKey(0)
你没有发布它给出的错误

编辑:我不明白负面的观点…关于什么???

有一个关于

使用图像的绝对路径,则没有路径问题


使用0而不是
cv2.IMREAD\u GRAYSCALE
,我会硬编码文件的位置,而不是像这样引用它。例如,如果文件位于C驱动器上,请输入
'C:\\Filename.jpg'
如果您试图使用matplotlib显示OpenCV图像,请使用下面的代码

import numpy as np
import cv2
import matplotlib.pyplot as plt
%matplotlib inline  # if you are running this code in Jupyter notebook

# reads image 'opencv-logo.png' as grayscale
img = cv2.imread('/path_to_image/opencv-logo.png', 0) 
plt.imshow(img, cmap='gray')

出现此错误消息的原因是cv2.imread()无法找到正在查找图像的图像。如果将完整路径添加到图像中,这应该会起作用,如

img = cv2.imread('/home/foo/images/dumb.jpg',cv2.IMREAD_GRAYSCALE)
试试这个:

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()
我写了一封信。您可以看到下面的代码段及其说明

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.

看起来你的代码很好。问题似乎在于参数中提供的图像尺寸。错误代码显示:
size>0&&width>0
。此条件未正确满足。尺寸
size
width
小于零。您可能需要检查任何其他图像并尝试使用该图像。

cv2.error:C:\build\master\u winpack-bindings-win64-vc14-static\opencv\modules\highgui\src\window.cpp:325:error:(-215)size.width>0&&size.height>0,在函数cv::imshowdelete cv2.IMREAD\u灰度中,并使用双反斜杠设置图像的正确路径`\\`
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.