How about scanning with a 3x3 window and computing the standard deviation - that will be 0 if all the pixels are the same and high if they're different. Then subtract that from 255
out = 255 - stdfilt(grayImage);
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!