Trimming background and isolated pixels

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
Lodu
Posts: 2
Joined: 2019-10-08T07:22:57-07:00
Authentication code: 1152

Trimming background and isolated pixels

Post by Lodu » 2019-10-08T07:52:25-07:00

Hi!

I'm using IM 7 on a Mac OS 10.12. I'm trying to trim several black and white scanned pages of a book so that I can get rid off the black background. Here are a couple of these images :
- https://drive.google.com/file/d/1k5bQqs ... sp=sharing
- https://drive.google.com/file/d/1J7yYqP ... sp=sharing

I thought I could easily do that with something like that:

Code: Select all

convert image.tif -trim image_cropped.tif
Unfortunately there are a very few isolated white pixels (groups of 1or 2 generally) in the background that are preventing a complete trimming of the pictures... I'm trying to remove these. I tried to adapt some code I found online using connected-components:

Code: Select all

mogrify -define connected-components:area-threshold=25 \
-define connected-components:mean-color=true \
-connected-components 4 -threshold 50%%\
*.tiff
It works fine on text pages but it produces very large files (60 Mo vs 3.9 Mo for the normal files) and it does not work on the plates of the book I scanned because objects are drawn with lots of small dots...

Are there any other solutions?

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

Re: Trimming background and isolated pixels

Post by snibgo » 2019-10-08T08:46:21-07:00

There are many way to denoise an image. When the noise is small dark marks on a light background, we can blur then threshold. This finds the trimmed parameters from a denoised image, and uses those parameters on the input image.

Code: Select all

magick \
  VII-3-Prussing-1982-072.tif \
  \( +clone -blur 0x2 -threshold 50% -set option:MYCROP %@ +delete \) \
  -crop %[MYCROP] \
  out.png
snibgo's IM pages: im.snibgo.com

Lodu
Posts: 2
Joined: 2019-10-08T07:22:57-07:00
Authentication code: 1152

Re: Trimming background and isolated pixels

Post by Lodu » 2019-10-09T04:04:05-07:00

Thanks a lot for your answer!

Unfortunately, as these are monochromatic images with only black and white pixels, the blur did not have much effect on the white pixels on the black borders I want to remove... I would like to remove these so that I could just use make an automatic crop with -trim.

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

Re: Trimming background and isolated pixels

Post by snibgo » 2019-10-09T04:21:58-07:00

Did you try the command I showed? It works for me. Resized down for the web:
Image
snibgo's IM pages: im.snibgo.com

Post Reply