numpy 如何在Python OpenCV中读取图像

czq61nw1  于 2023-11-18  发布在  Python
关注(0)|答案(9)|浏览(146)

我正在尝试在Python OpenCV中读取和显示图像。
执行以下代码:

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. img = cv2.imread('dumb.jpg', cv2.IMREAD_GRAYSCALE)
  5. cv2.imshow('image',img)
  6. cv2.waitKey(0)
  7. 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图像,请使用以下代码。

  1. import numpy as np
  2. import cv2
  3. import matplotlib.pyplot as plt
  4. %matplotlib inline # if you are running this code in Jupyter notebook
  5. # reads image 'opencv-logo.png' as grayscale
  6. img = cv2.imread('/path_to_image/opencv-logo.png', 0)
  7. plt.imshow(img, cmap='gray')

字符串

rwqw0loc

rwqw0loc2#

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

  1. import numpy as np
  2. import cv2
  3. # Load an color image in grayscale
  4. img = cv2.imread('/path_to_image/messi5.jpg',0)
  5. # Show the image
  6. cv2.imshow('image',img)
  7. cv2.waitKey(0)
  8. cv2.destroyAllWindows()

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

展开查看全部
byqmnocz

byqmnocz3#

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

  1. import cv2 #Import openCV
  2. 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
  3. #Read the image. The first Command line argument is the image
  4. image = cv2.imread(sys.argv[1]) #The function to read from an image into OpenCv is imread()
  5. #imshow() is the function that displays the image on the screen.
  6. #The first value is the title of the window, the second is the image file we have previously read.
  7. cv2.imshow("OpenCV Image Reading", image)
  8. 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。如果它不工作,则说明安装有问题。

  1. import cv2
  2. image = cv2.imread('path_of_the_image.png')
  3. cv2.imshow('img', image)
  4. cv2.waitKey(0)

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

j13ufse2

j13ufse25#

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

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

字符串

pepwfjgg

pepwfjgg6#

用途:

  1. import cv2
  2. import matplotlib.pyplot as plt
  3. %matplotlib inline
  4. # Hide grid lines
  5. fig, ax = plt.subplots(figsize=(10,10))
  6. ax.grid(False)
  7. im=cv2.imread('./my_images.jpg')
  8. plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
  9. 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#

试试这个:

  1. import cv2 as cv # OpenCV 3.4.1
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. img = cv.imread('image path and name .file type ', 0)
  5. cv.imshow('img', img)
  6. cv.waitKey(0)
  7. cv.destroyAllWindows()

字符串

相关问题