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

故事扮演 提交于 2019-12-13 05:00:43

问题


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:


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



来源:https://stackoverflow.com/questions/29188423/polarizing-an-image-how-to-find-the-average-of-each-rgb-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!