'OpenCV(4.5.5) error:-1: error: (-5:Bad argument) in function 'imshow'

I'm very new to openCV and I'm trying to create a collage of multiple images, but I keep getting the error:

error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'imshow'
> Overload resolution failed:
>  - mat data type = 19 is not supported
>  - Expected Ptr<cv::cuda::GpuMat> for argument 'mat'
>  - Expected Ptr<cv::UMat> for argument 'mat'

I think the problem is that my images are of the type CV_16S, which imshow doesn't support, however I don't know how to change the data type. The full code is here:

import cv2
import numpy as np
import glob
import os
import shutil

source_path = "C:/source/path/sampledata/"
destination_path = "C:/destination/path/sampledata2/"

if os.path.exists(destination_path):
    print("destination file already exists")
else:
        dir = os.mkdir(destination_path)
        files = glob.iglob(os.path.join(source_path, "*raw.png"))
        for file in files:
            if os.path.isfile(file):
                shutil.copy2(file, destination_path)

count=1

for file_name in os.listdir(destination_path):

    source = destination_path + file_name
    destination = destination_path + "im" + str(count) + ".png"
    os.rename(source, destination)
    globals()["im" + str(count)] = destination
    
    count += 1
print('All Files Renamed')

print('New Names are')
res = os.listdir(destination_path)
print(res)

path = glob.glob("C:/source/file/*.png")
for file in path:
    images = cv2.imread(file)
    images = cv2.resize(images,(50,50))
    images = cv2.flip(images, 0)
    
    h_stack1 = np.hstack([im1,im2,im3,im4,im5,im6,im7])
    h_stack2 = np.hstack([im14,im13,im12,im11,im10,im9,im8])
    h_stack3 = np.hstack([im15,im16,im17,im18,im19,im20,im21])
    h_stack4 = np.hstack([im28,im27,im26,im25,im24,im23,im22])
    h_stack5 = np.hstack([im29,im30,im31,im32,im33,im34,im35])
    h_stack6 = np.hstack([im42,im41,im40,im39,im38,im37,im36])
    h_stack7 = np.hstack([im43,im44,im45,im46,im47,im49,im49])
    
    v_stack = np.vstack([h_stack1,h_stack2,h_stack3,h_stack4,h_stack5,h_stack6,h_stack7])
    
    cv2.imshow("collage",v_stack)
    cv2.imwrite("collage.png",v_stack)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

How can I resolve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source