Tools,

Document edge detection, tested on 60 made-up photos

60 of 60

pages found by Apple Vision. OpenCV: 34

Tesseract then read 95.8% of words, not 34.4%.

Read your photos: $0.006 a page

Apple Vision's document request found every page

10 book pages on 6 drawn desks, corners known.

60 photos: pages found, and words read after the crop
Way Pages found Words read
No crop skipped 34.4%
OpenCV: Canny and contours 34 67.3%
OpenCV: plus saturation edges 48 82.3%
OpenCV: close, then Canny 33 72.8%
OpenCV: Otsu threshold 33 70.8%
Apple Vision: rectangles 57 94.3%
Apple Vision: document segmentation 60 95.8%
The true corners 60 95.8%

Found means the corners were off by 2% of the page diagonal or less, on average. That is 49 to 60 pixels here.

Where the OpenCV recipe fails

Three photos of book pages. Black outlines from Apple Vision fit each page. The OpenCV recipe drew no red outline on a gray desk, outlined one word on a white desk, and one square of a red plaid cloth.
Black: Apple Vision. Red: the OpenCV recipe. On the gray desk it found nothing.

On the gray and white desks, every miss had paper and desk within 21 gray levels of each other at the edge. Every find had 75 or more.

The OpenCV fix: add saturation edges

The usual recipe runs Canny on a gray copy only. Add a second Canny on the saturation channel:

sat = cv2.cvtColor(small, cv2.COLOR_BGR2HSV)[:, :, 1]
edges = cv2.Canny(gray, 75, 200) | cv2.Canny(sat, 75, 200)
The full Python script, 28 lines
import cv2
import numpy as np

img = cv2.imread("photo.jpg")
ratio = img.shape[0] / 500
small = cv2.resize(img, (int(img.shape[1] / ratio), 500))
gray = cv2.GaussianBlur(cv2.cvtColor(small, cv2.COLOR_BGR2GRAY), (5, 5), 0)
sat = cv2.cvtColor(small, cv2.COLOR_BGR2HSV)[:, :, 1]
edges = cv2.Canny(gray, 75, 200) | cv2.Canny(sat, 75, 200)
cnts, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for c in sorted(cnts, key=cv2.contourArea, reverse=True)[:5]:
    quad = cv2.approxPolyDP(c, 0.02 * cv2.arcLength(c, True), True)
    if len(quad) == 4:
        break
else:
    raise SystemExit("No page outline found")

pts = quad.reshape(4, 2).astype("float32") * ratio
s, d = pts.sum(1), np.diff(pts, axis=1).ravel()
tl, tr = pts[s.argmin()], pts[d.argmin()]
br, bl = pts[s.argmax()], pts[d.argmax()]
w = int(max(np.linalg.norm(tr - tl), np.linalg.norm(br - bl)))
h = int(max(np.linalg.norm(bl - tl), np.linalg.norm(br - tr)))
src = np.float32([tl, tr, br, bl])
dst = np.float32([[0, 0], [w - 1, 0], [w - 1, h - 1], [0, h - 1]])
M = cv2.getPerspectiveTransform(src, dst)
cv2.imwrite("page.png", cv2.warpPerspective(img, M, (w, h)))
print(src.round().astype(int).tolist())

Save your photo next to it as photo.jpg. It writes page.png, the flat page.

Apple Vision in 24 lines of Swift

One request finds the page. Core Image flattens it.

let request = VNDetectDocumentSegmentationRequest()
try VNImageRequestHandler(ciImage: image).perform([request])
The full Swift script, 24 lines
import CoreImage
import Vision

let url = URL(fileURLWithPath: CommandLine.arguments[1])
let image = CIImage(contentsOf: url)!
let request = VNDetectDocumentSegmentationRequest()
try VNImageRequestHandler(ciImage: image).perform([request])
guard let page = request.results?.first else {
  fatalError("No page found")
}
let size = image.extent.size
func px(_ p: CGPoint) -> CIVector {
  CIVector(x: p.x * size.width, y: p.y * size.height)
}
let flat = image.applyingFilter("CIPerspectiveCorrection", parameters: [
  "inputTopLeft": px(page.topLeft),
  "inputTopRight": px(page.topRight),
  "inputBottomLeft": px(page.bottomLeft),
  "inputBottomRight": px(page.bottomRight),
])
try CIContext().writePNGRepresentation(
  of: flat, to: URL(fileURLWithPath: "page.png"),
  format: .RGBA8, colorSpace: CGColorSpace(name: CGColorSpace.sRGB)!)
print(page.confidence, Int(flat.extent.width), Int(flat.extent.height))

Tesseract needs the crop. Our Actor less so

On pages turned 4 degrees or more, plain Tesseract read 18.8% of words as shot. Less turned: 75.3%.

PDF OCR to Markdown, our Apify Actor, levels the page. Its second reader, RapidOCR, rereads pages Tesseract reads badly.

Words read: 12 photos, 2 pages on 6 desks
Reader As shot Vision crop
Tesseract 16.5% 98.2%
Our Actor 94.7% 97.4%

What I did not test

How I tested it
  • Pages: 10 proofread book pages from Wikisource, 1,955 words, 1920 px wide scans.
  • Photos: each page pasted on 6 desks (wood, gray, white, plaid, cluttered, wood with a thumb on the edge). 2250 x 3000 px, JPEG.
  • Each page fills 70% to 85% of the height, turned up to 12 degrees, with a tilt, a light gradient, a soft shadow, blur and noise. The random seed is fixed.
  • The OpenCV ways run at 500 px high, then scale the corners back. The unwarp is the same for every way.
  • Tesseract: the command line, default page mode, English tessdata_fast 4.1.0, one thread.
  • Words read: true words found in order, after the crop, against the proofread text.
  • I added the saturation step after I saw the misses, on these same 60 photos. So its 48 is a best case.
  • The Actor: its own code at commit 5dcdaa5f, run on this Mac, with text output and the second reader on (the default).
  • opencv-python-headless 5.1.0.95, NumPy 2.5.5, Tesseract 5.5.4, Swift 6.4.2, Python 3.14.7, macOS 26.6 (arm64).
  • Scripts and results stay in our private repo, in research/bench/document-edge-detection-2026-11-23/.
Sources

Questions people ask

How do I detect the edges of a document in a photo?

Find the 4 corners, then warp them flat. Apple Vision's document segmentation found the page in all 60 of our test photos, on a Mac. OpenCV's Canny and contour recipe found 34, and 48 with saturation edges added.

Why does the OpenCV document scanner not find my page?

Three desks broke it: tan paper on a gray desk of nearly the same brightness (5 of 10 found), pale paper on a white desk (5 of 10), and a plaid cloth (0 of 10). Saturation edges fixed the gray and white desks.

Do I need to crop a photo before OCR?

For plain Tesseract, yes. It read 34.4% of the words on our photos as shot, and 95.8% after an Apple Vision crop. Our Apify Actor read 94.7% of the words on 12 photos as shot. It levels the page and has a second reader.

VNDetectDocumentSegmentationRequest or VNDetectRectanglesRequest?

Document segmentation found 60 of 60 pages. The rectangle request found 57: its misses were 2 plaid cloth photos and 1 cluttered desk. When both found the page, the rectangle corners were closer on 42 of 57.

Two ways to skip the work

Run it yourself. Our Apify Actor turns scanned PDFs and images into Markdown, tables included.

Try the Actor

Or send us the scan. Tack, our AI, does the job for $29, up to 50 pages. First page free.

Get one page free