Skip to content

How to convert OpenCVSharp.Mat to?

Takuya Takeuchi edited this page Jun 1, 2019 · 2 revisions

To DlibDotNet.Matrix<T>

using (var mat = Cv2.ImRead("Lenna.png", ImreadModes.AnyColor))
{
    var array = new byte[mat.Width * mat.Height * mat.ElemSize()];
    Marshal.Copy(mat.Data, array, 0, array.Length);

    // TODO: support BGR image
    using (var image = new Matrix<RgbPixel>(array, mat.Height, mat.Width, mat.ElemSize()))
    {
        // something to do
    }
}

https://github.com/takuya-takeuchi/DlibDotNet/tree/master/examples/3rdparty/OpenCVSharp/MatToMatrix

To DlibDotNet.Array2D<T>

using (var mat = Cv2.ImRead("Lenna.png", ImreadModes.AnyColor))
{
    var array = new byte[mat.Width * mat.Height * mat.ElemSize()];
    Marshal.Copy(mat.Data, array, 0, array.Length);

    using (var image = Dlib.LoadImageData<RgbPixel>(array, (uint)mat.Height, (uint)mat.Width, (uint)(mat.Width * mat.ElemSize())))
    {
        // something to do
    }
}

https://github.com/takuya-takeuchi/DlibDotNet/tree/master/examples/3rdparty/OpenCVSharp/MatToArray2D