I am trying to build a mobile-friendly document scanner. I’ve been using OpenCV for contour detection, but I’m struggling to get a clean four-point transform when the paper is at an angle. Does anyone have a reliable script for identifying the corners of a rectangular sheet accurately?
3 answers
To get a clean scan, the workflow should be: grayscale, Gaussian blur, Canny edge detection, and then finding contours. Once you have the contours, sort them by area and take the largest one. Use cv2.approxPolyDP to simplify the contour to four points. If your result doesn't have exactly four points, you might need to adjust the epsilon parameter. This OpenCV technique is the industry standard for mobile scanning apps. I used this exact logic for a FinTech app last year, and it worked perfectly even for wrinkled receipts, provided the contrast against the background was high enough.
Kimberly, how do you handle cases where the document has the same color as the table? Doesn't the edge detection fail in those low-contrast scenarios?
Make sure you sort your four coordinates in a consistent order (top-left, top-right, etc.) before passing them to the perspective transform function in OpenCV.
Correct, Karen. Without sorting the points, the image will come out warped or mirrored. I always use a small helper function to calculate the sum and difference of the coordinates to order them.
Daniel, that's a common hurdle. In those cases, I usually apply an adaptive threshold before the edge detection. It helps highlight the boundaries better. Another trick is to use the Hough Line Transform to find the dominant lines of the document and calculate their intersections. It’s a bit more computationally expensive, but it’s much more reliable when the background is messy or similar in color to the paper.