Suppose I have an RGB (or HSV) image represented by an (M, N, 3) numpy array wherein each dimension ( [x, y, 0]
or [x, y, 1]
) represents a color channel value at a specific pixel. I want to reshape the array to (M*N, 3) where the color channels are combined ( [R1, G1, B1], [R2, G2, B2]
...) into a flat list (is that the correct terminology in this case?). I understand that one must use the reshape function, but I'm having difficulty understanding how to use the function. Any help is appreciated.
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]
would be the G value for a specific pixel. I want to take all 3 color channels and combine them into the following output.
Output: ([R, G, B], [R, G, B], [R, G, B]...)
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]])
If I understood you correctly, then, I would do something like this:
## loading the image...
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread("my_image.jpg")
## extracting R, G, B and flatten them separately and then make
## an array out of them.
res = np.transpose(np.array([image[...,0].flatten(), image[...,1].flatten(), image[...,2].flatten()]))
This might not be the most elegant way, but it will work. Actually, this do not give (M, 3)
but it give (MxN, 3)
. This should be actually desired, because with (M, 3)
you are loosing some data!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.