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:)

Re: Trimming Noisy Images - another robust solution

Posted: 2019-03-09T21:45:48-07:00
by djd
The clever script presented by Hugo Rune and approved by IM elders Anthony and Fred, is not quite so robust for me.

The original version of IM is not stated but the year was 2009. The versions I am using on Windows and Ubuntu are both:

IM 6.8.7-4 2013-10-26 Q16

a) For the -scale option, the number given for a 1 pixel strip is 0.00001.However, for the above IM versions, the number needed for a 1 pixel
strip needs to be close to correct. Eg for a 3000 pixel image, the to obtain a 1 pixel strip, 0.03 is required (0.03x3000/100=0.9). 0.00001 just does not work.

Can a calculation be applied within the convert string to make the setting automatic (%=100/h and %=100/w)?

b) In the script for the width and height of the -format string, in one case h is explicitly set at zero, in the second case, w is explicitly set to zero. For the above IM versions, setting h to zero results in garbled output. This can be worked around by using gres or sed to change the string after output from the -format option.

c) The final two commands in each section of the script seem superfluous (-border ...)???.

The trim script seems to work OK with high contrast images, even if the contrast is not visible to the naked eye. For example with the image:

convert -size 100x100 canvas:gray50 -bordercolor gray51 -border 10x10 gray50x51.png

However, with a scan of a quite readable text page, the -scale option produces a completely homogeneous result for both the vertical and horizontal strips. After a trim operation, the size dimensions for a crop are then just 1x1 pixels which are naturally of no use.

Using a `long-hand' script to average the pixels in each row and in each column to result in an average row and column of about 1 pixel width, a trim
each way can produce reasonable size dimensions for a crop of the original.

This was used on the text page mentioned and worked well.

What type of averaging is provided by -scale?

Re: Trimming Noisy Images - another robust solution

Posted: 2019-03-09T23:23:59-07:00
by fmw42
What type of averaging is provided by -scale?
It is a simple average of blocks of pixels. See https://imagemagick.org/script/command- ... .php#scale

Re: Trimming Noisy Images - another robust solution

Posted: 2019-03-10T08:31:03-07:00
by snibgo
To scale to a single column, use "-scale 1x!".

To scale to a single row, use "-scale x1!".

Re: Trimming Noisy Images - another robust solution

Posted: 2019-03-10T16:11:24-07:00
by anthony
Yes that was added later. Defaults when numbers not given being the images virtual canvas size.
Though that default for image area selection is more commonly used for -crop rather than -scale.

When percentages are involved the default is a copy of the other percentage, that was -crop 50% or -scale 50% is 50% in both X and Y directions.