This is part of graph cut algorithm, in which energy is modeled using class labeling and pairwise coherence. I used Gaussian mixture model and max-flow graph cut, this is an iterative process and usually takes about a few second on an image of 500x500 pixels. I would like this to be very fast. Any help will be appreciated.