How to convert numpy matrix to cv2 image [python]
32,482
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
Author by
user2266175
Updated on July 31, 2022Comments
-
user2266175 almost 2 years
I have a numpy 2d matrix which represents a colored image. This matrix has some negative and floating point numbers but of course I can display the image using imshow(my_matrix).
I need to perform histogram equalization to this colored image so I found a code here in stackoverflow using cv2 (OpenCV Python equalizeHist colored image) but the problem is I am unable to convert the 2d matrix to cv matrix which takes three channels for RGB.
I was searching again but all I found is to convert regular 3d numpy matrix to cv2 matrix so how can numpy 2d matrix be converted to cv2 matrix which has 3 channels?
-
user2266175 over 6 yearsThank you! but I get totally distracted image or you can say different image when I used the code above!
-
Prasad over 6 yearsHope you have not used
np.random.rand
line while running. You have to replace that line with your already existing 90x100 numpy array. I generated a random array just for the code completion sake. -
user2266175 over 6 yearsof course not. I just plugged my matrix over there ;) but I got the weird result. I don't know if it is possible to send you the original image and the resulted image. here is what I am doing: a = new_img_tmp 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()
-
Rafael_Espericueta almost 4 yearsThis answer seems to be answering an entirely different question. The question didn't ask about resizing an array.