How to find ticket and crop it into a single image?
How to find ticket and crop it into a single image?
Hello,
I'm working on an OCR app project which needs to read ticket info from user-shot photos. The ticket may not be perpendicular or even distorted inside the photo so I can't just crop the image to get it.
Imagemagick can detect edges, but I don't know what to do next to crop and fix the distortion to get a rectangle image file from the edge info. Many thanks.
I'm working on an OCR app project which needs to read ticket info from user-shot photos. The ticket may not be perpendicular or even distorted inside the photo so I can't just crop the image to get it.
Imagemagick can detect edges, but I don't know what to do next to crop and fix the distortion to get a rectangle image file from the edge info. Many thanks.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to find ticket and crop it into a single image?
Please supply an example image. You can post to some place such as dropbox.com and put the URL here. Also please supply your IM version and platform. See viewtopic.php?f=1&t=9620
Re: How to find ticket and crop it into a single image?
This is the photo. I need to get the standard rectangle ticket image to guarantee OCR accuracy.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to find ticket and crop it into a single image?
A perspective transformation, moving the ticket corners to the corners of a rectangle, does what you want. Windows BAT syntax.
You can clean up t.png and pass it to OCR.
How do we get the corners of the ticket: 42,645 etc? There are many methods, including:
h.mvg gives four lines. The intersections are at the corners.
"+write t1.png" etc are only for debugging, so you can see the intermediate results in t1.png etc. Those lines can be removed.
Code: Select all
set DIST=42,645,0,0,^
498,544,500,0,^
99,966,0,500,^
614,824,500,500
%IM%convert ^
ticket.jpg ^
-distort perspective "%DIST%" ^
-crop 500x500+0+0 +repage ^
t.png
How do we get the corners of the ticket: 42,645 etc? There are many methods, including:
Code: Select all
%IM%convert ^
ticket.jpg ^
-contrast-stretch 10%%x10%% ^
-despeckle ^
+write t1.png ^
-threshold 50%% ^
-despeckle ^
-median 5x5 ^
+write t2.png ^
-canny 0x1+10%%+30%% ^
+write t3.png ^
-hough-lines 9x9+50 ^
+write t4.png ^
h.mvg
"+write t1.png" etc are only for debugging, so you can see the intermediate results in t1.png etc. Those lines can be removed.
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to find ticket and crop it into a single image?
What platform are you on? If Linux/MacOSX/Windows with Cygwin, you can try my scripts unperspective or whiteboard at the link below.
Re: How to find ticket and crop it into a single image?
Thank you for so detailed instruction! I'll have a try.
Re: How to find ticket and crop it into a single image?
snibgo wrote:A perspective transformation, moving the ticket corners to the corners of a rectangle, does what you want. Windows BAT syntax.Code: Select all
set DIST=42,645,0,0,^ 498,544,500,0,^ 99,966,0,500,^ 614,824,500,500 [/quote] How do I find the intersections? Thanks again...
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to find ticket and crop it into a single image?
See, for example, https://en.wikipedia.org/wiki/Line%E2%8 ... tersection
snibgo's IM pages: im.snibgo.com
Re: How to find ticket and crop it into a single image?
Many thanks. I'm trying your unperspective example 8 but get different result using the sample image/argument you provide(reporting too many peaks).fmw42 wrote:What platform are you on? If Linux/MacOSX/Windows with Cygwin, you can try my scripts unperspective or whiteboard at the link below.
I'm using Cygwin under Windows7 x64.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to find ticket and crop it into a single image?
The background is not clean enough -- too speckled to extract the picture. You may be better off with snibgo's solution.Many thanks. I'm trying your unperspective example 8 but get different result using the sample image/argument you provide(reporting too many peaks).
Re: How to find ticket and crop it into a single image?
Thank you for explanations.
I'm newcomer to IM, sorry for not-too-smart questions.
All code from Linux environment.
I have about the same situation - I need to pass photos to OCR.
And then there will be tons of photographed rectangular documents (2-5Mpix. mostly A4/Legal) that must be converted to flatbed scanner-like images.
Sample source image http://url.mik.lv/sc/1.jpg.
I've tuned your numbers a bit to get just 4 lines. It takes about 30 seconds to compute.
Code: Select all
convert 1.jpg -contrast-stretch 10%%x10%% -despeckle -threshold 50%% \
-despeckle -median 15x15 -canny 0x1+10%%+30%% -hough-lines 50x50+150 h.mvg
Code: Select all
# Hough line transform: 50x50+150
viewbox 0 0 3264 1836
line 476.236,0 -907.289,1836 # 171
line 0,-180.575 3264,2190.86 # 509
line 2944.17,0 1705.77,1836 # 274
line 0,643.842 3264,2929.32 # 609
Code: Select all
DIST='476.236,0,-907.289,1836,0,-180.575 3264,2190.86,2944.17,0 1705.77,1836,0,643.842 3264,2929.32'
convert 1.jpg -distort perspective "$DIST" +repage c1.jpg
I feel that I'm missing something simple, but important, at last stage.
And also probably it's a good idea (CPU time) to scale source image down before finding corners, but I have no idea how to handle all that coordinates.
Could you please point me to proper direction?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to find ticket and crop it into a single image?
Yes. The first stage finds four lines, giving the coordinates of each end.MikeG wrote:I feel that I'm missing something simple, but important, at last stage.
Then you distort the image to move one end of the first line to the other end, and so on for the other lines. That makes no sense.
A better idea is to find the intersections of the four lines. This gives the coordinates of the corners of the document. Then distort from those coordinates to the corners of a squared-up rectangle.
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to find ticket and crop it into a single image?
If you are using a Unix-like system, then you could try my script, unperspective, at my link below.
unperspective -f 50 -A 99 receipt.jpg receipt_unpersp.jpg
unperspective -f 50 -A 99 receipt.jpg receipt_unpersp.jpg