Page 1 of 1

Despeckle

Posted: 2014-10-09T23:43:33-07:00
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

Re: Despeckle

Posted: 2014-10-10T03:03:08-07:00
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.

Re: Despeckle

Posted: 2014-10-10T11:23:26-07:00
by magick
See -kuwahara, its a new option available in ImageMagick 6.8.9-9 Beta.

Re: Despeckle

Posted: 2014-10-10T20:36:05-07:00
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

Re: Despeckle

Posted: 2014-12-27T14:09:19-07:00
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

Re: Despeckle

Posted: 2014-12-27T15:12:22-07:00
by fmw42

Re: Despeckle

Posted: 2014-12-27T17:16:06-07:00
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.

Re: Despeckle

Posted: 2014-12-27T19:26:59-07:00
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.

Re: Despeckle

Posted: 2014-12-28T11:38:30-07:00
by snibgo
Ah, yes, I overlooked the area-threshold. That's a much better way of doing it than my thoughts above.