--- id: wiki-2026-0508-straightening title: Straightening category: 10_Wiki/Topics status: verified canonical_id: self aliases: [Image Straightening, Perspective Correction, Deskew] duplicate_of: none source_trust_level: A confidence_score: 0.9 verification_status: applied tags: [computer-vision, image-processing, perspective-correction, opencv] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: language: python framework: opencv --- # Straightening ## 매 한 줄 > **"매 tilted image 의 axis-aligned 로 복원"**. Document scanning, satellite imagery, photo correction 의 fundamental preprocessing. Classical (Hough line + rotation, perspective transform) + modern deep learning (DeepDeskew, DocAligner) 의 combo. ## 매 핵심 ### 매 두 가지 problem - **Skew correction (rotation)**: 매 in-plane rotation 의 보정. Hough line 의 dominant angle detection. - **Perspective correction (homography)**: 매 4-point 의 quadrilateral → rectangle. 매 non-frontal photo 의 document. ### 매 classical pipeline 1. Edge detection (Canny). 2. Line detection (Hough transform) 또는 corner detection. 3. Dominant angle estimation 또는 4-point selection. 4. Rotation matrix / homography 계산. 5. Warp (affine / perspective). ### 매 modern (deep learning) - **DocTr / DocAligner** (2022+): document 의 corner regression. - **CNN-based skew angle predictor**: 매 single forward pass. - **LayoutLMv3-based**: 매 document understanding 의 part. ### 매 응용 1. Document scanning apps (CamScanner, Adobe Scan). 2. OCR preprocessing — 매 accuracy boost. 3. Satellite imagery alignment. 4. Receipt / business card capture. ## 💻 패턴 ### Skew detection via Hough ```python import cv2 import numpy as np def detect_skew(img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150) lines = cv2.HoughLines(edges, 1, np.pi / 180, 200) angles = [(theta * 180 / np.pi) - 90 for rho, theta in lines[:, 0]] return np.median([a for a in angles if -45 < a < 45]) ``` ### Rotation correction ```python def rotate_image(img, angle): h, w = img.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, 1.0) return cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) ``` ### Perspective correction (4-point) ```python def four_point_transform(img, pts): rect = order_points(pts) (tl, tr, br, bl) = rect width = max(np.linalg.norm(br - bl), np.linalg.norm(tr - tl)) height = max(np.linalg.norm(tr - br), np.linalg.norm(tl - bl)) dst = np.array([[0, 0], [width-1, 0], [width-1, height-1], [0, height-1]], dtype="float32") M = cv2.getPerspectiveTransform(rect, dst) return cv2.warpPerspective(img, M, (int(width), int(height))) def order_points(pts): rect = np.zeros((4, 2), dtype="float32") s = pts.sum(axis=1); diff = np.diff(pts, axis=1) rect[0] = pts[np.argmin(s)]; rect[2] = pts[np.argmax(s)] rect[1] = pts[np.argmin(diff)]; rect[3] = pts[np.argmax(diff)] return rect ``` ### Document corner detection (modern) ```python # OpenCV contour-based (heuristic) def find_document_corners(img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) edges = cv2.Canny(blurred, 75, 200) cnts, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] for c in cnts: approx = cv2.approxPolyDP(c, 0.02 * cv2.arcLength(c, True), True) if len(approx) == 4: return approx.reshape(4, 2) return None ``` ### Deep learning approach (DocAligner-style) ```python import torch from torchvision import transforms class CornerRegressor(torch.nn.Module): def __init__(self, backbone): super().__init__() self.backbone = backbone # ResNet34 self.head = torch.nn.Linear(512, 8) # 4 corners x (x, y) def forward(self, x): feat = self.backbone(x) corners = self.head(feat).view(-1, 4, 2) return torch.sigmoid(corners) # normalized [0, 1] ``` ## 매 결정 기준 | 상황 | Approach | |---|---| | 매 simple skew (rotation only) | Hough line + rotate | | 매 document photo | 4-point perspective | | 매 noisy / cluttered scene | DL corner regressor | | 매 mobile real-time | Lightweight CNN (MobileNet) | | 매 batch / cloud | LayoutLMv3 + classical refinement | **기본값**: 매 document → contour-based 4-point. 매 OCR pipeline → DocAligner. ## 🔗 Graph - 부모: [[Computer Vision|Computer-Vision]] - 응용: [[OCR]] ## 🤖 LLM 활용 **언제**: OCR 의 preprocessing pipeline, document understanding 의 normalization, vision-language model 의 input quality 개선. **언제 X**: 매 ill-defined edges (handwriting on textured background), 매 already aligned image (overhead). ## ❌ 안티패턴 - **Single Hough line**: 매 outlier 의 dominate. median angle 사용. - **Aggressive crop**: rotation 후 black border 의 crop 시 content loss. - **Over-correction**: 매 small skew (< 0.5°) 무시 — overhead > benefit. ## 🧪 검증 / 중복 - Verified (OpenCV docs, Hough 1962 patent, Suzuki & Be 1985 contour algorithm). - 신뢰도 A. ## 🕓 Changelog | 날짜 | 변경 | |---|---| | 2026-05-08 | Phase 1 | | 2026-05-10 | Manual cleanup — Hough + perspective + DL corner coverage |