Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cv2.imread: "<built-in function imread> returned NULL without setting an error", as if it can't open the picture or get the data

Tags:

python

cv2

imread

This is the part of my code that gives the problem. It is supposed to count the amount of green pixels in a picture:

img = Image.open('path.tif')

BLACK_MIN = np.array([0, 20, 20], np.uint8)

BLACK_MAX = np.array([120, 255, 255], np.uint8)

imgg = cv2.imread(img, 1)

dst = cv2.inRange(imgg, BLACK_MIN, BLACK_MAX)

no_black = cv2.countNonZero(dst)

print('The number of black pixels is: ' + str(no_black))
like image 701
anoold Avatar asked Sep 26 '19 19:09

anoold


People also ask

What will the cv2 Imread () return if a wrong image name is passed as argument?

Typically, the cv2. imread function will return None if the path to the input image is invalid, so be sure to double-check and triple-check your input image paths!

What does Imread return cv2?

cv2. imread() method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix.

Can Imread read JPG?

imread does not read jpg files.

What is the difference between CV2 imread and imread_color?

cv2.IMREAD_COLOR reads the image with RGB colors but no transparency channel. This is the default value for the flag when no value is provided as the second argument for cv2 imread(). cv2.IMREAD_GRAYSCALE reads the image as grey image. If the source image is color image, grey value of each pixel is calculated and is read into the array.

What if the image cannot be read by CV2?

NOTE: It returns an empty matrix ( Mat::data==NULL ) if the image cannot be read because of any reason (like missing file, improper permissions, unsupported or invalid format). Following are the image formats supported by cv2.imread () method:

What is the default value of imread_color in C++?

NOTE: By default, the value of this flag parameter is cv2.IMREAD_COLOR or 1. Return Value: cv2.imread () method returns a numpy.ndarray (NumPy N-dimensional array) if the loading of the image is successful.

What is CV2 imread_grayscale in C++?

cv2.IMREAD_GRAYSCALE: It specifies to load an image in grayscale mode. Alternatively, we can pass integer value 0 for this flag. cv2.IMREAD_UNCHANGED: It specifies to load an image as such including alpha channel.


1 Answers

You are passing a PIL image to imread but it expects a filepath (https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat%20imread(const%20string&%20filename,%20int%20flags)

You should use:

imgg = cv2.imread('path.tif', 1)
like image 193
Blue Three Wheeler Avatar answered Sep 22 '22 07:09

Blue Three Wheeler