How to retrieve a total pixel value above an average-based threshold in Python
Question: Currently, I am practicing with retrieving the total of the pixel values above
a threshold based on the mean of the whole image. (I am very new to Python). I
am using Python 3.5.2, and the above code was copied from the Atom program I
am using to write and experiment with the code.

For the time being, I am just practicing with the red channel - but
eventually, I will need to individually analyse all colour channels.

The complete code that I am using so far:

    
    
    import os  
    from skimage import io  
    from tkinter import *  
    from tkinter.filedialog import askopenfilename  
    def callback():  
        M = askopenfilename()       #to select a file  
        image = io.imread(M)        #to read the selected file  
        red = image[:,:,0]          #selecting the red channel  
        red_av = red.mean()         #average pixel value of the red channel  
        threshold = red_av + 100    #setting the threshold value  
        red_val = red > threshold  
        red_sum = sum(red_val)  
        print(red_sum)  
    Button(text = 'Select Image', command = callback).pack(fill = X)  
    mainloop()
    

Now, everything works so far, except when I run the program, red_sum comes out
to be the number of pixels above the threshold, not the total of the pixels.

What I am I missing? I am thinking that my (possible naive) way of declaring
the `red_val` variable has something to do with it.

But, how do I retrieve the total pixel value above the threshold?


Answer: When you did (red > threshold) you got a mask such that all the pixels in red
that are above the thrshold got the value 1 and 0 other wise. Now to get the
values you can just multiply the mask with the red channel. The multiplcation
will zero all the values that are less than the threshold and will leave the
values over the threshold unchanged.

The code:

    
    
    red_val = (red > threshold)*red
    red_sum = sum(red_val)
    

