close
   1: public Mat ConvertBitmapToMat(Bitmap bmp)
   2: {
   3:     // Lock the bitmap's bits.  
   4:     Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
   5:  
   6:     System.Drawing.Imaging.BitmapData bmpData =
   7:         bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
   8:             bmp.PixelFormat);
   9:  
  10:     // data = scan0 is a pointer to our memory block.
  11:     IntPtr data = bmpData.Scan0;
  12:  
  13:     // step = stride = amount of bytes for a single line of the image
  14:     int step = bmpData.Stride;
  15:  
  16:     // So you can try to get you Mat instance like this:
  17:     Mat mat = new Mat(bmp.Height, bmp.Width, Emgu.CV.CvEnum.DepthType.Cv32F, 4, data, step);
  18:  
  19:     // Unlock the bits.
  20:     bmp.UnlockBits(bmpData);
  21:  
  22:     return mat;
  23: }

 

利用Bitmap讀入一張圖片, 並傳給Image<Bgr, Byte>影像格式

   1: using (OpenFileDialog ofd = new OpenFileDialog())
   2: {
   3:     if (ofd.ShowDialog() == DialogResult.OK)
   4:     {
   5:           Bitmap bmp = new Bitmap(ofd.FileName);
   6:           System.Drawing.Imaging.BitmapData bd;
   7:           bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
   8:                                        System.Drawing.Imaging.ImageLockMode.ReadWrite, 
   9:                                        System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  10:           Image<Bgr, Byte> inputImage = new Image<Bgr, Byte>(bd.Width, bd.Height, bd.Stride, bd.Scan0);
  11:           pictureBox1.Image = inputImage.ToBitmap();
  12:           bmp.UnlockBits(bd);
  13:      }
  14: } 

 

參考:

1.  EmguCV - C# / Convert Bitmap Object to Mat Object

2.  EmguCV: How to Load a Image?

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(3) 人氣()