Python numpy array manipulation -


i need manipulate numpy array:

my array has followng format:

x = [1280][720][4] 

the array stores image data in third dimension:

x[0][0] = [red,green,blue,alpha] 

now need manipulate array following form:

x = [1280][720] x[0][0] = red + green + blue / 3  

my current code extremly slow , want use numpy array manipulation speed up:

for in range(0,719):     b in range(0,1279):         newx[a][b] = x[a][b][0]+x[a][b][1]+x[a][b][2] x = newx             

also, if possible need code work variable array sizes.

thansk alot

use numpy.mean function:

import numpy np  n = 1280 m = 720  # generate n * m * 4 matrix random values  x = np.round(np.random.rand(n, m, 4)*10)  # calculate mean value on first 3 values along 2nd axix (starting 0)  xnew = np.mean(x[:, :, 0:3], axis=2) 
  • x[:, :, 0:3] gives first 3 values in 3rd dimension, see: numpy indexing

  • axis=2 specifies, along axis of matrix mean value calculated.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -