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

Programmatically distinguish background gray

Post by muccigrosso » 2017-12-01T09:17:56-07:00

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: 22427
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Programmatically distinguish background gray

Post by fmw42 » 2017-12-01T10:52:09-07:00

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

Re: Programmatically distinguish background gray

Post by muccigrosso » 2017-12-01T12:10:26-07:00

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: 22427
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Programmatically distinguish background gray

Post by fmw42 » 2017-12-01T13:04:31-07:00

Try my textcleaner. It can keep your text smooth and the text colors.

muccigrosso
Posts: 6
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Re: Programmatically distinguish background gray

Post by muccigrosso » 2017-12-01T15:09:01-07:00

Yep, I know that script. :-)

Part of this is me trying to work with IM.

Post Reply