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: 25260
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
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: 8883
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
https://imagemagick.org/Usage/

User avatar
fmw42
Posts: 25260
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
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.

Erik
Posts: 12
Joined: 2016-04-30T00:18:15-07:00
Authentication code: 1151

Re: Trimming Noisy Images - another robust solution

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

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

djd
Posts: 41
Joined: 2011-11-12T22:20:18-07:00
Authentication code: 8675308

Re: Trimming Noisy Images - another robust solution

Post by djd » 2019-03-09T21:45:48-07:00

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?

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

Re: Trimming Noisy Images - another robust solution

Post by fmw42 » 2019-03-09T23:23:59-07:00

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

snibgo
Posts: 11913
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trimming Noisy Images - another robust solution

Post by snibgo » 2019-03-10T08:31:03-07:00

To scale to a single column, use "-scale 1x!".

To scale to a single row, use "-scale x1!".
snibgo's IM pages: im.snibgo.com

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

Re: Trimming Noisy Images - another robust solution

Post by anthony » 2019-03-10T16:11:24-07:00

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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

Post Reply