cropping document with variable background

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
mthstn
Posts: 2
Joined: 2013-06-18T09:36:51-07:00
Authentication code: 6789

cropping document with variable background

Post by mthstn » 2013-06-18T11:08:39-07:00

I am trying to crop out the background of a document so that I can then flatten and remove perspective of the document. I can use Fred Weinhaus's unperspective for that part. The problem I am having is preparing the image mask to use. I have an example document with a blank page on a wood table, I am trying to create a mask of the page and remove the background but am having difficulty doing so because of the darkness change of the wood.

Here's my original
Image

Here's after using unperpsective with a couple different -f values
Image

Image

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

Re: cropping document with variable background

Post by fmw42 » 2013-06-18T11:48:27-07:00

I don't have too much time right now, but try separating channels and see if one of them works better. Also try converting to other colorspaces and separate channels and see if one of them works.

Note that your page here is gray and has a brown background. Thus for gray saturation will be low. Perhaps you can do a fuzzy floodfill on the gray to white and then replace all non-white with black to create a mask.

aporthog
Posts: 37
Joined: 2012-05-30T08:24:46-07:00
Authentication code: 13

Re: cropping document with variable background

Post by aporthog » 2013-06-18T13:06:05-07:00

You can play around with something like this:

convert 0M5Hiq1.jpg -normalize -fuzz 50% -fill black -opaque red -fill black -opaque brown out.jpg

Arvin

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

Re: cropping document with variable background

Post by fmw42 » 2013-06-18T15:55:09-07:00

This seems to work well for me on IM 6.8.6.0 Q16 Mac OSX as I mentioned above separating the Saturation channel


convert 0M5Hiq1.jpg -colorspace HSL -channel g -separate +channel -fuzz 10% -fill black -opaque black -fill white +opaque black mask.png


But you can play with the -fuzz value

mthstn
Posts: 2
Joined: 2013-06-18T09:36:51-07:00
Authentication code: 6789

Re: cropping document with variable background

Post by mthstn » 2013-06-19T06:35:16-07:00

fmw42 wrote:This seems to work well for me on IM 6.8.6.0 Q16 Mac OSX as I mentioned above separating the Saturation channel


convert 0M5Hiq1.jpg -colorspace HSL -channel g -separate +channel -fuzz 10% -fill black -opaque black -fill white +opaque black mask.png


But you can play with the -fuzz value
Thanks! Will test this with a variety of captures that I have but it has worked well with the initial set. Greatly appreciated, also thanks for all your scripts on your site.

Post Reply