Page 1 of 1

Trim left and top based on index image

Posted: 2016-07-18T16:40:48-07:00
by iknowmac
Hi Guys... I'm trying to trim the top and left of a registration based on a barcode and block rectangle. See image...

https://goo.gl/photos/rFDiKzzhUeKoWWiu7

Here is the code I'v tried, but have gotten inconsistent behavior...

Code: Select all

convert input.jpg -gravity NorthWest -crop \
  `convert input.jpg -canny 0x1+10%+30% -virtual-pixel edge -blur 0x5 -fuzz 10% -trim \
  -format '%[fx:w-20]x%[fx:h-20]+%[fx:page.x-10]+%[fx:page.y-10]' \
  info:` +repage output_trimmed.jpg
Here is an image of what I am looking to achieve...

https://goo.gl/photos/zitxKiGLKhNbg76SA

ImageMagick version...
Version: ImageMagick 6.9.5-2 Q16 x86_64 2016-07-13 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib
Any assistance would be appreciated...

Re: Trim left and top based on index image

Posted: 2016-07-18T17:38:18-07:00
by snibgo
You might also show an example where your command doesn't do what you want.

You might find it helpful at the start to turn the most of the paper white, and some ink black:

Code: Select all

convert input.jpg -contrast-stretch 1%x50% -canny 0x1+10%+30% -virtual-pixel edge  {etc...}
And, of course, don't use JPEG unless you really have to.

Re: Trim left and top based on index image

Posted: 2016-07-19T14:24:06-07:00
by iknowmac
This is tremendous snibgo... It's getting closer and closer... Below are three examples of what I mean by "inconsistent behavior" using your suggestion.

https://goo.gl/photos/En9HE9mphZEwbgtL7
https://goo.gl/photos/DhNbMYnsB4trzFa68
https://goo.gl/photos/yTZPVF8YFmZx7XzQA

Sometimes the top and left are right on, and other times is over or under. Would it be possible to use just the black rectangle on the left to get an accurate alignment each time? I really don't need the barcode, I thought it would just be easier to align on.

Thanks for all you advice...

Re: Trim left and top based on index image

Posted: 2016-07-21T13:04:24-07:00
by iknowmac
After a lot of tweaking I finally have something usable...

Code: Select all

convert input.png -crop \
  `convert input.png -morphology Smooth Octagon:3 -canny 0x1+10%+30% -trim -format '%[fx:w]x%[fx:h]+%[fx:page.x]+%[fx:page.y]' info:` \
  +repage output_trimmed.png
However is there a way to only apply the trim to the top and left of the image and leave the rest alone?

Re: Trim left and top based on index image

Posted: 2016-07-21T13:16:58-07:00
by snibgo
"-crop" takes parameters WxH+X+Y. If W==0, it will crop from the x-offset to the remainder of the width. Likewise H.

So just use 0x0 instead of %[fx:w]x%[fx:h]