.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);
    }
}


DocumentPerspectiveCorrectionCommandは、QuadrilateralWarpCommandを使用して、パースペクティブ補正の変換を実行します。歪んだ画像のコーナーポイントがわかっている場合は、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);
    }
}