numpy 如何在Python OpenCV中读取图像

czq61nw1  于 11个月前  发布在  Python
关注(0)|答案(9)|浏览(118)

我正在尝试在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.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
我该怎么解决这个问题?

  • 注意 *:我已经具备了执行此操作所需的所有先决条件(Python 2.7、OpenCV 3.3、Matplotlib和NumPy)
7z5jn7bk

7z5jn7bk1#

如果您尝试使用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')

字符串

rwqw0loc

rwqw0loc2#

有一个关于 Getting Started with Images 的教程。

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('/path_to_image/messi5.jpg',0)

# Show the image
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

字符串
使用图像的 * 绝对 * 路径,这样就不会有任何路径问题。

byqmnocz

byqmnocz3#

我写了一个short post to learn image reading with OpenCV in Python。你可以看到下面的代码片段的描述。

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.

字符串

2fjabf4q

2fjabf4q4#

要使用OpenCV读取图像,您必须使用下面的JavaScript。如果它不工作,则说明安装有问题。

import cv2

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

cv2.imshow('img', image)
cv2.waitKey(0)

字符串
你没有发布它给出的错误。
编辑:我不明白负面观点……为了什么?

j13ufse2

j13ufse25#

此错误消息的原因是cv2.imread()无法在查找图像的位置找到图像。

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

字符串

pepwfjgg

pepwfjgg6#

用途:

import cv2
import matplotlib.pyplot as plt
%matplotlib inline
        
# Hide grid lines
fig, ax = plt.subplots(figsize=(10,10))
ax.grid(False)
    
im=cv2.imread('./my_images.jpg')
plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
plt.show()

字符串
它使用cv2.COLOR_BGR2RGB,因为它将使用BGR的OpenCV的默认设置转换为RGB格式。

mepcadol

mepcadol7#

看起来你的代码很好。问题似乎是在参数中提供的图像的尺寸中。错误代码说:size > 0 && width > 0。这个条件没有正确满足。尺寸sizewidth小于零。你可能想检查任何其他图像并尝试使用它。

nwsw7zdq

nwsw7zdq8#

使用0而不是cv2.IMREAD_GRAYSCALE
我会硬编码文件的位置,而不是像这样引用它。例如,如果它在C驱动器上,使用'C:\\Filename.jpg'

vawmfj5a

vawmfj5a9#

试试这个:

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()

字符串

相关问题