.NET에서 문서 이미지의 원근 왜곡 보정

블로그 카테고리: 이미징.NET

2023/12/08

현재 문서 이미지는 대부분 스마트폰 카메라로 촬영됩니다. 하지만 촬영 대상 문서 위에 카메라를 이상적인 위치에 배치하기가 어렵기 때문에 거의 모든 이미지에 원근 왜곡이 발생합니다.

다음은 최신 스마트폰 카메라로 촬영한 회전된 문서 이미지의 예입니다.
Document image that is captured by phone camera
Document image that is captured by phone camera


전자 문서 작업을 위한 거의 모든 시스템은 회전되거나 왜곡되지 않은 이미지를 필요로 합니다. 따라서 회전되거나 왜곡된 이미지를 수정(바로잡기)하는 작업이 필요합니다.
VintaSoft Document Cleanup .NET Plug-in 7.3(VintaSoft Imaging .NET SDK 12.3)에는 문서 이미지의 원근 왜곡을 수정하는 명령이 추가되었습니다.

DocumentPerspectiveCorrectionCommand는 다음과 같은 문제를 해결하는 데 사용할 수 있습니다.

참고: DocumentPerspectiveCorrectionCommand는 문서 이미지 테두리의 대비가 적절한 경우, 즉 문서 테두리는 사람의 눈으로 쉽게 볼 수 있습니다.

다음은 DocumentPerspectiveCorrectionCommand로 처리된 수정된 문서 이미지의 예입니다.
Corrected document image that was processed by DocumentPerspectiveCorrectionCommand
Corrected document image that was processed by DocumentPerspectiveCorrectionCommand



다음은 문서 이미지의 원근 왜곡을 수정하는 방법을 보여주는 C# 코드입니다.
/// <summary>
/// Corrects perspective distortion of document image.
/// </summary>
/// <param name="sourceFile">Source image file.</param>
/// <param name="resultFile">Result image file.</param>
public void ApplyPerspectiveCorrection(string sourceFile, string resultFile)
{
    using (Vintasoft.Imaging.VintasoftImage image =
        new Vintasoft.Imaging.VintasoftImage(sourceFile))
    {
        // create the perspective correction command
        Vintasoft.Imaging.ImageProcessing.Document.DocumentPerspectiveCorrectionCommand command =
            new Vintasoft.Imaging.ImageProcessing.Document.DocumentPerspectiveCorrectionCommand();

        // apply the perspective correction to a document image
        command.ExecuteInPlace(image);

        // save the result image to a file
        image.Save(resultFile);
    }
}


문서 이미지의 가로세로 비율을 미리 알고 있는 경우 DocumentPerspectiveCorrectionCommand.DocumentAspectRatio 속성을 사용하여 가로세로 비율을 지정해야 합니다.
다음은 A4 문서 이미지의 원근 왜곡을 수정하는 방법을 보여주는 C# 코드입니다.
/// <summary>
/// Corrects perspective distortion of A4 document image.
/// </summary>
/// <param name="sourceFile">Source file.</param>
/// <param name="resultFile">Result file.</param>
public void ApplyPerspectiveCorrectionA4Paper(string sourceFile, string resultFile)
{
    using (Vintasoft.Imaging.VintasoftImage image =
        new Vintasoft.Imaging.VintasoftImage(sourceFile))
    {
        // create the perspective correction command
        Vintasoft.Imaging.ImageProcessing.Document.DocumentPerspectiveCorrectionCommand command =
            new Vintasoft.Imaging.ImageProcessing.Document.DocumentPerspectiveCorrectionCommand();

        // set the document aspect ratio to the aspect ratio of the A4 paper
        Vintasoft.Imaging.ImageSize paperSizeA4 = Vintasoft.Imaging.ImageSize.FromPaperKind(Vintasoft.Imaging.PaperSizeKind.A4);
        command.DocumentAspectRatio = paperSizeA4.WidthInDip / paperSizeA4.HeightInDip;

        // apply the perspective correction to a document image
        command.ExecuteInPlace(image);

        // save the result image to a file
        image.Save(resultFile);
    }
}


DocumentPerspectiveCorrectionCommandQuadrilateralWarpCommand를 사용하여 원근 보정 변환을 수행합니다. 왜곡된 이미지의 모서리 점을 알고 있다면 QuadrilateralWarpCommand를 사용하여 원근 왜곡의 역변환을 수행할 수 있습니다.
왜곡된 이미지의 모서리 점을 알고 있는 경우 문서 이미지의 원근 왜곡을 수정하는 방법을 보여주는 C# 코드입니다.
/// <summary>
/// Corrects perspective distortion of document image.
/// </summary>
/// <param name="sourceFile">Source image file.</param>
/// <param name="resultFile">Result image file.</param>
/// <param name="documentImagePoints">An array of four points, which define the corner points of document image.
/// Points should be set in the following order: 0 - top-left, 1 - top-right,
/// 2 - bottom-left, 3 - bottom-right.</param>
public void ApplyQuadrilateralUnwarp(string sourceFile, string resultFile, System.Drawing.PointF[] documentImagePoints)
{
    using (Vintasoft.Imaging.VintasoftImage image =
        new Vintasoft.Imaging.VintasoftImage(sourceFile))
    {
        // create the perspective correction command
        Vintasoft.Imaging.ImageProcessing.Transforms.QuadrilateralWarpCommand command =
            new Vintasoft.Imaging.ImageProcessing.Transforms.QuadrilateralWarpCommand();

        // specify that command must use invert transform (command must work as unwarp command)
        command.IsInverseTransform = true;

        // set the corner points of document image
        command.DestinationPoints = documentImagePoints;

        // apply perspective correction to a document image
        command.ExecuteInPlace(image);

        // save the result image to a file
        image.Save(resultFile);
    }
}