Remove blackborder after scanning

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
adil@alfasoft.ro
Posts: 24
Joined: 2012-01-10T05:19:23-07:00
Authentication code: 8675308

Remove blackborder after scanning

Post by adil@alfasoft.ro »

I scanned some books and randomly there are pages with a black border because of skewing in different sides of the page (probably because the page was not cut straight).

What i would like to do is either:

1.Automatically Color the black borders in white
2.Automatically Cut the black borders

I've attached an example showing the problem that i have.

Image

Thanks for your help!
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Remove blackborder after scanning

Post by whugemann »

Something like

Code: Select all

convert test.jpg -deskew 40% -crop 480x680+10+10 -gravity center -extent 500x700 ready.jpg
should do: It corrects the angle by -deskew, then crops the page and extends it again, assuming that the borders of the page are, if not black, then more or less plain white. The code refers to the dimensions of your example.
Wolfgang Hugemann
adil@alfasoft.ro
Posts: 24
Joined: 2012-01-10T05:19:23-07:00
Authentication code: 8675308

Re: Remove blackborder after scanning

Post by adil@alfasoft.ro »

Thank you. The problem is that the black border appear randomly and the width and position is randomly.In my opinion, first operation must be color in white the blackborder, and after that the deskew. But I dot know how to do.
There is an example somewhere in the forum
-bordercolor black -border 4x4 -fuzz "30%" -trim .....
but in my case is not working.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove blackborder after scanning

Post by fmw42 »

try this. seems to work for me

convert 28uibes.jpg -bordercolor black -border 1 -fuzz 95% -fill white -draw "color 0,0 floodfill" -alpha off -shave 1x1 28uibes_result.png
adil@alfasoft.ro
Posts: 24
Joined: 2012-01-10T05:19:23-07:00
Authentication code: 8675308

Re: Remove blackborder after scanning

Post by adil@alfasoft.ro »

It's working! Thank you very much Fred.
hri
Posts: 2
Joined: 2013-05-30T06:11:17-07:00
Authentication code: 6789

Re: Remove blackborder after scanning

Post by hri »

Hello,

I have the same problem but i need to do this with Magick.NET is there a way to do this ?

Regads,
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove blackborder after scanning

Post by fmw42 »

Magick.NET is listed as Imagemagick API at http://www.imagemagick.org/script/api.php. So in principle it should work. But I know little about any APIs. So cannot advise past command line concepts.

__________________________


Another way, is to average the image down to one row and analyze the profile to find where the black edges end and pure white starts. Or to find where the pure white ends and you have the text area. Then crop as appropriate and if desired pad back out with white. Using the image provided at the first post:


convert 28uibes.jpg -resize x1! miff:- | im_profile - 28uibes_profile.png

Image
hri
Posts: 2
Joined: 2013-05-30T06:11:17-07:00
Authentication code: 6789

Re: Remove blackborder after scanning

Post by hri »

Another way, is to average the image down to one row and analyze the profile to find where the black edges end and pure white starts
Can you please clarify your idea? how to average the image down to one row? Is there a way with Magick.NET?
I simply need to calculate the black border width and height that is in the image and replace it with white border or crop it....

Thanks in advanced
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove blackborder after scanning

Post by fmw42 »

the command is as above, but probably better to use -scale

convert 28uibes.jpg -scale x1! image.txt


Once you have the 1 row image as text, you can filter for the color and coordinates. Then find the range of the pure white borders on the left and on the right and crop in the middle of the white borders (or on either the inside or the outside edge of the white -- anywhere is fine). Then pad back out to the original size if desired.

With the image above, there is black on the bottom. So what you probably need to do is find the max (near white) on the left and right, crop, then repeat with averaging to one column and do the same vertically.
Post Reply