[英]Numpy 3d Matrix to 2d Matrix
Hello I have this current matrix: 您好,我有这个当前矩阵:
[[[223 215 213]
[222 213 214]
[222 213 214]
...
[229 223 223]
[229 223 223]
[229 223 223]]
[[220 211 212]
[220 211 212]
[221 212 213]
...
[229 220 221]
[229 220 221]
[227 221 221]]
[[219 210 211]
[219 210 213]
[220 209 213]
...
[229 220 221]
[229 220 221]
[229 220 221]]
...
[[ 31 38 93]
[ 48 54 112]
[ 95 105 167]
...
[142 147 202]
[148 151 202]
[135 141 189]]
[[ 33 42 101]
[ 64 74 133]
[ 97 108 170]
...
[140 146 198]
[142 148 200]
[131 137 189]]
[[ 44 56 116]
[ 91 101 162]
[ 98 109 171]
...
[139 145 195]
[129 135 187]
[125 130 186]]]
And I need to turn it into three separate 2d Matrices representing R, G, B values of an Image 我需要将其转换为代表图像的R,G,B值的三个单独的2d矩阵
Here's the code I tried and here's my result for just the R array: 这是我尝试过的代码,这只是R数组的结果:
R = img1.transpose(2,0,1).reshape(300, -1)
Result: 结果:
[[223 222 222 ... 229 229 229]
[220 219 217 ... 230 230 229]
[221 222 222 ... 229 229 229]
...
[ 96 73 71 ... 196 190 190]
[103 94 106 ... 196 209 197]
[ 93 112 167 ... 195 187 186]]
What it should be: 应该是什么:
[[223 222 222 ... 229 229 229]
[220 220 221 ... 229 229 227]
[219 219 220 ... 229 229 229]
...
[ 31 48 95 ... 142 148 135]
[ 33 64 97 ... 140 142 131]
[ 44 91 98 ... 139 129 125]]
Any help on how to reach this format would be appreciated! 关于如何达到此格式的任何帮助将不胜感激!
You can use slices to extract along specific axes. 您可以使用切片沿特定轴提取。
I believe in your particular case, you should do: 我相信在您的特定情况下,您应该这样做:
red = data[:, :, 0] #Take all values along the first dimension, all values along the second dimension and only the first value along the third dimension
green = data[:, :, 1]
blue = data[:, :, 2]
you can use slicing. 您可以使用切片。
arr = np.array([[[1,2,3],[4,5,6],[7,8,9]],[[1,2,3],[4,5,6],[7,8,9]],[[1,2,3],[4,5,6],[7,8,9]]])
R = arr[:,:,0]
G = arr[:,:,1]
B = arr[:,:,2]
consider you have numpy array named img 考虑您有名为img的numpy数组
print img.shape
gives 给
(64,64,3)
and you want to create three variables R, G and B for each chanell as matrix so: 并且您想要为每个chanell创建三个变量R,G和B作为矩阵,因此:
R,G,B = img[:,:,0], img[:,:,1], img[:,:,2]
print "R:" R.shape, "G:", G.shape, "B:", B.shape
gives the shapes 给出形状
R: (64,64) G: (64,64) B: (64,64)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.