How to remove scattered spots in an image?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
calvi
Posts: 5
Joined: 2011-04-07T19:39:38-07:00
Authentication code: 8675308

How to remove scattered spots in an image?

Post by calvi »

Hi,

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

Image

Thanks.
Last edited by calvi on 2011-04-07T23:52:10-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to remove scattered spots in an image?

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
calvi
Posts: 5
Joined: 2011-04-07T19:39:38-07:00
Authentication code: 8675308

Re: How to remove scattered spots in an image?

Post 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. :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove scattered spots in an image?

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to remove scattered spots in an image?

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
calvi
Posts: 5
Joined: 2011-04-07T19:39:38-07:00
Authentication code: 8675308

Re: How to remove scattered spots in an image?

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to remove scattered spots in an image?

Post by anthony »

The TIF to PNG problems is likely to be something to do with TIF and its color handing
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply