'EmguCV C# ApplyColorMap Conversions

how do I correctly convert BitmapSource to Mat (to use it as input for the "ApplyColorMap" function)?

I currently use this code:

private static Mat ToMat(BitmapSource source)
        {
            Mat result = new Mat();
            result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 3);
            source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step * result.Rows, result.Step); 
            return result;
        }

The EmguCV method gets called like this:

Mat result = new Mat();
Mat input = ToMat(depthImageAzure.CreateBitmapSource()); //depthImageAzure image has format Depth16
Emgu.CV.CvInvoke.ApplyColorMap(returnedMatFromToMat, result, Emgu.CV.CvEnum.ColorMapType.Jet);

Result with ColorMap

The original image I used to create the BitmapSource in the beginning has type "Depth16".

Is there a mistake I overlooked?

Additional Info: Its a WPF application, Target Framework net6.0-windows, Emgu.CV Version 4.5.5.4823 (Nuget-Package)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source