Image Noise Detection in MATLAB
If you would like to do this faster, what you could try is the neighborhood operations. Take for example the sample code below:


I = imread('tire.tif');
f = @(x) max(x(:));
I2 = nlfilter(I,[3 3],f);
imshow(I);
figure, imshow(I2);


This applies a maximum filtering. You could consider changing your neighborhood function f to your custom algorithm (inside your loop) and this should give you a better speed.

Of course there are a lot of ways of getting speed. Use of integral images, moving averages and precomputations might save you from the burden of heavy block processing. But this would be way a lot to write in here. For more details check out these papers:

http://www.csse.uwa.edu.au/~shafait/papers/Shafait-efficient-binarization-SPIE08.pdf
http://arxiv.org/ftp/arxiv/papers/1201/1201.5227.pdf

Cheers,