DeeWBee - 1 year ago 69

Python Question

Suppose I have an RGB (or HSV) image represented by an (M, N, 3) numpy array wherein each dimension (

`[x, y, 0]`

`[x, y, 1]`

`[R1, G1, B1], [R2, G2, B2]`

EDIT: Here is an example of what I would like to happen.

Input: (640 x 640 x 3) array representative of image where

`[40, 40, 1]`

Output:

`([R, G, B], [R, G, B], [R, G, B]...)`

Answer Source

If `img`

is your array, you can use `img.reshape(-1, 3)`

.

For example,

```
In [50]: img.shape
Out[50]: (5, 2, 3)
In [51]: img
Out[51]:
array([[[2, 0, 4],
[1, 4, 3]],
[[2, 1, 4],
[3, 2, 2]],
[[2, 4, 1],
[4, 0, 2]],
[[1, 4, 2],
[3, 2, 2]],
[[3, 2, 1],
[2, 1, 0]]])
In [53]: x = img.reshape(-1, 3)
In [54]: x.shape
Out[54]: (10, 3)
In [55]: x
Out[55]:
array([[2, 0, 4],
[1, 4, 3],
[2, 1, 4],
[3, 2, 2],
[2, 4, 1],
[4, 0, 2],
[1, 4, 2],
[3, 2, 2],
[3, 2, 1],
[2, 1, 0]])
```