Trimming Noisy Images - another robust solution

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
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Trimming Noisy Images - another robust solution

Post by HugoRune » 2009-07-18T15:02:16-07:00

Just wanted to share the following solution for a problem I was struggling with:

problem: trimming a noisy images as per http://www.imagemagick.org/Usage/crop/#trim_blur
specifically, digital camera shots of printed documents in front of a noisy background, one document per image. (I used my camera as a scanner)

Image --> Image
Image --> Image

The solution from the tutorial with -blur -fuzz -trim worked, but required a lot of adjusting for the specific blur and fuzz factor for different images.

I found the following much more robust for this particular data:

for the vertical trim,
  • - shrink the image to a width of one pixel,
    - reduce colors to 2,
    - (stretch width to 2 pixels so that the -trim operator works)
    - trim the image
    - add a small border to avoid cutting of the edges of our object
    - extract the new height and vertical offset

Code: Select all

convert IMAGE.jpg -scale 0.00001x100%  +dither -colors 2 -scale 200x100% -trim -border 0x10 -repage "!+0-10" -format "0x%h%O" info:
and for the horizontal trim the same

Code: Select all

convert IMAGE.jpg -scale 100x0.00001%  +dither -colors 2 -scale 100x200% -trim -border 10x0 -repage "!-10+0" -format "%wx0%O" info:
then, crop the original image:

Code: Select all

convert IMAGE.jpg -crop \
`convert IMAGE.jpg  -scale 0.00001x100%  +dither -colors 2 -scale 200x100% -trim -border 0x10 -repage "!+0-10" -format "0x%h%O" info:` \
-crop \
`convert IMAGE.jpg -scale 100x0.00001%  +dither -colors 2 -scale 100x200% -trim -border 10x0 -repage "!-10+0" -format "%wx0%O" info:` \
+repage "IMAGE.png"
what I used on windows, to trim all JPGs in the current directory:

Code: Select all

for %a in (*.jpg) do 
   for /f "usebackq" %b in 
   (`convert "%a"  -scale 0.00001x100%  +dither -colors 2 -scale 200x100% -trim -border 0x10 -repage "!+0-10" -format "0x%h%O" info:`) do 
      for /f "usebackq" %c in 
      (`convert "%a" -scale 100x0.00001%  +dither -colors 2 -scale 100x200% -trim -border 10x0 -repage "!-10+0" -format "%wx0%O" info:`) do 
         convert "%a" -crop %b -crop %c +repage "%~na.png"
The above examples were created with this.
Last edited by HugoRune on 2009-07-19T02:05:48-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 22423
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Trimming Noisy Images - another robust solution

Post by fmw42 » 2009-07-18T17:47:08-07:00

it would be helpful to others if you could post an example before and after

HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Re: Trimming Noisy Images - another robust solution

Post by HugoRune » 2009-07-19T02:07:24-07:00

yeah, I added some.

User avatar
anthony
Posts: 8874
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trimming Noisy Images - another robust solution

Post by anthony » 2009-07-19T20:39:48-07:00

It is a very nice solution, and quite fast, Though limited to very strong rectangular regions of sharp contrast changes. It is a very good solution of you specific needs.

I thank you for contributing it to others, and would love to see more people to the same.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

User avatar
fmw42
Posts: 22423
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Trimming Noisy Images - another robust solution

Post by fmw42 » 2009-07-19T22:04:50-07:00

It is a bit similar in concept to what I used in my autotrim and unrotate scripts, and very clever. Averaging an image that is nearly binary to one row and/or column can provide a lot of information when sequencing along the row or column to look for the transitions.

Post Reply