public static Mat ToMat(BitmapSource source) { if (source.Format == PixelFormats.Bgra32) { Mat result = new Mat(); result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 4); source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step*result.Rows, result.Step); return result; } else if (source.Format == PixelFormats.Bgr24) { 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; } else { throw new Exception(String.Format("Convertion from BitmapSource of format {0} is not supported.", source.Format)); } }
public static Mat ToMat(this BitmapSource source) { if (source.Format == PixelFormats.Bgra32) { Mat result = new Mat(); result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 4); source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step * result.Rows, result.Step); return(result); } else if (source.Format == PixelFormats.Bgr24) { 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); } else { throw new Exception(String.Format("Convertion from BitmapSource of format {0} is not supported.", source.Format)); } }