MATLAB: How to get rid of some pixels around each dot in dot pattern

Image Processing Toolboxrandom dot pattern

Hi all,
I have a 1024X1280 uint8 image. After I am plotting random dot patterns on the image I use:
dots=(find(IMG));
IMG(dots) = IMG(dots)./range(IMG(dots(:)));
IMG(dots)=IMG(dots)-min(IMG(dots));
IMG(dots)=IMG(dots)-mean(IMG(dots));
IMG(dots)=IMG(dots)/max(IMG(dots));
IMG(dots)=IMG(dots)*125+127;
image = uint8(round(IMG));
figure(2);imshow(image)
to set the mean luminance of the dots to grey. When I show the image on a black background I do not see pixels that are odd. But when I plot it on the grey background there are some odd pixels, which are surrounding each dot. I would like to get rid of them.
Could you please help me with this?
Many thanks.

Best Answer

Your line
IMG(dots) = IMG(dots)./range(IMG(dots(:)));
is going to fail. You need to covert IMG to double before you can do that. You would need to start the code with
IMG = double(croppedImage2);
Related Question