简体   繁体   中英

How to read an image in Python OpenCV

I am trying to read and display an image in 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

How to solve this?

NOTE: I have all the prerequisites needed to execute this (python 2.7, opencv 3.3 matplotlib, numpy)

If you are trying to display OpenCV image using matplotlib, use the code below.

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

I have written a short post to learn image reading with OpenCV in Python . You can see the below code snippet with the description.

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.

there is a tutorial on 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 absolute path to the image then you have no path problems

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

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

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.

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 ??

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

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

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

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

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.

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

It use cv2.COLOR_BGR2RGB because it convert default settings of OpenCV which using BGR to RGB format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM