Found [This cute algorithm](http://angeljohnsy.blogspot.com/2011/04/matlab-code-histogram-equalization.html) to solve this.