Compare image to template and rotate / crop accordingly

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
inkihh
Posts: 2
Joined: 2018-02-04T04:15:52-07:00
Authentication code: 1152

Compare image to template and rotate / crop accordingly

Post by inkihh »

Hi there!

I'm not sure if IM is a suited tool for what I need, but I guess I will find out by asking :-)

So I have to do OCR (which I will do with tesseract or something similar) on photos of receipts. The photos won't all be completely straight, so I'd like to find a way to compare the photos to a perfectly aligned and cropped photo of another receipt of this type - there are several objects on all receipts which never change - and then rotate/crop the photos, so that I can do the OCR on predefined areas. Do you guys have any ideas?

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

Re: Compare image to template and rotate / crop accordingly

Post by snibgo »

To straighten photos of receipts, the usual method is to find edges or corners of the receipt, and this gives a perspective distortion that can be applied.

If a predefined area is standard on all receipts, you could find that and use it as a reference for rotation and scaling. If there are multiple predefined areas, it would give the full perspective transformation.

There would be differences in lighting and exposure, so you would need to normalize these, perhaps by adjusting the mean and SD at each window search. The search is for the predefined subimage, at any scale, any rotation and any translation. The brute-force approach is massively slow, but a number of techniques can be used to improve performance. I show a few on my pages. But this is not an easy task.

If you can show some samples, we might give more specific advice.
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: Compare image to template and rotate / crop accordingly

Post by fmw42 »

Please always provide your platform and IM version if you have it installed. IM syntax is different for different systems. For example, I have a few bash unix shell scripts that may be of use. See textcleaner and textdeskew if the images are just text. Also unperspective. They can be found at my link below.
inkihh
Posts: 2
Joined: 2018-02-04T04:15:52-07:00
Authentication code: 1152

Re: Compare image to template and rotate / crop accordingly

Post by inkihh »

Hi,

it will be some kind of Linux version. If I need a specific version for a certain script, I will get that version :-)

So what I hoped was that someone (probably multiple persons) already solved exactly this problem - comparing an image to a template image and rotate/crop it if it matches.

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

Re: Compare image to template and rotate / crop accordingly

Post by fmw42 »

You will need to provide examples for us to see what your problem is about and and explanation relative to the images. I cannot visualize your issue at this point. You can upload images to some free hosting service such as dropbox.com and put the URLs here.

But note that ImageMagick's compare utility does not compare images of different scales or orientations. It only can compare and match images of the same scale and orientation, but find the relative offsets between the two images.
Post Reply