9

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 OpenCV (OpenCV Python equalizeHist colored image) but the problem is I am unable to convert the 2d matrix to OpenCV matrix which takes three channels for RGB.

I was searching again but all I found is to convert regular 3d numpy matrix to OpenCV matrix so how can numpy 2d matrix be converted to OpenCV matrix which has 3 channels?

5
  • What is the output of my_matrix.shape? Commented Oct 14, 2017 at 16:52
  • The output is (90, 100) Commented Oct 14, 2017 at 17:16
  • "I have a numpy 2d matrix which represents a colored image" If you know the original shape of this color image, you can use np.reshape() to convert this to a 3D array Commented Oct 14, 2017 at 17:41
  • new_img = np.reshape(img,(rows,cols/3,3) Commented Oct 14, 2017 at 17:47
  • It gives an error: ValueError: cannot reshape array of size 9000 into shape (90,33,3) in this line: new_img = np.reshape(new_img_tmp,(int(new_img_tmp.shape[0]), int(new_img_tmp.shape[1]/3),3)) Commented Oct 14, 2017 at 19:18

2 Answers 2

10

because the numpy.ndarray is the base of OpenCV, 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

Sign up to request clarification or add additional context in comments.

1 Comment

This answer seems to be answering an entirely different question. The question didn't ask about resizing an array.
-3

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 np
import matplotlib.pyplot as plt

a = 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.

3 Comments

Thank you! but I get totally distracted image or you can say different image when I used the code above!
Hope 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.
of 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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.