使用VintaSoft基于 Emgu CV (Open CV) 库的 Imaging .NET SDK

博客类别:成像.NET

2022/02/25

VintaSoft Imaging .NET SDK 提供图像处理功能。该 SDK 允许创建、加载、处理、打印和保存图像。

该 SDK 提供超过 110 个图像处理命令,可用于:

SDK 使用 Vintasoft.Imaging.VintasoftImage 类来处理图像。Vintasoft.Imaging.VintasoftImage.OpenPixelManipulator 方法允许锁定图像数据,并使用 Vintasoft.Imaging.VintasoftImage.OpenPixelManipulator 方法访问图像数据。 Vintasoft.Imaging.PixelManipulator 类。Vintasoft.Imaging.PixelManipulator.Scan0 属性允许获取指向非托管内存中图像数据起始位置的指针。任何其他能够处理存储在非托管内存中的图像的 .NET 库都可以使用此指针。例如,可以使用 Emgu CV 库(OpenCV 图像处理库的跨平台 .NET 封装器)处理在 SDK 中创建的图像。

以下是 C# 代码,演示了如何使用 VintaSoft Imaging .NET SDK 创建图像(创建一个 Vintasoft.Imaging.VintasoftImage 对象),使用 OpenCV 库处理图像,并使用 VintaSoft Imaging .NET SDK 将处理后的图像保存为 TIFF 文件:
/// <summary>
/// Loads image from PNG file using VintaSoft Imaging .NET SDK,
/// inverts loaded image using Emgu CV (Open CV) library,
/// saves inverted image to a new PNG file using VintaSoft Imaging .NET SDK.
/// </summary>
static public void InvertVintasoftImageUsingEmguCV()
{
    // create VintasoftImage from PNG file
    using (Vintasoft.Imaging.VintasoftImage vintasoftImage = new Vintasoft.Imaging.VintasoftImage("source.png"))
    {
        // open pixel manipulator
        Vintasoft.Imaging.PixelManipulator pixelManipulator = vintasoftImage.OpenPixelManipulator();

        // create rectangle that defines region of interest on image
        System.Drawing.Rectangle imageROI = new System.Drawing.Rectangle(0, 0, vintasoftImage.Width, vintasoftImage.Height);
        // lock pixels of image
        pixelManipulator.LockPixels(imageROI, Vintasoft.Imaging.BitmapLockMode.ReadWrite);

        // create Emgu CV image from pointer to unmanaged memory
        using (Emgu.CV.Image emguCvImage =
            new Emgu.CV.Image(
                vintasoftImage.Width,
                vintasoftImage.Height,
                pixelManipulator.Stride,
                pixelManipulator.Scan0))
        {
            // invert image using Open CV and get new result image
            using (Emgu.CV.Image emguCvImage2 = emguCvImage.Not())
            {
                // copy inverted image back to the source image
                // (copy image data from unmanaged memory of emguCvImage2 object to unmanaged data of vintasoftImage/emguCvImage object)
                emguCvImage2.CopyTo(emguCvImage);
            }
        }

        // close pixel manipulator and specify that image data is changed
        vintasoftImage.ClosePixelManipulator(true);

        // save inverted image to new PNG file
        vintasoftImage.Save("result.png");
    }
}