Emgu CV (Open CV) ライブラリで VintaSoft Imaging .NET SDK を使用する

ブログ カテゴリ: イメージング.NET

2022/02/25

VintaSoft Imaging .NET SDK画像を操作する機能を提供します。SDK を使用すると、画像の作成、読み込み、処理、印刷、保存が可能です。

SDK は、画像処理用の 110 を超えるコマンドを提供します。これにより、次の操作が可能になります。

SDK は Vintasoft.Imaging.VintasoftImage クラスを使用して画像を操作します。Vintasoft.Imaging.VintasoftImage.OpenPixelManipulator メソッドを使用すると、画像データをロックし、Vintasoft.Imaging.PixelManipulator クラスを使用して画像データにアクセスできます。Vintasoft.Imaging.PixelManipulator.Scan0 プロパティを使用すると、アンマネージ メモリ内の画像データの先頭へのポインタを取得できます。アンマネージ メモリ内の画像データの先頭へのポインタは、アンマネージ メモリに格納されている画像を操作できる他の .NET ライブラリで使用できます。たとえば、SDK 内で作成された画像は、Open CV 画像処理ライブラリのクロスプラットフォーム .NET ラッパーである Emgu CV ライブラリを使用して処理できます。

以下は、VintaSoft Imaging .NET SDK を使用して画像を作成し (Vintasoft.Imaging.VintasoftImage オブジェクトを作成)、Open CV ライブラリを使用して画像を処理し、VintaSoft Imaging .NET SDK を使用して処理済みの画像を TIFF ファイルに保存する方法を示す C# コードです。
/// <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");
    }
}