you don't show us all your data types. what types are hmax,hmin,hist[]? it looks like all variables should be floats except the images themselves.

is the loop computing cfg[i] supposed to go...