Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Charltsing opened this issue Nov 3, 2023 · 8 comments
Open

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

Charltsing opened this issue Nov 3, 2023 · 8 comments

Comments

@Charltsing
Copy link

Charltsing commented Nov 3, 2023

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

Charltsing commented Nov 5, 2023

thanks for your help

there are some sample images

img.zip

@DanBloomberg
Copy link
Owner

Thanks. That should be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants