Extract Page from Scanned Image

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
gvandyk
Posts: 15
Joined: 2015-09-08T12:40:32-07:00
Authentication code: 1151

Extract Page from Scanned Image

Post by gvandyk »

Hi

How can I extract a page from a scanned image that was taken with a camera?

I want to extract the the page that is displayed within the below image. I did manage to extract most of the page using "autotrim", but I am still left with the piece of the "left" page on the opposite glass.

How can I detect the page borders. I managed to get a rough outline with "-edge" and "-canny", but is still left with the outlines of the words as well.

Any help would be appreciated.

https://www.dropbox.com/s/ow0ntk3is4vwf ... 2.JPG?dl=0
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extract Page from Scanned Image

Post by fmw42 »

If you have IM 6.8.9-10 or higher, you can use -connected-components to get a clean mask. This works for me in IM 6.9.2.3 Q16 Mac OSX (unix syntax)

Code: Select all

convert IMG_0102.JPG -fuzz 10% -fill white -opaque "gray(200)" -fill black +opaque white tmp.gif
convert tmp.gif -define connected-components:verbose=true -connected-components 4 null:
Objects (id: bounding-box centroid area mean-color):
0: 4608x3456+0+0 2517.2,1260.0 8181266 srgb(0,0,0)
1738: 3305x2221+405+1021 2072.1,2104.4 6055383 srgb(255,255,255) <-- largest white region
20369: 3360x228+466+3228 2189.2,3358.1 595495 srgb(255,255,255)
3309: 58x1708+771+1360 796.2,2251.9 6636 srgb(0,0,0)
....

Code: Select all

convert tmp.gif -define connected-components:area-threshold=6055383 \
    -connected-components 4 -threshold 0 mask.png
convert IMG_0102.JPG mask.png -compose multiply -composite IMG_0102_page.png

see
http://magick.imagemagick.org/script/co ... onents.php
gvandyk
Posts: 15
Joined: 2015-09-08T12:40:32-07:00
Authentication code: 1151

Re: Extract Page from Scanned Image

Post by gvandyk »

Thank you very much, that did it.

Now, for the deskewing... Will have a crack at it
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extract Page from Scanned Image

Post by fmw42 »

see -deskew
Post Reply