Correction de la distorsion de perspective d'une image de document en .NET

Catégorie du blog: Imagerie.NET

08.12.2023

De nos jours, il est très fréquent de photographier des documents avec l'appareil photo d'un téléphone. Or, il est souvent difficile de positionner l'appareil photo de manière idéale au-dessus du document photographié. Par conséquent, presque toutes les images capturées présentent des distorsions de perspective.

Voici un exemple d'images de documents pivotées, capturées par l'appareil photo d'un smartphone moderne:
Document image that is captured by phone camera
Document image that is captured by phone camera


Presque tous les systèmes destinés à la gestion de documents électroniques nécessitent des images non pivotées et non déformées. Il est donc nécessaire de corriger (redresser) les images pivotées ou déformées.
VintaSoft Document Cleanup .NET Plug-in 7.3 (VintaSoft Imaging .NET SDK 12.3) a été ajouté avec un Commande destinée à la correction des distorsions de perspective dans les images de documents.

DocumentPerspectiveCorrectionCommand peut être utilisé pour résoudre les problèmes suivants:

Remarque: DocumentPerspectiveCorrectionCommand donne un bon résultat si le contour de l’image du document présente un contraste acceptable, c’est-à-dire si le contour du document est facilement visible à l’œil nu.

Voici un exemple d’image de document corrigée qui a été traitée par DocumentPerspectiveCorrectionCommand:
Corrected document image that was processed by DocumentPerspectiveCorrectionCommand
Corrected document image that was processed by DocumentPerspectiveCorrectionCommand



Voici un exemple de code C# montrant comment corriger une distorsion de perspective d'une image de document:
/// <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);
    }
}


Si le rapport d'aspect de l'image du document est initialement connu, alors le rapport d'aspect doit être spécifié à l'aide de la propriété DocumentPerspectiveCorrectionCommand.DocumentAspectRatio.
Voici un exemple de code C# montrant comment corriger une distorsion de perspective pour une image de document A4:
/// <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 utilise QuadrilateralWarpCommand pour effectuer la transformation de correction de perspective. Si vous connaissez les points d'angle de l'image déformée, vous pouvez utiliser QuadrilateralWarpCommand pour effectuer une transformation inverse de perspective.
Voici un exemple de code C# montrant comment corriger une distorsion de perspective d'une image de document, si les points d'angle de l'image déformée sont connus:
/// <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);
    }
}