Sum of color image values

I am looking for a way to sum color values ​​for all pixels in an image. I demand that this evaluate the full flux of a bright source (say, a distant galaxy) from its image of surface brightness. Someone please help me how can I sum the color values ​​for all the pixels in the image.

For example: Each pixel of the next image has a color value from 0 to 1. But when I read the image with imread the color values ​​of each pixel, I get an array of three elements. I am very new to matplotlib and I don't know how to convert this array to single values ​​on a scale from 0 to 1 and add them.

enter image description here

+4
source share
1 answer

PIL, ( "" ) :

from PIL import Image
col = Image.open('sample.jpg')
gry = col.convert('L') # returns grayscale version.

, , , numpy:

arr = np.asarray(col)
tot = arr.sum(-1)  # sum over color (last) axis
mn  = arr.mean(-1) # or a mean, to keep the same normalization (0-1)

-:

wts = [.25, .25, .5]    # in order: R, G, B
tot = (arr*wts).sum(-1) # now blue has twice the weight of red and green

, , , :

tot = np.einsum('ijk, k -> ij', arr, wts)

, () . , :

tot = arr.sum(0).sum(0) # first sums all the rows, second sums all the columns

, tot - . , . , sum mean:

mn = arr.mean(0).mean(0)
+6

Source: https://habr.com/ru/post/1535254/


All Articles