Page 1 of 1
How to remove black borders from scanned images?
Posted: 2013-05-23T13:25:55-07:00
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
Re: How to remove black borders from scanned images?
Posted: 2013-05-23T13:52:10-07:00
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.
Re: How to remove black borders from scanned images?
Posted: 2013-05-23T14:06:03-07:00
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?
Re: How to remove black borders from scanned images?
Posted: 2013-05-23T14:20:15-07:00
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.
Re: How to remove black borders from scanned images?
Posted: 2013-05-23T14:43:11-07:00
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
Re: How to remove black borders from scanned images?
Posted: 2013-05-23T15:34:11-07:00
by fmw42
I think we will need a (sanitized) example to see what the issue might be and how best to recommend you proceed.
Re: How to remove black borders from scanned images?
Posted: 2013-05-23T22:52:40-07:00
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.
Re: How to remove black borders from scanned images?
Posted: 2013-05-24T07:43:06-07:00
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?
Re: How to remove black borders from scanned images?
Posted: 2013-05-24T08:11:31-07:00
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.
Re: How to remove black borders from scanned images?
Posted: 2013-05-24T08:13:52-07:00
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.
Re: How to remove black borders from scanned images?
Posted: 2013-05-24T08:35:11-07:00
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
would produce, where
is a black and white image.
Re: How to remove black borders from scanned images?
Posted: 2013-05-24T09:18:15-07:00
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.
Re: How to remove black borders from scanned images?
Posted: 2013-05-24T11:06:24-07:00
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
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.
Re: How to remove black borders from scanned images?
Posted: 2013-05-24T12:19:42-07:00
by izavorin
GreenKoopa wrote:
Here is an example:
Thanks, I'll experiment with this!
Re: How to remove black borders from scanned images?
Posted: 2013-06-02T19:22:32-07:00
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