2012-03-26 1 views
0

Kinect와 EmguCV를 함께 사용하고 싶습니다. 저는 Kinect에서 이미지를 가져 와서 EmguCV의 Image 객체를 만들었습니다. 난 잠시 동안 응용 프로그램을 실행하고 메모리가 제대로 해제되지 않았기 때문에 응용 프로그램이 잠시 후 충돌합니다.Kinect & EmguCV & GC

작은 코드는 Kinect에서 RGB 컬러 이미지를 가져 와서 HSV 컬러 이미지로 변환합니다. 메모리가 공개되지 않은 부분을 해결할 수 없습니다. 나는 인터넷과 일부 책에서 읽은 예제와 마찬가지로 "구조 사용"을 사용했습니다.

저는 C#에 익숙하지 않아 코드에서 잘못하고있는 것에 대해 조언을 구하고 싶습니다. 이미지 데이터를 변환하는 내 다리를 당겨야합니다. 나는 다른 간단한 Kinect + EmguCV 프로젝트를보고 싶습니다. 여러분이 추천한다면 매우 감사 할 것입니다.

미리 감사드립니다. 코드에서 주위에 거짓말을 내키지 않는 비트 맵이 많이있다

private void showHSV(Bitmap bmp) 
    { 
     Image<Bgr, byte> img = new Image<Bgr, byte>(bmp); 
     Image<Hsv, byte> imgHsv = img.Convert<Hsv, byte>(); 

     Bitmap bmp2 = imgHsv.ToBitmap(); 

     image2.Source = sourceFromBitmap(bmp2); 
    } 


    private BitmapSource sourceFromBitmap(Bitmap bmp) 
    { 
     BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
      bmp.GetHbitmap(), 
      IntPtr.Zero, 
      System.Windows.Int32Rect.Empty, 
      BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height)); 

     return bs; 
    } 

    private void ColorImageReady(object sender, ColorImageFrameReadyEventArgs e) 
    { 
     using (ColorImageFrame imageFrame = e.OpenColorImageFrame()) 
     { 
      if (imageFrame != null) 
      { 
       byte[] pixelData = new byte[imageFrame.PixelDataLength]; 
       imageFrame.CopyPixelDataTo(pixelData); 

       BitmapSource bmp = BitmapImage.Create(imageFrame.Width, imageFrame.Height, 96, 96, PixelFormats.Bgr32, null, 
        pixelData, imageFrame.Width * imageFrame.BytesPerPixel); 

       image1.Source = bmp; 

       showHSV(bitmapFromSource(bmp)); 
      } 
      else 
      { 
       // imageFrame is null because the request did not arrive in time   } 
      } 
     } 
    } 

    private System.Drawing.Bitmap bitmapFromSource(BitmapSource bitmapsource) 
    { 
     System.Drawing.Bitmap bitmap; 

     using (System.IO.MemoryStream outStream = new System.IO.MemoryStream()) 
     { 
      BitmapEncoder enc = new BmpBitmapEncoder(); 
      enc.Frames.Add(BitmapFrame.Create(bitmapsource)); 
      enc.Save(outStream); 
      bitmap = new System.Drawing.Bitmap(outStream); 
     } 
     return bitmap; 
    } 

답변

0

, 내가 그들을 지적하자 : 아마 더 누수가있다

private void showHSV(Bitmap bmp) 
{ 
    Image<Bgr, byte> img = new Image<Bgr, byte>(bmp); // Not sure what Image<,> is, but I guess it needs disposing at some point 
    Image<Hsv, byte> imgHsv = img.Convert<Hsv, byte>(); // same here 

    Bitmap bmp2 = imgHsv.ToBitmap(); // bmp2 is never disposed in your code 
    var oldBmp = image2.Source; 
    image2.Source = sourceFromBitmap(bmp2); 
    oldBmp.Dispose() // try this 
} 


private BitmapSource sourceFromBitmap(Bitmap bmp) 
{ 
    BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
     bmp.GetHbitmap(), 
     IntPtr.Zero, 
     System.Windows.Int32Rect.Empty, 
     BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height)); 

    return bs; 
} 

private void ColorImageReady(object sender, ColorImageFrameReadyEventArgs e) 
{ 
    using (ColorImageFrame imageFrame = e.OpenColorImageFrame()) 
    { 
     if (imageFrame != null) 
     { 
      byte[] pixelData = new byte[imageFrame.PixelDataLength]; 
      imageFrame.CopyPixelDataTo(pixelData); 

      BitmapSource bmp = BitmapImage.Create(imageFrame.Width, imageFrame.Height, 96, 96, PixelFormats.Bgr32, null, 
       pixelData, imageFrame.Width * imageFrame.BytesPerPixel); // bmp never disposed 

      var oldBmp = image1.Source; 
      image1.Source = bmp; 
      oldBmp.Dispose(); // try so 

      showHSV(bitmapFromSource(bmp)); // what happens inside? bmp needs to be disposed at some point... 
     } 
     else 
     { 
      // imageFrame is null because the request did not arrive in time   } 
     } 
    } 
} 

private System.Drawing.Bitmap bitmapFromSource(BitmapSource bitmapsource) 
{ 
    System.Drawing.Bitmap bitmap; 

    using (System.IO.MemoryStream outStream = new System.IO.MemoryStream()) 
    { 
     BitmapEncoder enc = new BmpBitmapEncoder(); 
     enc.Frames.Add(BitmapFrame.Create(bitmapsource)); 
     enc.Save(outStream); 
     bitmap = new System.Drawing.Bitmap(outStream); 
    } 
    return bitmap; 
} 

는 코드입니다. 기본 규칙은 모든 일회용 리소스 (비트 맵과 같은)가 만들어지면 (비트 맵을 반환하는 모든 순수 함수가 생성하는 모든 수단을 통해) 모든 작업을 완료 한 후에 처리해야합니다.

현재 비트 맵이 특정 컨트롤에 바인딩되어 있거나 다른 방식으로 사용 된 경우에는 비트 맵을 폐기해서는 안됩니다. 신선한 것으로 교체 한 다음 오래된 것을 폐기하십시오.

+0

감사합니다. 가능한 한 빨리 시도하고 의견을 말하게 될 것입니다. – honnix

+0

oldBmp.Dispose()를 컴파일 할 수 없습니다. 이미지를 처리 ​​할 수있는 또 다른 방법은 무엇입니까? – honnix

+0

oldBmp는 어떤 유형입니까? 비트 맵으로 변환해야할까요? –