Page 1 of 1

Trimming Noisy Images - another robust solution

Posted: 2009-07-18T15:02:16-07:00
by HugoRune
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.

Re: Trimming Noisy Images - another robust solution

Posted: 2009-07-18T17:47:08-07:00
by fmw42
it would be helpful to others if you could post an example before and after

Re: Trimming Noisy Images - another robust solution

Posted: 2009-07-19T02:07:24-07:00
by HugoRune
yeah, I added some.

Re: Trimming Noisy Images - another robust solution

Posted: 2009-07-19T20:39:48-07:00
by anthony
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.

Re: Trimming Noisy Images - another robust solution

Posted: 2009-07-19T22:04:50-07:00
by fmw42
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.

Re: Trimming Noisy Images - another robust solution

Posted: 2018-03-15T00:42:30-07:00
by Erik

Code: Select all

Version: ImageMagick 7.0.7-25 Q8 x86_64 2018-03-05 http://www.imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP
The following problem is most likely based on my IM version. I consulted the documentation, but was not able to figure out the correct argument.

Input file: https://imgur.com/aHyZnIo

Original command:

Code: Select all

convert IMAGE.jpg -scale 100x0.00001%  +dither -colors 2 -scale 100x200% -trim -border 10x0 -repage "!-10+0" -format "%wx0%O" info:
I modified the command.
And I tried to let -trim follow -border as otherwise the error convert: geometry does not contain image `in.jpg' occurred.
Also the -repage argument needs to be placed in apostrophes, using linux.
But then the output of format does simply state the original width of the image.

Code: Select all

echo $(convert in.jpg -scale 100x0.00001%  +dither -colors 2 -scale 100x200% -border 10x0 -trim  -repage '!-10+0' -format "%wx0%O" info:)