Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert numpy matrix to cv2 image [python]

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).

my_matrix_screenshot

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?

like image 340
user2266175 Avatar asked Oct 14 '17 16:10

user2266175


People also ask

How do I display a NumPy array in an image in Python?

Create a sample Numpy array and convert the array to PIL format using fromarray() function of PIL. This will open a new terminal window where the image will be displayed. To save the Numpy array as a local image, use the save() function and pass the image filename with the directory where to save it.

Can cv2 read NumPy array?

OpenCV cv2 imread() You can read image into a numpy array using opencv library. The array contains pixel level data. And as per the requirement, you may modify the data of the image at a pixel level by updating the array values.


1 Answers

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

like image 125
shouhuxianjian Avatar answered Sep 20 '22 20:09

shouhuxianjian