How to convert numpy matrix to cv2 image [python] How to convert numpy matrix to cv2 image [python] numpy numpy

How to convert numpy matrix to cv2 image [python]


because the numpy.ndarray is the base of cv2, so you just write the code as usual,like

img_np = np.ones([100,100])img_cv = cv2.resize(img_np,(200,200))

you can try


It is better to stack the existing numpy array one above the other of its own copy than to reshape it and add the third axis. Check this code:

import numpy as npimport matplotlib.pyplot as plta = np.random.rand(90, 100) # Replace this line with your 90x100 numpy array.a = np.expand_dims(a, axis = 2)a = np.concatenate((a, a, a), axis = 2)print(a.shape)# (90, 100, 3)plt.imshow(a)plt.show()

You will get a gray colored image.