Hi,
I haven't been able to look at your code, but are you sure it hasn't got anything to do with overflow? (values > 255)?
Easiest way (if there is no time constraint), is to use the ImageToArray function. Convert then to I32, and you are able to add as many pictures as you want.
Afterwards divide and use the ArrayToImage function.