Nagaraj
Nagaraj

Reputation: 668

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 can I solve this?

Note: I have all the prerequisites needed to execute this (Python 2.7, OpenCV 3.3, Matplotlib, and NumPy)

Upvotes: 59

Views: 321283

Answers (9)

Logan Young
Logan Young

Reputation: 93

Use 0 rather than cv2.IMREAD_GRAYSCALE.

And I would hard code the location of the file rather than refer to it like that. For example, if it was on the C drive, use 'C:\\Filename.jpg'.

Upvotes: 0

Ichsan
Ichsan

Reputation: 828

Use:

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 uses cv2.COLOR_BGR2RGB, because it converts the default settings of OpenCV which is using the BGR to RGB format.

Upvotes: 3

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

Upvotes: -1

ralf htp
ralf htp

Reputation: 9422

There is a tutorial on 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()

Use an absolute path to the image, and then you don't have any path problems.

Absolute and relative paths

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

Upvotes: 22

ksp585
ksp585

Reputation: 1770

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

Upvotes: 49

Muhammad Faizan Khan
Muhammad Faizan Khan

Reputation: 10551

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.

Upvotes: 11

Faizi
Faizi

Reputation: 460

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.

Upvotes: 0

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)

Upvotes: 5

lucians
lucians

Reputation: 2269

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

Upvotes: 6

Related Questions