0

I am making my own document scanner pipeline, this is good to note since image-specific methods of finding corners/contours..etc tend to be ineffective for a range of different images. I have found the best contour of the document and have retrieved an array of lines and their corresponding intersections.

Image with contour:

enter image description here

I am trying to narrow down all the lines/intersections to best fit the original contour, so I can get my 4 corner points of the document. I have tried narrowing it down based on the inner-angle of the intersections, then picking the best 4 intersections.

Image with line intersections with 90°(+-10°) interior angles (Green are the 4 best angles):

enter image description here

As you can see, the outcome is not ideal, I can make a rectangle with the green lines, and while it's the closest to a true rectangle, it's not the closest to the previous contour around the receipt.

What methods can I use to further narrow this down to pick intersections that better represent the shape of the original contour?

Reference Images:

Original Image:

enter image description here

Edged:

enter image description here

Douglas Gaskell
  • 9,017
  • 9
  • 71
  • 128
  • I would suggest using HoughLinesP with a threshold high enough to only get the four sides, and then you can find the intersections from the line segment endpoints using determinants. See my answer [here](https://stackoverflow.com/questions/44449871/fine-tuning-hough-line-function-parameters-opencv/44454619#44454619). I segmented those lines based off the fact that they were horizontal and vertical though; you can segment instead based off angle like you did here. – alkasm Jun 29 '17 at 00:54
  • Also check out this answer [here](https://stackoverflow.com/a/44054699/5087436) for an awesome method to normalize the surface brightness of your image once you nail down the mask. Super simple and incredibly effective. – alkasm Jun 29 '17 at 01:15
  • @AlexanderReynolds Thanks, I've read your posts before, however, in this case I have what I need as far as lines go, and I just need to sort out an efficient way to select the correct ones. The HoughLinesP is my backup method for finding these, but even then I end up at the same spot I am now with selecting the right lines. – Douglas Gaskell Jun 29 '17 at 01:33
  • What if you find the intersections of those lines, expand the contour location a bit (or find the `minAreaRect` or something surrounding it) and then only take the intersections inside that area? – alkasm Jun 29 '17 at 02:18
  • How do you define "closest to a true rectangle"? You are in a 3D space, a rectangle in 3D will not be a rectanlge on an image. Also "gree anre the best 4"-> shows 6 – Ander Biguri Jun 29 '17 at 08:53
  • To elaborate in the 3D space: https://openclipart.org/download/216995/3d-perspective-grid-very-long.svg All rectangles here are real rectangles, most of them have angles far from 90 degrees – Ander Biguri Jun 29 '17 at 08:55
  • 1
    @AnderBiguri Closest true rectangle are 4 points that represent the closest I can get to 4x 90 degree corners, which is shown in the image (if you exclude the odd one out). This is 2D space. The green & red grabs the best 4 intersections by angle, this could include up to 8 lines. It's a flawed approach I am trying to work around. You can see in the picture that there is another red line that would be a better choice to fit the original contour, I just need a way to identify that for this and other images. – Douglas Gaskell Jun 29 '17 at 14:53
  • 1
    Exactly, closest true is false in your case. You can not find a true rectangle like that. A true rectangle will be any rectangle that can be generated by appliying an affine transform to a falt 2D rectangle, or something liek that – Ander Biguri Jun 29 '17 at 15:32

0 Answers0