繁体   English   中英

Numpy 3d矩阵到2d矩阵

[英]Numpy 3d Matrix to 2d 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]]]

我需要将其转换为代表图像的R,G,B值的三个单独的2d矩阵

这是我尝试过的代码,这只是R数组的结果:

R = img1.transpose(2,0,1).reshape(300, -1)

结果:

[[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]]

应该是什么:

[[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]] 

关于如何达到此格式的任何帮助将不胜感激!

您可以使用切片沿特定轴提取。

我相信在您的特定情况下,您应该这样做:

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]

您可以使用切片。

 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]

考虑您有名为img的numpy数组

print img.shape

(64,64,3)

并且您想要为每个chanell创建三个变量R,G和B作为矩阵,因此:

R,G,B = img[:,:,0], img[:,:,1], img[:,:,2]

print "R:" R.shape, "G:", G.shape, "B:", B.shape

给出形状

R: (64,64) G: (64,64) B: (64,64)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM