How to remove black borders from scanned images?

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
izavorin
Posts: 9
Joined: 2011-03-22T12:01:31-07:00
Authentication code: 8675308

How to remove black borders from scanned images?

Post by izavorin »

I have a bunch of black and white TIFFs which are scans of old documents. Some of them have black borders of various sizes and locations: some around all 4 sides, some only on 1 or 2 sides, some borders thick, some thin. Is it possible to use ImageMagick to remove those?

I tried "-trim" on a couple of them but that produced no effect. Maybe was not using it right. Don't think "-fuzz" will help since images are black and white. What else can I do?

Thx
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to remove black borders from scanned images?

Post by GreenKoopa »

Assuming the level of noise isn't too high, IM could do this without too much effort. Grayscale or true 1-bit B&W? If you can post a sample document, even just a mostly empty page to give us an idea of your scanner, someone would probably write the initial command for you.
izavorin
Posts: 9
Joined: 2011-03-22T12:01:31-07:00
Authentication code: 8675308

Re: How to remove black borders from scanned images?

Post by izavorin »

The content of the images is a bit sensitive, so can I just post samples with the original borders but with the text area whited out?
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to remove black borders from scanned images?

Post by GreenKoopa »

Sure, as long as the nature of the images doesn't change. Things such as 1-bit vs grayscale vs color, resolution (DPI), file format, etc. Are your scans all similar in these regards? Interesting for a crop is both sides of the border, so leave the page margins as much as you can. Do these need to be rotated to straighten? That is a much more difficult task to automate.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to remove black borders from scanned images?

Post by GreenKoopa »

-trim is the right track, and you are right that -fuzz isn't enough to overcome the noise of many scans. There are other ideas already documented here:
http://www.imagemagick.org/Usage/crop/#trim_blur
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove black borders from scanned images?

Post by fmw42 »

I think we will need a (sanitized) example to see what the issue might be and how best to recommend you proceed.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove black borders from scanned images?

Post by snibgo »

It's often useful to add a black border before doing the trim. It may be necessary to do some heavy processing to find the black edge. In that case, a trick is to create a mask, so the heavy processing has no impact on the useful areas of the scan.

There have been similar examples on this forum.
snibgo's IM pages: im.snibgo.com
izavorin
Posts: 9
Joined: 2011-03-22T12:01:31-07:00
Authentication code: 8675308

Re: How to remove black borders from scanned images?

Post by izavorin »

I am having trouble uploading the samples to my online repository so that I can link to them in the post. But in the meantime... Is there a way in IM to compute (black) connected components for a bitonal image?
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to remove black borders from scanned images?

Post by GreenKoopa »

Sorry, I'm not sure what you mean by "compute (black) connected components". The only things that come to my mind are a flood fill tool, a fuzzy select magic wand tool, or morphology, and then I'm not sure what you are doing with them.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove black borders from scanned images?

Post by snibgo »

I'm also not sure what you mean. Perhaps, "Given an image that contains only black and white pixels, how do I count the number of components, where a component is a number of connected black pixels?"

The answer is a rather horrible and slow script.

The process is:

1. Find a black pixel.
2. Flood-fill from that point, turning black into white.
3. Increment a counter.
4. Repeat from 1 until there are no more black pixels.
snibgo's IM pages: im.snibgo.com
izavorin
Posts: 9
Joined: 2011-03-22T12:01:31-07:00
Authentication code: 8675308

Re: How to remove black borders from scanned images?

Post by izavorin »

GreenKoopa wrote:Sorry, I'm not sure what you mean by "compute (black) connected components". The only things that come to my mind are a flood fill tool, a fuzzy select magic wand tool, or morphology, and then I'm not sure what you are doing with them.
Given a black and white image, produce a mask "image" of the same size (or something equivalent) that would have labeled pixels according to whether they belong to a specific black connected component based on 4- or 8-pixel neighborhoods. If you are familiar with the Image Processing toolbox of Matlab, it would be what

Code: Select all

L = bwlabel(~im);
would produce, where

Code: Select all

im
is a black and white image.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to remove black borders from scanned images?

Post by GreenKoopa »

Producing a mask image is a common technique. The only IM commands I know that work with connected components are -floodfill and -draw floodfill. These can be used to create a mask in a round about way, or used more directly if that makes sense. I don't know if they use 4- or 8-pixel neighborhoods, but that would be easy to test. I can't recall seeing an example, but I'll look.

Note that internally IM doens't work in B&W or even grayscale. All images are full RGB (at least until version 7). You can achieve great results all the same, but learning that helped me think about commands more clearly.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to remove black borders from scanned images?

Post by GreenKoopa »

izavorin wrote:Given a black and white image, produce a mask "image" of the same size that would have labeled pixels according to whether they belong to a specific black connected component based on 4- or 8-pixel neighborhoods.
Here is an example:

Code: Select all

: Create test image
convert -size 100x60 xc:white -fill black -draw "rectangle 30,10 70,30 rectangle 10,30 30,50 rectangle 71,31 90,50 rectangle 40,40, 60,50" image.png

: Optioin 1, using -floodfill
convert image.png -fill green -floodfill +50+20 black filled_1.png
convert filled_1.png -fill black +opaque green -fill white -opaque green mask_1.png

: Option 2, using -draw
convert image.png -fill green -draw "color 50,20 floodfill" filled_2.png
convert filled_2.png -fill black +opaque green -fill white -draw "color 50,20 replace" mask_2.png
Image
IM appears to use 4-pixel neighborhoods for floodfill.

--- EDIT ---
Changed -opaque white to +opaque green to make the example work beyond the 1-bit B&W case.

An alternative option to creating a mask is to introduce an alpha channel. For finding connected areas you are stuck with the first step of floodfill, but there are many options for the next step.
Last edited by GreenKoopa on 2013-05-24T14:06:03-07:00, edited 1 time in total.
izavorin
Posts: 9
Joined: 2011-03-22T12:01:31-07:00
Authentication code: 8675308

Re: How to remove black borders from scanned images?

Post by izavorin »

GreenKoopa wrote: Here is an example:
Thanks, I'll experiment with this!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to remove black borders from scanned images?

Post by anthony »

You can also do multi seed point flood fills with user defined neighbourhoods using Conditional Dilation
Conditional Dilation is actually what 'flood fill' is doing, just using a fast more specific algorithm.

See Conditional Dilation
http://www.imagemagick.org/Usage/morpho ... onditional
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply