Error "TypeError: Expected Ptr<cv::UMat> for argument 'mat'" when trying to display images from an Array

Viewed 1486

I've read in all the photos from a folder into an array. I'm trying to confirm that this works by displaying the first photos 5 in the array. Here's what I've tried:

def read_img(img_list, img):
    n = cv2.imread(img, 0)
    img_list.append(n)
    return img_list

path = glob.glob("foo/images/*.png")
list_ = []

cv_image = [read_img(list_, img) for img in path]

for i in range(5):
    cv2.imshow("images", cv_image[i])
    cv2.waitKey(0)

I'm currently getting the error: cv2.imshow("images", cv_image[i]) TypeError: Expected Ptr<cv::UMat> for argument 'mat'

I'm not very familiar with OpenCV and haven't found a solution online so far. Please let me know.

1 Answers

In your for loop, when accessing the images from your list cv_image, you need to have another [0] index to "enter" into the array of the read image. Try:

for i in range(5):
    cv2.imshow("images", cv_image[i][0])  # The zero is to *dive* into the list of image arrays
    cv2.waitKey(0)
Related