Utilizar VintaSoft Imaging .NET SDK com biblioteca Emgu CV (Open CV)

Categoria do blog: Imagens.NET

25.02.2022

O VintaSoft Imaging .NET SDK fornece a funcionalidade para trabalhar com imagens. O SDK permite criar, carregar, processar, imprimir e salvar imagens.

O SDK oferece mais de 110 comandos para processamento de imagens, que permitem:

O SDK usa a classe Vintasoft.Imaging.VintasoftImage para trabalhar com uma imagem. O método Vintasoft.Imaging.VintasoftImage.OpenPixelManipulator permite bloquear os dados da imagem e obter acesso a esses dados usando a classe Vintasoft.Imaging.PixelManipulator. A propriedade Vintasoft.Imaging.PixelManipulator.Scan0 permite obter o ponteiro para o início dos dados da imagem na memória não gerenciada. Esse ponteiro pode ser usado por qualquer outra biblioteca .NET que consiga trabalhar com imagens armazenadas em memória não gerenciada. Por exemplo, uma imagem criada no SDK pode ser processada usando a biblioteca Emgu CV, um wrapper .NET multiplataforma para a biblioteca de processamento de imagens OpenCV.

Aqui está um código C# que demonstra como criar uma imagem usando o VintaSoft Imaging .NET SDK (criar um objeto Vintasoft.Imaging.VintasoftImage), processar a imagem com a biblioteca OpenCV e salvar a imagem processada em um arquivo TIFF usando o VintaSoft Imaging .NET SDK:
/// <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");
    }
}