Despeckle

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
PSi
Posts: 2
Joined: 2014-10-09T22:55:03-07:00
Authentication code: 6789

Despeckle

Post by PSi »

Hi,

I have old schematics scanned as bitmaps and they have a lot of "pepper" due to copier/scanner dithering of partly smudged originals. What I would like to try, is to find and replace limited size contiguous areas, and replace them with surrounding colour. If the "pepper particle" is attached to a larger area I dont mind it, but all loose separated pepper particles I do wish to get rid of. The Median tool will attack the edges of larger areas (text and "wiring") of the bitmap. I just wish to get rid of the "loose pepper". Any suggestions (bearing in mind I'm just a novice!)?

pekka
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Despeckle

Post by snibgo »

[Mod note: It's usually better to start a new topic than tack on to an old one. I've moved this to a new topic.]

There are many techniques for removing noise. Perhaps your pepper can be made transparent, then surrounding pixels blurred into those areas.

The best advice will come if you post a sample. You can upload to dropbox.com or similar site and paste the URL here.
snibgo's IM pages: im.snibgo.com
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Despeckle

Post by magick »

See -kuwahara, its a new option available in ImageMagick 6.8.9-9 Beta.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Despeckle

Post by fmw42 »

It would be best to see some examples as has been suggested above.

One possibility would be to use -morphology open or close. see http://www.imagemagick.org/Usage/morphology/#open
PSi
Posts: 2
Joined: 2014-10-09T22:55:03-07:00
Authentication code: 6789

Re: Despeckle

Post by PSi »

I'm looking for a way to search for areas of x number of pixels that are black and are complelety surrounded with white pixels. If I find less than x number of black pixels surrounded with white pixels all round I wish to erease it.

I wish to set the maximum number of black pixels touching each other and still be surrounded all around with white pixels to be erased. The scanned schematics may have one pixel wide lines possibly staggered, but still touching at the corners. Those I do not wish to dissappear. None of the filters I've seen seem to handle this. They all tend to round black areas and erase thin lines. I did a test with Perl that tried to find white, one pixel wide, 7 x7 pixel squares from the image and in case such a white outlined square could be found the inside of the square was made white.

I should have first tested with 3 x 3 and move to 4 x 4 and up to 7 x 7. This is slow and inefficient, and only works if the area I'm testing is square. I wished to test any shape of black areas with certain max number of black pixels touching each other and surrounded with white pixels. My old Windows computer had Active perl 5.16.3 and ImageMagick-6.7.7-Q8. After computer crash I've been unable to test/edit my script as I've not found a working combination of available ActivePerl/StrawberryPerl and Image::Magick (PerlMagick) using latest available ImageMagick-6.9.0-Q16 that would get installed:

https://rt.cpan.org/Public/Dist/Display ... PerlMagick

pekka
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Despeckle

Post by fmw42 »

snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Despeckle

Post by snibgo »

Yes, "-connected-components" seems the best solution.

You would generate the verbose output and test each line for the area being greater than your threshold. Sadly, this doesn't give text output of a pixel coordinate within the shape, from which you could floodfill. (Eg if you have a ring of pixels, the centroid will be in the hole.) Instead, you could use the id number which is a gray level, then "-fill Red -opaque X" where X is the colour of that gray level.

Clearly, this needs a script. You could do one "convert" for each qualifying shape. But if the file is large and there are many qualifying shapes, it would be quicker to build one long "convert" command with multiple copies of "-fill White -opaque X".

This would change a prepared image from black and white, to black and white and red. Then use the red pixels as a mask to change pixels in your original image.

The job would be simpler if the "-connected-components" verbose output gave the coordinate of a pixel guaranteed to be within a shape. Then you could use that coordinate to flood-fill your original image.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Despeckle

Post by fmw42 »

snibgo wrote:The job would be simpler if the "-connected-components" verbose output gave the coordinate of a pixel guaranteed to be within a shape. Then you could use that coordinate to flood-fill your original image.
Generally, you should not have to use -fill. The area-merge feature should be all you need to remove small objects. Just specify it as per the define mentioned at the bottom of the page. That will merge all smaller size regions into their respective neighboring/surrounding largest region.

If, for a grayscale image, you need/want to do a -fill ... -opaque ..., you can use the graylevel corresponding to the id value. But since in Q16, -opaque does not use 16-bit grayvalues, you need to convert the id to a percent as per gray(xx%) rather than gray(xxx).

If you still feel that having a coordinate is useful, perhaps for color images, then describe your rationale further and or an example. It should be possible to add that or other features that seem useful. Perhaps a new define or argument would allow one to recolor by id from a list of id's and colors.

I have used the current connected-components features for some time to clean up binary (thresholded) images and have been successful in scripting the above concepts. However, the current -connected-components was just the basic concept with what we thought were useful features. But we are open to any good ideas for enhancements.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Despeckle

Post by snibgo »

Ah, yes, I overlooked the area-threshold. That's a much better way of doing it than my thoughts above.
snibgo's IM pages: im.snibgo.com
Post Reply