Page 1 of 1

Compare image to template and rotate / crop accordingly

Posted: 2018-02-04T11:21:20+00:00
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

Re: Compare image to template and rotate / crop accordingly

Posted: 2018-02-04T13:05:36+00:00
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.

Re: Compare image to template and rotate / crop accordingly

Posted: 2018-02-04T18:27:41+00:00
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.

Re: Compare image to template and rotate / crop accordingly

Posted: 2018-02-05T08:09:14+00:00
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

Re: Compare image to template and rotate / crop accordingly

Posted: 2018-02-05T17:47:51+00:00
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.