DanBloomberg / leptonica

Leptonica is an open source library containing software that is broadly useful for image processing and image analysis applications. The official github repository for Leptonica is: danbloomberg/leptonica. See leptonica.org for more documentation.
Other
1.77k stars 389 forks source link

How to detect the four corners of paper or ID card #722

Open Charltsing opened 11 months ago

Charltsing commented 11 months ago

800 example3

like these picture, I need to obtain the pixel coordinates of the four top corners of the paper or ID card

Awesome-Edge-Detection-Papers

DanBloomberg commented 11 months ago

It's a great question. There is nothing in the leptonica toolbox that does this in a simple and robust way.

One approach is to try to get a binarized image where the background is (say) white and much of the rest is black. To do that, one might first get the median (component by component) of all colors that are not bright, and identify the background (binarized to white) as colors that are sufficiently close to that median. Everything else is binarized to black. So ideally you would get a black quadrangle on a white background, but you likely would need to do morphological filtering to remove noise. Then, if there is no remaining noise in the white background, you can get most of the corners by finding the bounding rectangle of the foreground. To get the corners more robustly, you can use morphological hit-miss structuring elements that represent each of the four corners. For this to work well, it would be a good idea to start by aligning the objects roughly with the edges of the image before you take the picture.

DanBloomberg commented 11 months ago

Better: once the image has been binarized with noise removal, I would find the bounding boxes of the foreground components, selecting only ones that are large enough. Most of the corners will touch the bounding box. If you don't get 4 corners that way, you can traverse the boundary of the c.c. and look for a change in direction. Alternatively, a corner that does not touch the bounding box can usually be found by sweeping in from the corner of the bounding box along 45 degree lines. (This works for any convex quadrangle where the two edges for the missing corner don't both have slopes either less than 1 or greater than 1 -- i.e., one slope is <1 and one is >1)

Charltsing commented 11 months ago

thank you for your help.

i think it is suitable for one subject in image . (sweeping in from the corner of the bounding box along 45 degree lines)

but if you could provide sample code, I would greatly appreciate it . ( I know only a little about leptonica.)

Charltsing commented 11 months ago

Another approach is to use canny edge detection,then sort to find the longest four edges, If the background is not pure, this method will recognize incorrect edge lines. Imaging-Library

DanBloomberg commented 11 months ago

Selecting the largest connected components of the non-background, and working with their bounding boxes will allow you to identify multiple 4-sided objects in an image, as long as the bounding boxes from different quadrangles do not overlap (intersect).

I'll see what I can do about writing some code to demonstrate an approach to the problem.

Dan

DanBloomberg commented 11 months ago

Also, we don't have Canny edge detection in leptonica, and using edge detection will also have the problem of noise from within the non-background regions. However, edge detection may be a good way to identify the background (because there won't be any long edges)!

Charltsing commented 11 months ago

thanks for your help

there are some sample images

img.zip

DanBloomberg commented 11 months ago

Thanks. That should be useful.