Detect rectangle on the photo

A plethora of command-line scripts that perform geometric transforms, blurs, sharpens, edging, noise removal, and color manipulations.
Post Reply
Nihisil
Posts: 3
Joined: 2016-01-04T02:57:27-07:00
Authentication code: 1151

Detect rectangle on the photo

Post by Nihisil »

I'm working on the project that should get text from user shot photos with retail receipt.

Usually it is white rectangle on the various backgrounds. So, I need to do three steps:

1. Detect receipt content
2. Align the receipt, to be horizontal
3. Crop the not needed background

The issue that I don't know where is to start. I have read a lot of internet pages, but didn't find anything particular.

I guess there is no completed method, and I need to combine different image processing methods.

Maybe you can give me the point to the right road. I mean tell what articles I should to read, what methods to learn.

My system is Ubuntu 14. And I can install any Image Magick version.

Examples of images. A marked with red rectangle the content that I want to get.

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

Re: Detect rectangle on the photo

Post by fmw42 »

What IM version and platform? Please read viewtopic.php?f=1&t=9620.

If you are on unix systems, then see my script unperspective and textcleaner at the link below.
Nihisil
Posts: 3
Joined: 2016-01-04T02:57:27-07:00
Authentication code: 1151

Re: Detect rectangle on the photo

Post by Nihisil »

fmw42 wrote:What IM version and platform? Please read viewtopic.php?f=1&t=9620.

If you are on unix systems, then see my script unperspective and textcleaner at the link below.
Hello Fred.

My system is ubuntu 14.04 and my version of Imagemagick is

Code: Select all

Version: ImageMagick 6.9.3-0 Q16 i686 2016-01-05 http://www.imagemagick.org
I tried to run your script:

Code: Select all

sh unperspective.sh -f 20 -w 500 -V input.jpg output.jpg
And it is failed with this output:

Code: Select all

trap: ERR: bad trap
unperspective.sh: 964: unperspective.sh: Syntax error: "(" unexpected (expecting "fi")
Also, I tried run it with:

Code: Select all

bash unperspective.sh -f 20 -w 500 -V input.jpg output.jpg
And got output:

Code: Select all

unperspective.sh: line 343: type: unperspective.sh: not found
dirname: missing operand
Try 'dirname --help' for more information.
basename: missing operand
Try 'basename --help' for more information.

--- Number Of Peaks = 50 Is Larger Than 40 ---
Both runs didn't create output.jpg.

I have bash version: GNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)

I guess my bash is not suitable to your scripts. What bash I need to use to run your scripts?

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

Re: Detect rectangle on the photo

Post by fmw42 »

I am not sure what your problem is. The second with bash should have worked even though you have the dirname issue, which is caused by not having my script in your PATH. Nevertheless if that was all, it would still have made an output. See my Pointers on my home page.

However, I tested one of my examples on the unperspective page and am getting similar errors to the latter message with IM 6.9.3.0 Q16 Mac OSX.

Code: Select all

unperspective  -f 20 -V receipt1.jpg tmp.jpg 
--- Number Of Peaks Is Greater Than 4 ---


So something has changed in IM and I will try to fix it in the next day or so. I have some commitments tomorrow.

Best to always test with one of my examples before trying yours. If you want to send me one of your real images to see if I can get it to work after fixing the script, then email me your image (fmw at alink dot net).
Nihisil
Posts: 3
Joined: 2016-01-04T02:57:27-07:00
Authentication code: 1151

Re: Detect rectangle on the photo

Post by Nihisil »

Thank you for clarifications. I installed ImageMagick 6.8.9 and script working without issues. Will do more tests with images and etc.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect rectangle on the photo

Post by fmw42 »

Thanks for the info about 6.8.9.

It works for 6.9.2.0 and fails for 6.9.2.1 and above.

My suspicion is that it has to do with the following from the changelog:


2015-08-15 6.9.2-1 Cristy <quetzlzacatenango@image...>
Support color compliance for TXT format (e.g. -define txt:compliance=css) (bug report from Anthony).
The first argument of a hsl() color is integer, not percentage (reference viewtopic.php?f=2&t=26072).

I will check this out as soon as I can. But I can see in the unpersepective code where there was a trap for different versions of txt format (when it had changed before). So I hope this will be a simple fix that I may get to tomorrow.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect rectangle on the photo

Post by fmw42 »

OK. That was the issue.

I have uploaded a fix that seems to work for me now using IM 6.9.3.0 (and presumably the other versions starting with 6.9.2.1). Try downloading it anew from my web site and let me know if it works for you.
Post Reply