Correggi una distorsione prospettica dell'immagine del documento in .NET

Categoria blog: Imaging.NET

08.12.2023

Attualmente molto spesso l'immagine del documento viene fotografata dalla fotocamera del telefono. Evidentemente è piuttosto difficile posizionare la fotocamera in modo ideale sopra il documento fotografato, di conseguenza quasi tutte le immagini acquisite presentano delle distorsioni prospettiche.

Ecco un esempio di immagini di documenti ruotate, catturate dalla fotocamera di uno smartphone moderno:
Immagine del documento catturata dalla fotocamera del telefono
Immagine del documento catturata dalla fotocamera del telefono


Quasi tutti i sistemi destinati al lavoro con documenti elettronici richiedono immagini,che non sono ruotate o distorte. Quindi, si presenta il problema di correggere (raddrizzare) le immagini ruotate o distorte.
VintaSoft Document Cleanup .NET Plug-in 7.3 (VintaSoft Imaging .NET SDK 12.3) è stato aggiunto con un comando per la correzione delle distorsioni prospettiche nelle immagini dei documenti.

DocumentPerspectiveCorrectionCommand può essere utilizzato per risolvere i seguenti problemi:

Nota: DocumentPerspectiveCorrectionCommand restituisce un buon risultato se il bordo dell'immagine del documento ha un contrasto accettabile, ovvero se il bordo del documento è facilmente visibile all'occhio umano.

Ecco un esempio di immagine di documento corretta elaborata da DocumentPerspectiveCorrectionCommand:
Immagine del documento corretta elaborata da DocumentPerspectiveCorrectionCommand
Immagine del documento corretta elaborata da DocumentPerspectiveCorrectionCommand



Ecco il codice C# che dimostra come correggere una distorsione prospettica dell'immagine del documento:
/// <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);
    }
}


Se inizialmente si conoscono le proporzioni dell'immagine del documento,quindi il rapporto d'aspetto deve essere specificato utilizzando la proprietà DocumentPerspectiveCorrectionCommand.DocumentAspectRatio.
Ecco il codice C# che mostra come correggere una distorsione prospettica per un'immagine di un documento 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 utilizza QuadrilateralWarpCommand per eseguire la trasformazione della correzione prospettica. Se conosci i punti angolari dell'immagine distorta, puoi usare QuadrilateralWarpCommand per eseguire una trasformazione inversa della prospettiva.
Ecco il codice C# che mostra come correggere una distorsione prospettica dell'immagine del documento, se sono noti i punti angolari dell'immagine distorta:
/// <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);
    }
}