如何在Python OpenCV中读取图像

Nag*_*raj 10 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()
Run Code Online (Sandbox Code Playgroud)

导致以下错误:

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在函数cv :: imshow中

如何解决呢?

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

ksp*_*585 9

@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')
Run Code Online (Sandbox Code Playgroud)


ral*_*htp 8

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()
Run Code Online (Sandbox Code Playgroud)

使用图像的绝对路径,那么你就没有路径问题

https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths

OpenCV 错误:(-215)size.width>0 && size.height>0 in function imshow


Muh*_*han 8

我写了一篇短文来学习在 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.
Run Code Online (Sandbox Code Playgroud)


Lin*_*ink 6

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

import cv2

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

cv2.imshow('img', image)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

您没有发布它给出的错误..

编辑:我不明白缺点......为什么?