2017-01-19 4 views
1

필자는 WritableBitmap을 가지고 있으며 EmguCV/OpenCV Mat으로 변환하고 싶습니다. 그 일을 어떻게 하죠? 코드 온라인에서 몇 가지 연쇄 솔루션 (WritableBitmap -> 비트 맵 -> 맵)을 시도했지만 작동하는 것을 찾지 못했습니다. 감사!어떻게 C# WritableBitmap을 EmguCV/OpenCV Mat으로 변환합니까?

+0

당신이 볼 [링크] (http://stackoverflow.com/a/37383502/2050745) 및 노력을? –

+0

@NejcGalof 네, 그 대답을 보았습니다. 그러나 C++이 아니고 C#이 아니기 때문에 작동하지 않습니다. –

답변

0

나는이 꽤 잘 작동 찾을 :

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)); 
     } 
    } 

더그

관련 문제