Փաստաթղթի պատկերի հեռանկարային աղավաղման ուղղում .NET-ում

Բլոգի կատեգորիա՝ Պատկերացում.NET

08.12.2023

Ներկայումս շատ հաճախ փաստաթղթի պատկերը լուսանկարվում է հեռախոսի տեսախցիկով։ Ակնհայտ է, որ բավականին դժվար է տեսախցիկը իդեալականորեն տեղադրել լուսանկարված փաստաթղթի վերևում, ինչի արդյունքում գրեթե բոլոր լուսանկարները որոշակի հեռանկարային աղավաղումներ ունեն։

Ահա ժամանակակից սմարթֆոնի տեսախցիկով լուսանկարված պտտված փաստաթղթերի պատկերների օրինակ՝
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 հատկության միջոցով:
Ահա C# կոդը, որը ցույց է տալիս, թե ինչպես շտկել 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-ը օգտագործում է 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);
    }
}