Trimming Noisy Images - another robust solution
Posted: 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)
-->
-->
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,
and for the horizontal trim the same
then, crop the original image:
what I used on windows, to trim all JPGs in the current directory:
The above examples were created with this.
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)
-->
-->
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:
Code: Select all
convert IMAGE.jpg -scale 100x0.00001% +dither -colors 2 -scale 100x200% -trim -border 10x0 -repage "!-10+0" -format "%wx0%O" info:
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"
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"