【Emgu】一起學EmguCV(二)Image和Matrix的使用


  上一篇簡單介紹了EmguCV庫的簡單配置,並演示了Hello World程序,本篇繼續介紹關於Emgu的基本使用

1、關於Image類的使用

  Image<TColor, TDepth>用兩個參數定義:ColorDepth

TColor類型 TDepth類型
Gray Byte
Bgr (Blue Green Red) SByte
Bgra (Blue Green Red Alpha) Single (float)
Hsv (Hue Saturation Value) Double
Hls (Hue Lightness Saturation) UInt16
Lab (CIE L*a*b*) Int16
Luv (CIE L*u*v*) Int32 (int)
Xyz (CIE XYZ.Rec 709 with D65 white point)  
Ycc (YCrCb JPEG)  

  例如:創建一個8位的灰度圖像    

        //創建一張灰度圖
        Image<Gray, Byte> img1 = new Image<Gray, Byte>(480, 320);
        //創建一張藍色的圖片
        Image<Bgr, Byte> img2 = new Image<Bgr, Byte>(480, 320, new Bgr(255, 0, 0));
        //從文件創建Image
        Image<Bgr, Byte> img3 = new Image<Bgr, Byte>("MyImage.jpg");
        //從Bitmap創建Image
        Bitmap bmp = new Bitmap("MyImage.jpg");
        Image<Bgr, Byte> img4 = new Image<Bgr, Byte>(bmp);

  .Net會自動完成垃圾回收,對於比較大的圖片,我們可以使用using關鍵字在不需要的時候自動對其進行回收

        using (Image<Gray, Single> image = new Image<Gray, Single>(1000, 800))
        {
            //對image操作
        }

  獲取和設置像素顏色

  有兩種方式對圖片的像素進行直接操作

        Image<Bgr, byte> img = new Image<Bgr, byte>(480, 320, new Bgr(0, 255, 0));

        //直接通過索引訪問,速度較慢,返回TColor類型
        Bgr color = img[100, 100];
        img[100, 100] = color;

        //通過Data索引訪問,速度快
        //最后一個參數為通道數,例如Bgr圖片的 0:藍色,1:綠色,2:紅色,Gray的0:灰度,返回TDepth類型
        Byte blue = img.Data[100, 100, 0];
        Byte green = img.Data[100, 100, 1];
        Byte red = img.Data[100, 100, 2];

  Image<TColor, TDepth>還對操作運算符進行了重載( + - * / )

        Image<Bgr, byte> img1 = new Image<Bgr, byte>(480, 320, new Bgr(255, 0, 0));
        Image<Bgr, byte> img2 = new Image<Bgr, byte>(480, 320, new Bgr(0, 255, 0));

        //img3 == new Image<Bgr, byte>(480, 320, new Bgr(255, 255, 0));
        Image<Bgr, byte> img3 = img1 + img2;

  Image<TColor, TDepth>有一個Not函數,可以讓圖片反色

  Image<TColor, TDepth>還提供了轉換器,可以讓我們更方便的編寫轉換邏輯 

  Image<TColor, TDepth>還有一個 ToBitmap() 函數可以轉換為Bitmap

        Image<Bgr, byte> img1 = new Image<Bgr, byte>(@"test.jpg");
        Image<Bgr, byte> img2 = img1.Not();
        //下面轉換效果與Not()函數相同
        Image<Bgr, Byte> img3 = img1.Convert<byte>(delegate(Byte b) { return (Byte)(255 - b); });
        pictureBox1.Image = img3.ToBitmap();

 

2、關於Matrix矩陣類的使用

  Martix的使用與Image類似,這里就不闡述了

        Matrix<Single> matrix = new Matrix<Single>(480, 320);

        float f = matrix[100, 100];
        float df = matrix.Data[100, 100];

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM