Using Registration Marks To Crop?

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
blobby
Posts: 5
Joined: 2013-08-21T13:44:41-07:00
Authentication code: 6789

Using Registration Marks To Crop?

Post by blobby »

Hi:
I am using ImageMagick to clean up scanned black and white tiffs before passing them to our OMR (bubble mark reader) software. The problem with our forms is that they are printed out by users (from pdfs), and they come back all at slightly differnt scales, which confuses the OMR software. Currently I'm using a combination of shave (to get rid of the black lines around the edge from scanning), fuzz and trim (to crop to the edges of the printing), resize (to get them all to the same height), and gravity (to put them on a white background of a consistent size). This usually works pretty well, but it fails when there are too many marks outside the area we want to crop to.
The forms we use have barcodes in the top right and bottom left which it seems to me could be used as registration marks, or we could add some marks to the forms. What I would like is to be able to consistently scale the images so that the bubble marks are in the same position in each image. Has anyone done such a tranformation using ImageMagick (or anything else, really)?
Thanks for any input!
Blobby
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using Registration Marks To Crop?

Post by fmw42 »

See distort bilinearReverse

http://www.imagemagick.org/Usage/distor ... ar_reverse

You would need 4 corners


or distort Affine

http://www.imagemagick.org/Usage/distorts/#affine

which requires 3
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using Registration Marks To Crop?

Post by snibgo »

Printing your own registration marks seems the obvious route. They need to be different to anything else on the form. You can then subimage-search for the marks, distort and crop.

Searching can take a long time, but two techniques improve performance: (a) search small (cropped) areas of the form and (b) search a resized copy of the form, eg 10%, and use this result to refine the cropped area for the full-size search.
snibgo's IM pages: im.snibgo.com
blobby
Posts: 5
Joined: 2013-08-21T13:44:41-07:00
Authentication code: 6789

Re: Using Registration Marks To Crop?

Post by blobby »

Those distorts look like they would do the trick, but I'm not sure how I would get the coordinates for the corners. Is there a way to, for example, find a barcode in a general area, and then get the corner point from that? That seems to be the major difficulty.
Thanks,
Blobby

Edit: Hadn't seen the subimage-search post when I replied, I'll look into that. Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using Registration Marks To Crop?

Post by snibgo »

If forms have different barcodes, finding them is a messy problem. It's much easier to find something that is invariant between the forms.

You can also use registration marks to detect whether, for example, a scanned form is inverted.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using Registration Marks To Crop?

Post by fmw42 »

If your scanned images are different sizes, then one barcode reference image will not always match well with the scanned image. Matching like that is not scale invariant. However, using a cross for a marker, will generally do better, though even that will not be totally scale invariant. Such registrations are also not rotationally invariant.

At least with crosses, you do not have to offset the match by the size of the barcode to get its upper right or lower right or lower left corners. Though you do know the size of the template. But again it will not offset correctly if the scale of the scanned image is different from the reference barcode.
blobby
Posts: 5
Joined: 2013-08-21T13:44:41-07:00
Authentication code: 6789

Re: Using Registration Marks To Crop?

Post by blobby »

snibgo wrote:If forms have different barcodes, finding them is a messy problem. It's much easier to find something that is invariant between the forms.

You can also use registration marks to detect whether, for example, a scanned form is inverted.
Well, there's one barcode in the lower left which never changes; there are two more at the top right & left which change. I'm thinking that adding some crosses to each corner might be the best solution.
blobby
Posts: 5
Joined: 2013-08-21T13:44:41-07:00
Authentication code: 6789

Re: Using Registration Marks To Crop?

Post by blobby »

fmw42 wrote:If your scanned images are different sizes, then one barcode reference image will not always match well with the scanned image. Matching like that is not scale invariant. However, using a cross for a marker, will generally do better, though even that will not be totally scale invariant. Such registrations are also not rotationally invariant.

At least with crosses, you do not have to offset the match by the size of the barcode to get its upper right or lower right or lower left corners. Though you do know the size of the template. But again it will not offset correctly if the scale of the scanned image is different from the reference barcode.
That makes sense. If I could get the coordinates of the center of 4 crosses I'd be set.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using Registration Marks To Crop?

Post by fmw42 »

Once you get your resulting correlation image from the subimage search (via compare), you can process it for the highest scores with something like my script, maxima, at the link below (http://www.fmwconcepts.com/imagemagick/maxima/index.php). IM generally only reports one (best) match. You need to find the four best. My script may help you do that. See some of the examples at the reference.
blobby
Posts: 5
Joined: 2013-08-21T13:44:41-07:00
Authentication code: 6789

Re: Using Registration Marks To Crop?

Post by blobby »

Thanks, I'll try that.
Post Reply