1

I have 3 2D numpy float arrays (R, G, B) with the same dimensions. I want to merge it in one 2d array, causing every element in the new 2d array to be an array of 3 floating numbers. Below is my initial code:

image = cv2.imread('hurricane katrina 1.jpg', cv2.IMREAD_GRAYSCALE)

a = 255
b = 2 * (np.pi/255)
c = np.pi / 5


R = a * np.absolute(np.sin(b * image))
G = a * np.absolute(np.sin(b * image + c))
B = a * np.absolute(np.sin(b * image + 2 * c))

How will I do it in Python without iterating through the arrays? Thanks.

UPDATE:

I want to merge the 3 Arrays. Say for example

R[0][0] = 3 
G[0][0] = 4 
B[0][0] = 6

Then the merge array will be

RGB[0][0] = (3,4,6)

And this will be true for all elements in the arrays.

So the final output shape will be (1000,775,3)

1
  • Merge and concat are literally the one and the same thing in case of numpy and lists.. Commented Apr 11, 2018 at 5:20

1 Answer 1

2

np.concatenate((a, b,c), axis=see what you want, probably 1)

Or You can also use hstack...( They both are one and the same thing...)

Doing, rgb_transform = np.concatenate(R,G,B), throws an error. TypeError: only integer scalar arrays can be converted to a scalar index, But it shouldn't throw as I am already passing them in parenthesis

To resolve this, probably Do this np.concatenate([a, b, c], axis=see what you want, probably 1)

Refer the docs

Edit

What was asked is a bit different than above, so the correct answer is to do np.stack((r,g,b),2) as the final output is (1000,775,3)...

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

8 Comments

Doing, rgb_transform = np.concatenate(R,G,B), throws an error. TypeError: only integer scalar arrays can be converted to a scalar index
You are missing the extra parenthesis, they are necessary @Eliyah
The first parameter to concatenate should itself be a sequence of arrays to concatenate...
Thanks, I tried rgb_transform = np.concatenate([R,G,B], axis=0, out=None) and turns out working. But my problem is not address, I want to merge, not concantenate.
That solves the problem. Thank you, you can edit your answer so that I can mark it right. :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.