Programmatically distinguish background gray

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
muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Programmatically distinguish background gray

Post by muccigrosso »

I'm using IM to clean up some scanned grayscale pages from a text. Right now I clean up the background when there's some poor scanning artifacts like shadows near a book spine by doing a contrast stretch of 5%,90%. This works well _except_ when the page doesn't really need the cleanup. Then the stretch darkens pixels around the letters, widening them.

If I look at the histograms, I can see that the "dirty" images have a noticeable shoulder near white (as you'd expect), while the "clean" images don't. So what would be the best way to use this to turn on or off the contrast stretch? Do I need to process the histogram data or is there some built-in function that will work?

TIA.

EDIT: ImageMagick 7.0.7-12 Q16 x86_64 2017-11-30 on MacOS 10.11.6
Last edited by muccigrosso on 2017-12-01T11:43:40-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Programmatically distinguish background gray

Post by fmw42 »

Please always provide your IM version and platform, since syntax may differ. Also post an example image if you can to some free hosting service and put the URL here.

If you are on Unix, then you could try my textcleaner script at my link below. Otherwise, try -lat.
muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Re: Programmatically distinguish background gray

Post by muccigrosso »

I'm not a fan of the black/white result of -lat. I was hoping for a more algorithmic way to decide whether or not to do anything.

I'll save the next images this happens with.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Programmatically distinguish background gray

Post by fmw42 »

Try my textcleaner. It can keep your text smooth and the text colors.
muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Re: Programmatically distinguish background gray

Post by muccigrosso »

Yep, I know that script. :-)

Part of this is me trying to work with IM.
Post Reply