perspective_transform

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

perspective_transform

perspective_transform

La transformación de perspectiva (homografía) rectifica la imagen del documento para que aparezca como si hubiera sido fotografiado perfectamente de frente, mejorando la calidad del OCR y face match.
透视变换(单应性变换)可对文档图像进行校正,使其看起来像是完全正对拍摄的效果,从而提升OCR和人脸匹配的质量。

When to use

适用场景

Usar inmediatamente después de detectar los 4 vértices del documento con
opencv_contour_detection
.
在使用
opencv_contour_detection
检测到文档的4个顶点后可立即使用。

Instructions

使用说明

  1. Ordenar los 4 puntos esquina: top-left, top-right, bottom-right, bottom-left.
  2. Calcular dimensiones del documento rectificado:
    maxWidth = max(distance(br, bl), distance(tr, tl))
    .
  3. Definir puntos destino:
    dst = np.array([[0,0],[maxWidth,0],[maxWidth,maxHeight],[0,maxHeight]])
    .
  4. Calcular matriz de transformación:
    M = cv2.getPerspectiveTransform(src_pts, dst_pts)
    .
  5. Aplicar transformación:
    warped = cv2.warpPerspective(img, M, (maxWidth, maxHeight))
    .
  6. Verificar que el resultado tiene las proporciones esperadas del tipo de documento (DNI: 85.6mm × 54mm = ratio 1.585).
  1. 对4个角点进行排序:左上、右上、右下、左下。
  2. 计算校正后文档的尺寸:
    maxWidth = max(distance(br, bl), distance(tr, tl))
  3. 定义目标点:
    dst = np.array([[0,0],[maxWidth,0],[maxWidth,maxHeight],[0,maxHeight]])
  4. 计算变换矩阵:
    M = cv2.getPerspectiveTransform(src_pts, dst_pts)
  5. 应用变换:
    warped = cv2.warpPerspective(img, M, (maxWidth, maxHeight))
  6. 验证结果是否符合对应文档类型的预期比例(身份证:85.6mm × 54mm = 比例1.585)。

Notes

注意事项

  • Proporciones de referencia: DNI español (ID-1): 85.6 × 54mm; Pasaporte: 125 × 88mm.
  • Un ratio muy diferente al esperado puede indicar documento falso o imagen incorrecta.
  • 参考比例:西班牙身份证(ID-1):85.6 × 54mm;护照:125 × 88mm。
  • 如果比例与预期值相差过大,可能表示文档为伪造或图像不正确。