abshi
abshi

Reputation: 73

Polarizing an image--how to find the average of each RGB value

For an exercise in image processing, I have to write a program that applies various effects to images. One of the effects was grayscale, which was done find the average of RGB values ((red+green+blue)/3). To polarize an image, however, I need to first find the averages of each individual component (i.e. all red values/number of red pixels). Would it be appropriate to then loop through the rows and columns (with a counter for pixels, red values, green values, and blue values) as a way to find the average? Is there a more efficient way?

Also, pixels are polarized based on the average pixel value. "If average R is 100, average G is 200, and average B is 300 and a pixel has R 150, G 150, and B, 100, the polarized pixel would be 255, 0, and 0." I don't understand the relationship? Is it if the current value is less than the average, then it would be polarized to 0/more than the average, polarized to 255?

Upvotes: 0

Views: 3057

Answers (1)

Shaikh Sonny Aman
Shaikh Sonny Aman

Reputation: 177

Calculate average RGB value of an image:

    import numpy as np

    # load your image in a variable named `im`, then calculate average RGB value
    np.array(im).mean(axis=(0,1))

    # It gives a tuple like (71.710743801652896, 103.11570247933884, 64.165289256198349)

You don't need to iterate, numpy has it all :)

Upvotes: 0

Related Questions