Perspektivische Verzerrungen von Dokumentenbildern in .NET korrigieren

Blogkategorie: Bildgebung.NET

08.12.2023

Dokumente werden heutzutage sehr oft mit der Handykamera fotografiert. Es ist offensichtlich schwierig, die Kamera optimal über dem Dokument zu positionieren, weshalb fast alle aufgenommenen Bilder perspektivische Verzerrungen aufweisen.

Hier ist ein Beispiel für gedrehte Dokumentenbilder, die mit der Kamera eines modernen Smartphones aufgenommen wurden:
Document image that is captured by phone camera
Document image that is captured by phone camera


Fast alle Systeme zur Bearbeitung elektronischer Dokumente benötigen dreh- und verzerrungsfreie Bilder. Daher ist es notwendig, gedrehte oder verzerrte Bilder zu korrigieren (zu begradigen).
VintaSoft Document Cleanup .NET Plug-in 7.3 (VintaSoft Imaging .NET SDK 12.3) wurde mit einem Befehl zur Korrektur von perspektivischen Verzerrungen in Dokumentenbildern hinzugefügt.

DocumentPerspectiveCorrectionCommand kann zur Lösung der folgenden Probleme verwendet werden:

Hinweis: DocumentPerspectiveCorrectionCommand liefert ein gutes Ergebnis, wenn der Dokumentbildrand einen akzeptablen Kontrast aufweist, d. h. der Dokumentrand gut sichtbar ist. für das menschliche Auge.

Hier ist ein Beispiel für ein korrigiertes Dokumentenbild, das mit DocumentPerspectiveCorrectionCommand verarbeitet wurde:
Corrected document image that was processed by DocumentPerspectiveCorrectionCommand
Corrected document image that was processed by DocumentPerspectiveCorrectionCommand



Hier ist C#-Code, der zeigt, wie eine perspektivische Verzerrung eines Dokumentenbildes korrigiert wird:
/// <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);
    }
}


Wenn das Seitenverhältnis des Dokumentenbildes bereits bekannt ist, muss es über die Eigenschaft DocumentPerspectiveCorrectionCommand.DocumentAspectRatio angegeben werden.
Hier ist C#-Code, der zeigt, wie eine perspektivische Verzerrung für ein A4-Dokumentenbild korrigiert wird:
/// <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 verwendet QuadrilateralWarpCommand, um die Transformation zur Perspektivkorrektur durchzuführen. Wenn Sie die Eckpunkte des verzerrten Bildes kennen, können Sie QuadrilateralWarpCommand verwenden, um eine perspektivische Invertierung durchzuführen.
Hier ist C#-Code, der zeigt, wie eine perspektivische Verzerrung eines Dokumentbildes korrigiert wird, wenn die Eckpunkte des verzerrten Bildes bekannt sind:
/// <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);
    }
}