Remove spots in a black/white text 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
leyou
Posts: 3
Joined: 2017-12-26T09:01:03-07:00
Authentication code: 1152

Remove spots in a black/white text image

Post by leyou » 2017-12-26T09:07:59-07:00

Hi.

I have seen similar threads but I didn't manage to get what I wanted out of them.

Here is an example: https://i.imgur.com/REXnLuQ.png

I'd like to remove the spots such that only the text remains, and be able to choose a number of pixels threshold. Here the biggest spot has around 30 pixels but other images may have larger spots that I want to remove so I want to be able to change this value.

I tried this command: convert input.png -morphology Close Disk ouput.png

But besides the spots it also remove parts of the letters.

This command seems to do the job for small spots with less than 5 pixels, but I don't know how to modify it for larger spots: convert input.png -verbose -negate -morphology Thinning "4x4:0,0,0,0, 0,1,1,0, 0,1,1,0, 0,0,0,0" -morphology Thinning:5 LineEnds -morphology Thinning "3:0,0,0, 0,1,0, 0,0,0" -negate output.png

Also tried this method (viewtopic.php?p=132613#p132613) but the result is a mess.

Thank you in advance.

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

Re: Remove spots in a black/white text image

Post by snibgo » 2017-12-26T10:25:27-07:00

The obvious method for your problem uses "-connected-components". What version IM do you use? On what platform?
snibgo's IM pages: im.snibgo.com

leyou
Posts: 3
Joined: 2017-12-26T09:01:03-07:00
Authentication code: 1152

Re: Remove spots in a black/white text image

Post by leyou » 2017-12-26T15:10:01-07:00

Version: ImageMagick 6.9.9-11 Q16 x86_64 2017-09-30 http://www.imagemagick.org
on cygwin / windows 10.

How does -connect-components works?

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

Re: Remove spots in a black/white text image

Post by snibgo » 2017-12-26T15:21:14-07:00

snibgo's IM pages: im.snibgo.com

leyou
Posts: 3
Joined: 2017-12-26T09:01:03-07:00
Authentication code: 1152

Re: Remove spots in a black/white text image

Post by leyou » 2017-12-27T03:23:47-07:00

I tried it but I'm not getting what I expected... https://i.imgur.com/388RZfo.png

I ran convert input.png -define connected-components:area-threshold=40 -connected-components 4 -auto-level output.png

There is no documentation on the area-threshold parameter. I assumed it was the number of pixels in an object? And I don't understand why it turns the image from binary into black and white gradient.

It's most probably a very simple issue but I'm at loss..

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

Re: Remove spots in a black/white text image

Post by snibgo » 2017-12-27T05:34:05-07:00

By default, the components will be coloured with gray(0), gray(1) etc (where the maximum numer depends on your Q-number). But we don't want shades of gray, we want black or white, so we tell it to set each component colour to the mean of all the pixels that formed it. The background will be made of mostly black pixels but also a few white, so the mean will be not quite black, so we threshold at 50%.

Area-threshold removes small components, those with fewer than the given number of pixels. In this case, we want to remove the small white components, by making them black.

Windows BAT syntax:

Code: Select all

convert ^
  REXnLuQ.png ^
  -define connected-components:verbose=true ^
  -define connected-components:area-threshold=100 ^
  -define connected-components:mean-color=true ^
  -connected-components 4 ^
  -threshold 50%% ^
  out.png
snibgo's IM pages: im.snibgo.com

Post Reply