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))
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!
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.
imread does not read jpg files.
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.
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:
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With