由於需要使用到EmguCV,作爲初次接觸者,參照官方的wiki,記了一些筆記。原網址: http://www.emgu.com/wiki/index.php/Tutorial#C.23
一、映射
函數映射: Emgu.CV.CvInvoke
結構映射: Emgu.CV.Structure.Mxxx
數值映射: Emgu.CV.CvEnum
二、Image
emguCV3.0使用Mat類,內存大小自動分配。
1.1、使用Mat創建Image
<1>創建一個200*400的三通道圖像
Mat img = new Mat(200, 400, DepthType.Cv8U, 3);
Mat img = new Mat(img1.Size,DepthType.Cv8U, 3);
<2>創建空白Image(在調用cvInvoke的函數需使用新Image時,需要先創建空的Image)
Mat img = new Mat();
<3>從文件讀取圖像
Mat img = CvInvoke.Imread("myimage.jpg", CvEnum.LoadImageType.AnyColor);
1.2 使用 Image<,>創建Image
<1>從文件讀取
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>("MyImage.jpg");
<2>使用Image<TColor, TDepth>。但是使用該類下的方法時,必須採用相同的顏色或色深類型。
如Image<,>中的SetValue(TColor color, Image<Gray, Byte> mask)函數。
Image<Gray, Byte> image = new Image<Gray, Byte>( width, height);
<3> 創建帶背景色的Image
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(480, 320, new Bgr(255, 0, 0));
Image<Gray, Byte> img1 = new Image<Gray, Byte>(400, 300, new Gray(30));
<4>從 .Net的Bitmap類型創建
Image<Bgr, Byte> img = new Image<Bgr, Byte>(bmp); //where bmp is a Bitmap
TColor參數類型:
-
Gray
-
Bgr (Blue Green Red)
-
Bgra (Blue Green Red Alpha)
-
Hsv (Hue Saturation Value)
-
Hls (Hue Lightness Saturation)
-
Lab (CIE L*a*b*)
-
Luv (CIE L*u*v*)
-
Xyz (CIE XYZ.Rec 709 with D65 white point)
-
Ycc (YCrCb JPEG)
-
TDepth參數類型:
-
Byte
-
SByte
-
Single (float)
-
Double
-
UInt16
-
Int16
-
Int32 (int)
-
2.1.獲取Mat類型圖像的像素
Mat類型的大小自動分配,因此沒有Data屬性來直接獲取像素。
<1>使用 ToImage 方法將Mat類的圖像複製到一個Image<,>類型的圖像。
Image<Bgr, Byte> img = mat.ToImage<Bgr, Byte>();
然後使用Image<,>.Data屬性獲取圖像像素。
2.2.使用Image<,>處理像素
<1>The safe (slow) way
Bgr color = img[y, x];
img[y,x] = color;
<2>The fast way
Byte byCurrent = imageGray.Data[x, y, 0];
<3>運算符重載方式
Image<Gray, Byte> image3 = (image1 + image2 - 2.0) * 0.5;
2.3.對Image<,>的所有像素操作
<1>使用內置函數
Image<Gray, Byte> img2 = img1.Not();
img1._Not();//replace
<2>使用Convert函數,使用匿名委託類型,犧牲很少的效率,但更靈活
① Image<Gray, Byte> img3 = img1.Convert<Byte>
( delegate(Byte b) { return (Byte) (255-b); } );
② Image<Gray, Single> img4 = img1.Convert<Single>
( delegate(Byte b) { return (Single) Math.cos( b * b / 255.0); } );
2.4.在Image<,>圖像上繪製對象
使用Draw()方法繪製 fonts, lines, circles, rectangles, boxes, ellipses as well as contours
2.5Image<,>圖像類型轉換
例如有Image<Bgr, Byte> img1
Image<Gray, Single> img2 = img1.Convert<Gray, Single>();
2.6顯示圖像
<1>Emgu的ImageBox控件。直接引用圖像內存,速度快。
<2>wimForm的pictureBox控件
使用ToBitmap()方法,返回Bitmap類型
三、Matrix
//數據類型參考TDepth的類型列表
Matrix<Single> matrix = new Matrix<Single>(width, height);