0
votes

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?

1
You won't have to add to the total count as sum(red, green, blue) is the total number of pixels.user1016274
Is sum(red,green,blue) the total number of pixels or the total number of values? (Or are they the same thing?)abshi
"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?" That seems right given the sample data. Do you have any source?KevinOrr

1 Answers

0
votes

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 :)