Correct a perspective distortion of document image in .NET

Blog category: Imaging.NET

December 8, 2023

Currently very often the document image is being photographed by the phone camera. Evidently it is rather difficult to locate the camera in an ideal way above the photographed document, as a result almost all captured images have some distortions of perspective.

Here is an example of rotated document images, which are captured by camera of modern smartphone:
Document image that is captured by phone camera
Document image that is captured by phone camera


Almost all systems intended for work with electronic documents require images, which are not rotated or distorted. So there arises the task to correct (straighten) the images being rotated or distorted.
VintaSoft Document Cleanup .NET Plug-in 7.3 (VintaSoft Imaging .NET SDK 12.3) was added with a command intended for correction of perspective distortions in document images.

DocumentPerspectiveCorrectionCommand can be used to solve the following problems:

Note: DocumentPerspectiveCorrectionCommand returns a good result if the document image border has acceptable contrast, i.e. the document border is easily visible to the human eye.

Here is an example of corrected document image that was processed by DocumentPerspectiveCorrectionCommand:
Corrected document image that was processed by DocumentPerspectiveCorrectionCommand
Corrected document image that was processed by DocumentPerspectiveCorrectionCommand



Here is C# code that demonstrates how to correct a perspective distortion of document image:
/// <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);
    }
}


If the aspect ratio of document image is initially known, then the aspect ratio must be specified using DocumentPerspectiveCorrectionCommand.DocumentAspectRatio property.
Here is C# code that demonstrates how to correct a perspective distortion for an A4 document image:
/// <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);
    }
}


DocumentPerspectiveCorrectionCommand uses QuadrilateralWarpCommand to perform the transformation of perspective correction. If you know corner points of distorted image, then you can use QuadrilateralWarpCommand to perform an inverse transformation of perspective.
Here is C# code that demonstrates how to correct a perspective distortion of document image, if corner points of distorted image are known:
/// <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);
    }
}