幾種常見方式:

1. 指標方式:IntPtr

using (OpenFileDialog ofd = new OpenFileDialog())
{
     if (ofd.ShowDialog() == DialogResult.OK)
     {
         IntPtr inputImage = CvInvoke.cvLoadImage(ofd.FileName, LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR);
         CvInvoke.cvShowImage("IntPtr", inputImage);
     }
}

image

=======================================================================

2. EmguCV 影像格式 Image<Bgr, Byte>: a wrapper to IplImage of OpenCV

using (OpenFileDialog ofd = new OpenFileDialog())
{
     if (ofd.ShowDialog() == DialogResult.OK)
     {
         Image<Bgr, Byte> inputImage = new Image<Bgr, byte>(ofd.FileName);
         CvInvoke.cvShowImage("Image<Bgr, Byte>", inputImage);
     }
}

image

=======================================================================

3. EmguCV 影像格式 Image<Gray, Byte>: 宣告一個EmguCV灰階影像格式

 

如果輸入是一張彩色照片, 但是卻用一塊灰階的EmguCV資料型態去接收會發生甚麼事情呢?

using (OpenFileDialog ofd = new OpenFileDialog())
{
     if (ofd.ShowDialog() == DialogResult.OK)
     {
         Image<Gray, Byte> inputImage = new Image<Gray, byte>(ofd.FileName);
         CvInvoke.cvShowImage("Image<Gray, Byte>", inputImage);
     }
}

內建判斷, 直接自動將輸入彩色的照片轉成灰階, 酷吧!!!

image

=======================================================================

4. 輸入PictureBox 讀入影像, 將PictureBox.Image轉型Bitmpap丟給Image<Bgr, Byte>

語法: public Image(Bitmap bmp)

image

using (OpenFileDialog ofd = new OpenFileDialog())
         {
             if (ofd.ShowDialog() == DialogResult.OK)
             {
                 pictureBox1.Load(ofd.FileName);
                 Image<Bgr, Byte> inputImage = new Image<Bgr, Byte>((Bitmap)(pictureBox1.Image));
                 CvInvoke.cvShowImage("Image<Bgr, Byte>", inputImage);
             }
         }

=======================================================================

5. 開啟一張空的影像(黑底), 大小為640X480

語法: Image(Size size)

Image<Gray, Byte> inputImage = new Image<Gray, byte>(new Size(640,480));
pictureBox1.Image = inputImage.ToBitmap();

image

=======================================================================

6. 自己定義一塊影像大小和像素值

語法:Image(int width, int height, TColor value)

image

Image<Bgr, Byte> inputImage = new Image<Bgr, byte>(640, 480, new Bgr(255,0,255));
pictureBox1.Image = inputImage.ToBitmap();

=======================================================================

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

語法:Image(int width, int height, int stride, IntPtr scan0)

image

using (OpenFileDialog ofd = new OpenFileDialog())
             {
                 if (ofd.ShowDialog() == DialogResult.OK)
                 {
                     Bitmap bmp = new Bitmap(ofd.FileName);
                     System.Drawing.Imaging.BitmapData bd;
                     bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                                       System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                       System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                     Image<Bgr, Byte> inputImage = new Image<Bgr, Byte>(bd.Width, bd.Height, bd.Stride, bd.Scan0);
                     pictureBox1.Image = inputImage.ToBitmap();
                     bmp.UnlockBits(bd);
                 }
             } 

 

範例程式: EmguVS2008LoadImage

延伸閱讀:

Emgu CV: 基礎影像容器Mat

Emgu CV: Mask遮罩應用

Emgu CV: 影像形態學應用

Emgu CV: 如何有效取得像素值? 

參考資料:

  1. How to convert a Bitmap Image to IntPtr in C#?

  2. How to convert Image type to Image<Bgr, Byte>?

  3. convert image between Iplimage, emgu image, and bitmap.

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

    天天向上

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