Page 1 of 1

How to remove scattered spots in an image?

Posted: 2011-04-07T19:58:01-07:00
by calvi
Hi,

Anyone know how to remove the scattered small spots in an image? Like below:

Image

Thanks.

Re: How to remove scattered spots in an image?

Posted: 2011-04-07T20:08:52-07:00
by anthony
Try using a -morphology Open function with a 'kernel' that is just a little bigger that the spots.
http://www.imagemagick.org/Usage/morphology/#open

Code: Select all

   convert image.png -morphology Open Octagon result.png
Normally 'Close' is used to remove spots.
http://www.imagemagick.org/Usage/morphology/#close
Nut your image is inverted to the normal usage of greyscale shapes (white on black) so you need to use the negative equivalent, 'Open'

Alternative use 'Smooth' which cleans up both positive and negative images.

PS: yes you image is greyscale and not binary, that is not a problem.

Someone may suggest using 'floodfill'.. DON'T That will remove the grayscale anti-aliasing edge pixels.

Re: How to remove scattered spots in an image?

Posted: 2011-04-07T20:42:01-07:00
by calvi
Hi Anthony,

Thanks for your help.

I guess this method may affect the sharpness of the edge.

Do you think it is possible to trace the contour and fill the inside?

Thank. :)

Re: How to remove scattered spots in an image?

Posted: 2011-04-07T21:16:31-07:00
by fmw42
morphology open/close should not change smoothness, I don't believe, and is likely your best bet. You can also iterate with a small kernel shape. But your image(s) have white spots on the edge. So trying to get the boundary may have just as many problems. But Anthony has some examples, if I recall. See http://www.imagemagick.org/Usage/transform/#edge_vector

try this:

convert image.enhance.png -morphology open diamond:2 image.enhance_open2.png

seems to fill in all your holes.

Re: How to remove scattered spots in an image?

Posted: 2011-04-07T22:27:16-07:00
by anthony
Fred is right.

The operators while originally designed for binary images, work just fine for grayscale images. The greyscale edge will not be lost or destroyed, though some grey edge pixels may move, be removed, or duplicated, according to the kernel fill shape.

Seems to work just fine at preserving the greyscale edge.

Note when I tried the octagon on one of the above shapes some of the edges were 'connected' to the image edge more solidly. Adding a -virtual-pixel white will ensure that IM things the image proper is surrounded by white, and so help prevent that problem. Better still add some white border first.

It will also smooth the 'dents' and small gaps in the edge, and the sharpness of the inside corner as well.

Re: How to remove scattered spots in an image?

Posted: 2011-04-07T23:46:16-07:00
by calvi
Hi Anthony, Fred,

Thanks for your quick and great help.

I have tried "morphology" and found it is perfect and introduces no edge blur, like below:

Image

The command used is:

Code: Select all

convert noise.tif -morphology Open Disk enhanced.tif
The noisy image is .tif. If it is .png, then the result image will be light grey, not black. I have no idea why. Maybe it is not suitable for .png files.

Sorry that doubt without trying.

Thanks.

Re: How to remove scattered spots in an image?

Posted: 2011-04-08T00:23:14-07:00
by anthony
The TIF to PNG problems is likely to be something to do with TIF and its color handing