我有一个WritableBitmap,我想把它转换成EmguCV/OpenCV Mat。我该怎么做?我尝试过几种在线代码的链式解决方案(WritableBitmap -> Bitmap -> Map),但没有发现任何有效的解决方案。谢谢!
发布于 2017-01-31 21:51:25
我发现这个效果很好:
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 if (source.Format == PixelFormats.Pbgra32)
{
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
{
throw new Exception(String.Format("Conversion from BitmapSource of format {0} is not supported.", source.Format));
}
}道格
https://stackoverflow.com/questions/41735425
复制相似问题