Added

I created a histogram function to learn how it works. 
What is 512? Because I know exactly what do you want to do I know it the number of rows and the number of columns of the image. But you have to use explicit names for them.
`q[level]=math.floor((255*sumt)/(rows*cols)+0.00001)`