How Document Scanning Works Behind the Scenes: Edge Detection, Perspective Correction and Shadow Removal
11 min read
There's a visible difference between a document photo you take with your phone and an image that comes out of an office scanner. In the photo the paper sits at an angle, one edge looks wider than the other, your own shadow falls across it, and the surface underneath creeps into the frame. In the scanner output the page is perfectly straight, its edges are parallel, and the lighting is even.
Here's the interesting part: most of that difference isn't optical, it's mathematical. Which means that when you apply the right image processing steps, it's quite possible to bring an ordinary phone photo close to a scanner output. In this article we look at the three core steps the Document Scanner tool applies behind the scenes — edge detection, perspective correction and shadow removal — technically, but in a readable way.
The root of the problem: a camera is not a scanner
A flatbed scanner looks at the document from a fixed distance, at a perfectly perpendicular angle, under controlled light. A phone camera guarantees none of these:
- The angle isn't fixed. Looking at the paper perfectly straight on is practically impossible; even a tilt of a few degrees turns a rectangle into a trapezoid.
- The distance isn't the same at every point. The edge of the page nearest to you looks larger, the far edge smaller.
- The lighting isn't even. Light from the window illuminates one side while the other stays in shadow; on top of that, you and your phone cast your own shadows.
- There are things other than paper in the frame. The desk, a book, part of your hand.
Each of the image processing steps targets one of these problems.
Step 1: Edge detection — where does the paper begin?
The first question is this: which region of this photo is the document, and which is the background? The human eye does this instantly, but an algorithm has to derive it from pixel values.
The approach used is contour detection. The intensity differences between neighboring pixels in the image are computed; if pixel values change rapidly in a region, that means there's a boundary there. These boundary points are joined up to produce closed curves (contours).
In a document photo there are dozens of contours: the edge of the paper, the lines of text, the wood grain on the desk, the border of a shadow. But only one of them is what's being looked for — the contour that forms the outer boundary of the paper, has four corners, and covers a large portion of the image.
The success of this detection depends directly on contrast. If white paper is on a dark desk, the intensity difference at the boundary is large and the contour comes out clearly; if it's on a light-colored desk, the difference is small, and the contour comes out either broken or not at all. If one of the corners is outside the frame, a closed quadrilateral can't form in the first place.
What happens when it can't be found?
This is where a deliberate design choice in the tool comes into play: if the edge can't be found, the photo is used as it is and the process doesn't fail.
At first glance this may look like "lazy" behavior, but it's actually the right one. Consider the alternative: the algorithm makes a guess even though it isn't confident, and crops based on that guess. Wrongly guessed corners mean half the document gets cut off. Cutting away the signature line of a contract or the amount field of an invoice is a far worse outcome than not cropping at all.
So the system chooses not to intervene in the face of uncertainty. The price of that choice is that you're left with an uncropped photo — but that's reversible: you retake the photo on a more contrasting surface. A document that's been cut off is not reversible.
Step 2: Perspective correction — turning a trapezoid into a rectangle
Once the four corners have been found, the second step begins: the four-point perspective transform.
The problem is formulated like this. In the real world a sheet of A4 paper is a rectangle; all four of its angles are 90 degrees. But in a photo taken at an angle that rectangle appears as a trapezoid — the near edge long, the far edge short, the angles other than 90 degrees. The aim is to turn the trapezoid in the photo back into a rectangle.
Mathematically, this is the mapping of a plane from one view to another. You have four source points (the corners of the trapezoid in the photo) and four target points (the corners of the output rectangle). These four point pairs determine the transformation between the two planes uniquely. Once the transformation has been computed, for every pixel in the output image the corresponding position in the source image is found and the color value is taken from there.
The practical result is that you get an image as if you were looking at the page straight on: the edges become parallel, the lines of text become horizontal.
The assumption behind the transform, and its limit
There's a critical assumption in this calculation: the surface being transformed is a plane. If the paper is spread flat on the desk, the assumption holds and the result is very good.
But if the paper is curled, crumpled, or is a page near the binding of a book, the surface is no longer a plane. The transform is still applied — the edges get straightened — but the curvature on the surface remains. You see this most clearly on book pages: the edges straighten out, but the text in the region near the binding stays slightly curved and compressed. The practical takeaway is simple: spread the document out on as flat a surface as you can.
Step 3: Shadow removal — evening out uneven light
The third step, and perhaps the one that changes the result the most. A page whose perspective has been corrected can still look "like a photo," because the lighting isn't even: the left side is bright, the right side gray, one corner in shadow.
The method applied here rests on this logic: when you blur an image heavily, all that's left behind is the distribution of light.
It consists of two steps:
- A copy of the image is blurred strongly. This operation erases fine detail — the text, the lines, the texture — entirely; what remains is a smooth intensity distribution showing how bright each region of the page is. We can call this the background map.
- The original image is divided by this map. The division normalizes each pixel against the average brightness of its own region.
The result is this: a pixel in a bright region gets divided by a high background value and is pulled down, while a pixel in a shadowed region gets divided by a low value and is pulled up. That way every part of the page arrives at the same brightness level.
The critical point is that the text isn't affected by this operation. Because the text was erased during the blurring, it doesn't appear in the background map — and something that isn't in the map can't be neutralized by the division either. The background gets evened out while the text keeps its contrast; in fact, since the background becomes whiter, its relative contrast increases.
The method works very well on soft transitions, such as one-sided light from a window or the gradual brightness difference created by a desk lamp. Where it struggles is with sharp-edged, very dark shadows: the hard shadow your hand casts onto the paper remains in the map as a sharp boundary even after blurring and can't be fully neutralized.
Step 4: Filters and A4 normalization
After the three core steps comes one final processing layer: filter selection. Four modes apply different finishing operations to the same corrected image.
Document mode (the default) combines shadow removal with contrast enhancement and sharpening. It's designed for the majority of text-heavy pages: white background, dark and crisp text.
Grayscale removes color information; shadow removal still runs here, but the contrast is kept gentler than in document mode. Its job is to preserve tones rather than to harden the text, which is why it's safer than black & white on pages containing photos or shaded charts.
Black & white applies thresholding: every pixel above a threshold value becomes white and every pixel below it becomes black, with no intermediate tones. Of the four modes this one produces the smallest file, but it causes serious information loss on pages carrying grayscale content.
In Original mode the color image is left untouched; only edge detection and perspective correction are applied. One detail is worth noting: shadow removal runs in all three of the other modes, and original is the only one that skips it. So this is the right choice when the color information itself is meaningful — a blue signature, a colored stamp, color-coded tables — and when a photo or a grayscale chart on the page has to come through completely untouched.
Finally, every page is fitted to A4 size. The page orientation is chosen per image: a portrait shot produces a portrait A4 page (595x842 points) and a landscape shot a landscape A4 page (842x595 points). The value of this step shows up with multi-page documents — even if you shot each photo from a different distance, the output looks consistent. With a mixed-orientation shoot, of course, the page sizes won't all match.
The optional last step: the text layer
At its core the document scanner is an image processing tool, but it can also recognize text at the end of the job if you want it to. Pick Turkish, English, or Turkish + English from the Text recognition (OCR) dropdown on the tool page and an invisible text layer is placed over the finished PDF, making the document searchable. The default option is "Don't add": text recognition noticeably lengthens processing time, so it's there for those who want it.
If you leave the dropdown alone, every page of the output stays a corrected and cleaned-up image — readable to the eye, but still a picture from the computer's point of view.
It's no coincidence that recognition runs last, on the finished PDF rather than on the individual photos. Recognition accuracy depends directly on the quality of the input image: slanted lines, a shadowed background and low contrast are the three main factors that make character recognition harder — and those are exactly the three problems the earlier steps solve. If you already have a properly scanned PDF and a text layer is all you need, there's a separate OCR tool for that.
What does this mean for privacy?
These operations — contour analysis, perspective transform, blurring and division — run on the server side. Unlike some PDF tools that run inside the browser, here the file is transferred to the server, processed, and the result comes back to you. The files are deleted once processing is complete. Because document photos often carry sensitive content — an ID, a contract, an invoice, a medical report — it's worth being aware of this point as you use the tool.
Conclusion
What turns a phone photo into a scanned document isn't a single magic operation; it's three calculations that complement one another. Contour detection finds where the paper is, the four-point perspective transform corrects the angle, and dividing by the background map evens out the lighting. Each step has its own assumption: edge detection wants contrast, perspective correction assumes a plane, and shadow removal gives its best result with gradual differences in light.
Knowing these limits lets you use the tool better — because the three simple choices you make while taking the photo (dark surface, flat paper, even light) actually help each one of these three algorithms directly.
Frequently Asked Questions
Why does shadow removal start by blurring the image?
Because blurring erases the fine detail in the image (text, lines, texture) and leaves behind only the large-scale distribution of light and dark. That distribution is effectively a map of how the light fell on the page. When the original image is divided by this map, every pixel is normalized against the average brightness of its own region; that way a page that is bright on one side and shadowed on the other ends up evenly lit.
Why does edge detection sometimes fail to find the paper?
Contour detection relies on the intensity difference between neighboring regions. If a white sheet of paper is sitting on a light-colored desk, there isn't enough difference at the boundary and the outline of the paper can't be perceived as a closed quadrilateral. The same problem occurs when a corner of the paper falls outside the frame or when a sharp shadow falls across it. In those cases the tool doesn't crop at all and uses the photo as it is.
Does perspective correction also fix creases in the paper?
No. The four-point perspective transform is a linear transformation that converts the view of a flat surface from one angle into a head-on view; the fundamental assumption behind the math is that the surface is a plane. The binding curve on a page shot from a book, or the waviness of a crumpled sheet, breaks that assumption and remains after the transform. For the best result you need to spread the paper out on a flat surface.
Try this out right away with Belge Tarayıcı.
Try Belge Tarayıcı