Page 1 of 1

Remove Black Borders and Deskew

Posted: 2012-11-07T07:21:46-07:00
by PhilipeGatis
Hi,
I am trying manipulate a lot of scanned images. I want remove black borders and deskew, but exists a lot of different kinds of images layout and sometimes the result it's not the expected.
I use this code in all images.
$convert image -compress LZW -density 300 -set option:deskew:auto-crop 90 -deskew 40 -bordercolor black -border 1-fuzz 95% -trim +repage -fill white -draw "color 0,0 floodfill" -alpha off -shave 1x1 result

Example1:
Origin:
https://dl.dropbox.com/u/101393046/a.tif
Result:
https://dl.dropbox.com/u/101393046/a1.tiff

Example2:
Origin:
https://dl.dropbox.com/u/101393046/b.tif
Result:
https://dl.dropbox.com/u/101393046/b1.tiff

How can see the example 2, stayed a black area.
If i remove code -trim and +repage, the example1 image, stayed unreadable and example 2, works.

I'd like a way to remove the black edges of the 2 imagems without any loss of content.

Thanks.

Re: Remove Black Borders and Deskew

Posted: 2012-11-07T08:39:45-07:00
by Bonzo
I have see posts where a border is added and then a floodfill is used.

Add a black border 2px wide then use -floodfill white using the pixel at the corner.

Untested code:

Code: Select all

convert input -floodfill +1+1 white output
The problem you will have then is if your original image is an off white you will have some pure white areas!

Re: Remove Black Borders and Deskew

Posted: 2012-11-07T10:09:54-07:00
by fmw42
As Bonzo said, you are better to pad a few pixels around the image with black, then do a fuzzly floodfill and then deskew, then trim and then if needed add a white border back again.

I think you have all the pieces in your command (except the initial -border white -border 2), it just needs to be rearranged.

If you are on Linux, Mac OSX or Windows w/Cygwin, you might want to look at my script, textcleaner, that will also clean up some of the noise and even out the background color, if there is any variations. see the link below.

Re: Remove Black Borders and Deskew

Posted: 2012-11-08T11:53:09-07:00
by PhilipeGatis
Thanks for help.
fmw42 and Bonzo

I tried many times with other codes, reorganizing the of functions orders
But do not works.

In example 1, part of the text image, take in the black border and is erased together.
See example below:
https://dl.dropbox.com/u/101393046/blac ... hether.tif

The best code I made, but stay in the same problem.

Code: Select all

convert input -respect-parenthesis ( -compress LZW -density 300 -bordercolor black -border 1 -fuzz 95% -trim +repage -fill white -draw "color 0,0 floodfill" -alpha off -shave 1x1 ) ( -bordercolor black -border 2 -fill white -draw "color 0,0 floodfill" -alpha off -shave 0x1 -fuzz 95% -deskew 40 +repage ) result 
If i can disconect text of border i think the code works.

If have a kind of function who can disconnect text of edge i think works and will remove black edges without erase the text.

Anyone know any way?

Re: Remove Black Borders and Deskew

Posted: 2012-11-08T12:22:29-07:00
by fmw42
-fuzz 95% is probably way too large, try -fuzz 1%

But the main issue is that the signature at the bottom right extends to the bottom edge. That will mean that floodfill will creep into the image and change all black that is connected to that part of the signature to white.

I am not sure how to deal with this automatically, since your image is rotated and has a black background. If you scan against some other color. then the process might work.

Alternately, you have to manually add some white right at that point where the signature hits the edge of the page.

This was my simple test, which shows it fails after the floodfill


convert a.tif -bordercolor black -border 5 +write 1tmp1.png \
-fuzz 0% -fill white -draw "color 0,0 floodfill" +write 1tmp2.png \
-trim +repage +write 1tmp3.png -deskew 40% +write 1tmp4.png \
a_proc.tif

If I manually erase just a bit of the black signature at the bottom so that it is white at the edge, then this seems to work

convert a_new.tif -bordercolor black -border 5 +write 1tmp1.png \
-fuzz 0% -fill white -draw "color 0,0 floodfill" +write 1tmp2.png \
-trim +repage +write 1tmp3.png -deskew 40% +write 1tmp4.png \
a_new_proc.tif

PS. The above is Unix syntax. For Window, you need escape % to %% and replace the end of line \ with ^. See http://www.imagemagick.org/Usage/windows/

Re: Remove Black Borders and Deskew

Posted: 2012-11-08T12:44:52-07:00
by PhilipeGatis
If i remove -fuzzy from the code the result is:
https://dl.dropbox.com/u/101393046/a%20 ... 0fuzzy.tif

Code: Select all

convert -respect-parenthesis ( -compress LZW -density 300 -bordercolor black -border 1 -trim +repage -fill white -draw "color 0,0 floodfill" -alpha off -shave 1x1 ) ( -bordercolor black -border 2 -fill white -draw "color 0,0 floodfill" -alpha off -shave 0x1 -deskew 40 +repage ) *.tif
Other ideia?

Re: Remove Black Borders and Deskew

Posted: 2012-11-08T12:48:41-07:00
by fmw42
see my post just before yours. we were posting at the same time

Re: Remove Black Borders and Deskew

Posted: 2012-11-08T12:56:16-07:00
by fmw42
If you do not mind a little black around parts of the image, you could try my script, textcleaner, at the link below, if you are on Linux, Mac OSX or Windows w/Cygwin.

This seems to work reasonably for me without having to manually erase a small part of the signature at the bottom edge.

textcleaner -g -f 25 -o 10 -u a.tif a_proc_tc.tif

Re: Remove Black Borders and Deskew

Posted: 2012-11-08T14:16:59-07:00
by PhilipeGatis
I use windows. :(

If I use the first code, works in example 1 image, the signature don't disappears, but not works in example 2, and the black area not disappears.

Code: Select all

convert b.tif -respect-parenthessis ( -compress LZW -density 300 -set option:deskew:auto-crop 90 -deskew 40 -bord
ercolor black -border 1 -fuzz 95% -trim +repage -fill white -draw "color 0,0 flo
odfill" -alpha off -shave 2x2 ) b.tiff 
I think the problem is the -set option:deskew:auto-crop 90 -deskew 40, when applied the deskew, a new white area appears making the codification to remove blacks areas not works.
[url]https://dl.dropbox.com/u/101393046/b2.tiff[\url]

I need a automated function to be used in all my images, i'm usuing ephesoft community to index and ensure quality of images. It is a lot of images per day , manual work it's impossible.

Any idea? I'm almost giving up, I'm already 4 days of it.

Re: Remove Black Borders and Deskew

Posted: 2012-11-08T15:46:31-07:00
by fmw42
No ideas to help, because as it is, this particular image has black from the signature going to the edge of the scan and into the black background. Unless you can somehow keep that from happening, you will not get the results you want. You somehow need to put a white border around the white part of the image to keep any black text from reaching the edges. That means it must be there when you scan the image.My only suggestion is to scan against some other colored background that is not white and not black.

Try not using -set option:deskew:auto-crop 90 and just use -trim +repage. But I am sure that will not help as that is essentially what I did in my example above.

You could install Cygwin and try my script, textcleaner.

This is the result.
http://www.fmwconcepts.com/misc_tests/t ... c25x10.tif

This is the result from my code above after manually erasing the bottom part of the signature at the bottom edge of the image.
http://www.fmwconcepts.com/misc_tests/t ... w_proc.tif

Re: Remove Black Borders and Deskew

Posted: 2012-11-08T16:39:43-07:00
by fmw42
Your image 2 works just fine for me using


convert b.tiff -bordercolor black -border 5 \
-fuzz 0% -fill white -draw "color 0,0 floodfill" \
-trim +repage +write 1tmp3.png -deskew 40 \
b_proc.tiff

You can remove -fuzz 0% since 0% is like not having a fuzz value. But I included it for more flexibility. So in Windows syntax, this would be (removing the extra tmp files).


convert b.tiff -bordercolor black -border 5 ^
-fill white -draw "color 0,0 floodfill" ^
-trim +repage +write 1tmp3.png -deskew 40 ^
-trim +repage ^
b_proc2.tiff


Here is the resulting image. I have added another trim at the end to make the result a bit smaller.
http://www.fmwconcepts.com/misc_tests/t ... proc2.tiff

Re: Remove Black Borders and Deskew

Posted: 2012-11-09T13:44:21-07:00
by PhilipeGatis
Thanks for help.....

I have apreciated your spent time to resolve my problem.

I cant change the background color from scanner, because dont have this option, i use KODAK Scanner 1420, and only have the option black background or white background, because i scan in b&w images.

And the option to erase manually the signature where touch on edge is impossible, because remove the originality from document, if my client to need the document to some juridic process, loses the originality and to be different from the original paper.

I think is impossible to do what i want in automatic form.

Thanks again

Re: Remove Black Borders and Deskew

Posted: 2012-11-09T13:47:37-07:00
by fmw42
It should work so long as your document does not have ink at the very edge of the image.

Alternately, you can make a small white frame to border you documents, so that you can scan it and be sure that there is white about the whole document on a black background.

Have you tried scanning with a white background?

Re: Remove Black Borders and Deskew

Posted: 2012-11-09T14:00:37-07:00
by PhilipeGatis
I will try digitalize with white background, and i will be back with results.