-1

Although I am not new to python, I am a very infrequent user and get out of my depth pretty quickly. I’m guessing there is a simple solution to this which I am totally missing.

I have an image which is stored as a 2D numpy array. Rather than 3 values as with an RGB image, each pixel contains 6 numbers. The XY coordinates of a pixel is all stored as the row values of the array, while there are 6 columns which correspond to the different wavelength values. Currently, I can call up any single pixel and see what the wavelength values are, but I would like to add up the values over a range of pixels.

I know summing arrays is straightforward, and this is essentially what I'm looking to achieve over an arbitrary range of specified pixels in the image.

a=np.array([1,2,3,4,5,6])
b=np.array ([1,2,3,4,5,6])
sum=a+b
sum=[2,4,6,8,10,12]

I’m guessing I need a loop for specifying the range of input pixels I would like to be summed. Note, as with the example above, I’m not trying to sum the 6 values of one array, rather to sum the all the first elements, all the second elements, etc. However, I think what is happening is that the loop runs over the specified pixel range, but doesn’t store the values to be added to the next pixel. I’m not sure how to overcome this.

What I have so far is:

fileinput=np.load (‘C:\\Users\\image_data.npy’) #this is the image, each pixel is 6 values
exampledata=np.arange(42).reshape((7,6))
for x in range(1,4)
    signal=exampledata[x]
    print(exampledata[x]) #this prints all the arrays I would like to sum, ie it shows list of 6 values for each of the pixels specified within the range. 
    sum.exampledata[x] # sums up the values within each list, rather than all the first elements, all the second elements etc. 
    exampledata.sum(axis=1) # produces the error: AxisError: axis 1 is out of bounds for array of dimension 1

I suppose I could sum up a small range of pixels manually, though this only works for small ranges of pixels.

first=fileinput[1]
second=fileinput[2]
third=fileinput[3]
sum=first+second+third
Harry B
  • 351
  • 1
  • 5
  • 17

1 Answers1

0

This should work

exampledata = np.arange(42).reshape((7,6))
# Sum data along axis 0:
sum = exampledata[0:len(exampledata)].sum(0)
print(sum)

Initial 2D array:

[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]
 [24 25 26 27 28 29]
 [30 31 32 33 34 35]
 [36 37 38 39 40 41]]

Output:

[126 133 140 147 154 161]