簡體   English   中英

將位圖圖像(Kinect v2)轉換為Emgu Image時,“不支持URI格式”

[英]“URI formats are not supported” when converting Bitmap image (Kinect v2) to Emgu Image

我正在一個項目中使用Emgu庫(2.4.10.1940)使用Kinect相機2(SDK v2)。

首先,我將Kinect ColorFrame轉換為BitmapSource ,然后從BitmapSourceDrawing.Bitmap 當我嘗試從Drawing.Bitmap轉換為Image<Bgr, Byte> ,出現“ mscorlib.dll中發生類型'System.ArgumentException'的第一次機會異常。附加信息:不支持URI格式”。

有誰有主意,或者有人可以提示我如何用另一種方法來做?

在下面,您將找到我使用的代碼。

  public MainWindow() { InitializeComponent(); kinectSensor = KinectSensor.GetDefault(); if (kinectSensor == null) return; FrameDescription colorFrameDescription = kinectSensor.ColorFrameSource.FrameDescription; colorReader = kinectSensor.ColorFrameSource.OpenReader(); colorPixels = new byte[colorFrameDescription.Width * colorFrameDescription.Height * BytePerPixel]; colorBitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null); kinectSensor.Open(); colorReader.FrameArrived += colorReader_FrameArrived; kinectSensor.IsAvailableChanged += kinectSensor_IsAvailableChanged; StatusText = kinectSensor.IsAvailable ? "Running" : "Kinect sensor not available"; } void colorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame == null) return; FrameDescription colorFrameDesc = colorFrame.FrameDescription; // Check if the pixelWidth and pixelHeight is right if ((colorFrameDesc.Width == colorBitmap.PixelWidth) && (colorFrameDesc.Height == colorBitmap.PixelHeight)) { // Check if the image format is right. if (colorFrame.RawColorImageFormat == ColorImageFormat.Bgra) colorFrame.CopyRawFrameDataToArray(this.colorPixels); else colorFrame.CopyConvertedFrameDataToArray(this.colorPixels, ColorImageFormat.Bgra); // Write pixels to BitmapSource format colorBitmap.WritePixels(new Int32Rect(0, 0, colorFrameDesc.Width, colorFrameDesc.Height), colorPixels, colorFrameDesc.Width * BytePerPixel, 0); // Convert to Drawing.Bitmap image System.Drawing.Bitmap bmap = BitmapImage2Bitmap(colorBitmap); // Convert to Emgu image (This is where I get my error). Emgu.CV.Image<Bgr, byte> imageFrame = new Image<Bgr,byte>(bmap); } } } private System.Drawing.Bitmap BitmapImage2Bitmap(BitmapSource bitmapImage) { using (MemoryStream outStream = new MemoryStream()) { BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(bitmapImage)); enc.Save(outStream); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream); return new System.Drawing.Bitmap(bitmap); } } 

嗨,我發現了問題並解決了。 首先,我忘記將x86和x64文件夾從Emgu \\ bin復制到Visual Studio目錄的Debug文件夾中。 此外,將Media.BitmapSource轉換為Emgu.CV.Image也不是最好的主意,因此在閱讀一本書(James Emgu.CV.Image使用Microsoft SDK開始Kinect編程)之后,我可以將Drawing.Bitmap轉換為Emgu.CV.Image

  private void InitializeKinect() { KinectSensor Sensor = KinectSensor.GetDefault(); FrameDescription frameDescription = Sensor.ColorFrameSource.FrameDescription; ColorFrameReader FrameReader = Sensor.ColorFrameSource.OpenReader(); FrameReader.FrameArrived += FrameReader_FrameArrived; } void FrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame frame = e.FrameReference.AcquireFrame()) { if (frame == null) return; var width = frame.FrameDescription.Width; var heigth = frame.FrameDescription.Height; var data = new byte[width * heigth * System.Windows.Media.PixelFormats.Bgra32.BitsPerPixel / 8]; frame.CopyConvertedFrameDataToArray(data, ColorImageFormat.Bgra); var bitmap = new System.Drawing.Bitmap(width, height, PixelFormat.Format32bppRgb); var bitmapData = bitmap.LockBits( new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat); Marshal.Copy(data, 0, bitmapData.Scan0, data.Length); bitmap.UnlockBits(bitmapData); Emgu.CV.Image<Bgr, Byte> imageFrame = new Image<Bgr, Byte>(bitmap); } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM