matlab fcn to preprocess and stack images