close
自從開始學習EmguCV後,越來越覺得這玩意有一種相見恨晚的感覺,
這玩意比起MFC來說,真的快很多啊............,而且真的不難,
甚至在Compression上面,也有很好的表現,
比起MFC來說,這傢伙簡直完勝,
所以.......我決定下一次的Project,就用它來搞定了
這次要教的是各種圖像的基礎應用以及處理,
廢話不多說,咱們直接寫點Code吧 !
1. 創建一個圖片
private void btn_CreatePhoto_Click(object sender, EventArgs e)
{
// Create an image.
Image<Rgb, byte> image = new Image<Rgb, byte>(320, 240, new Rgb(255, 0,0 ));
imageBox1.Image = image ;
}
有沒有 !!! 就是這麼簡單。
2. 打開一張圖片
private void btn_CreatePhoto_Click(object sender, EventArgs e)
{
// Create an image.
Image image = new Image(320, 240, new Rgb(255, 0,0 ));
imageBox1.Image = image ;
}
3. 從ImageBox裡面Copy圖片
Image _picture = null;
private void btn_CopyImg_Click(object sender, EventArgs e)
{
if(_picture == null)
{
_picture = new Image(new Bitmap(imageBox1.Image.Bitmap));
imageBox1.Image = _picture;
}
else
{
imageBox1.Image = _picture;
}
}
4. 畫一些什麼,在圖片上面
private void btn_Draw_Click(object sender, EventArgs e)
{
// 創建一張白色長寬為 200 * 200的圖片
Image img = new Image(200, 200,
new Bgr(255, 255, 255));
// 創建一個矩形,左上角坐標為 (80,80)大小為(40,40)
Rectangle rect = new Rectangle(new Point(80, 80),
new Size(40, 40));
// 創建一個中心點座標為 (100,100)半徑為40的圓形
CircleF circlef = new CircleF(new PointF(100, 100), 40);
string str = "Eric";
// 創建Point類型做為字符串的左上角坐標
Point str_location = new Point(0, 30);
img.Draw(rect, new Bgr(0, 255, 0), 2);// 指定參數繪畫矩形
img.Draw(circlef,new Bgr(0,0,255),3); // 指定參數繪畫圓形
img.Draw(str,str_location,Emgu.CV.CvEnum.FontFace.HersheyComplexSmall,
1, new Bgr(255,0,0),3); //指定參數繪畫字體
imageBox1.Image = img;
}
5. Draw Polyline
private void btn_DrawPolyline_Click(object sender, EventArgs e)
{
// 創建一個隨機數產生器
Random m = new Random();
// 創建一張200*200的白色圖片
Image img = new Image(
200, 200, new Bgr(255, 255, 255));
// 創建一個三個三點數組
Point[] pt1 = new Point[3];
Point[] pt2 = new Point[3];
Point[] pt3 = new Point[3];
for (int i =0 ; i< 3 ;i++)
{
pt1[i] = new Point(m.Next(0, 200), m.Next(0, 200));
pt2[i] = new Point(m.Next(0, 200), m.Next(0, 200));
pt3[i] = new Point(m.Next(0, 200), m.Next(0, 200));
}
Point[][] pts = new Point[2][];
pts[0] = pt2;
pts[1] = pt3;
img.DrawPolyline(pt1, true, new Bgr(0, 0, 255), 2);
img.DrawPolyline(pts,true,new Bgr(255,0,0),3);
imageBox1.Image = img;
}
6. 使用Mat來Draw
補充 : Mat 是在Emgucv3.0之後才出來。
private void btn_DrawMat_Click(object sender, EventArgs e)
{
Image img = new Image(200, 200,
new Bgr(255, 255, 255));
Mat mat = img.Mat;
CvInvoke.PutText(mat, "Hello world",
new Point(0, 30), Emgu.CV.CvEnum.FontFace.HersheyComplexSmall,
1, new MCvScalar(255, 0, 0), 3);
Rectangle rect = new Rectangle(new Point(80, 80),
new Size(40,40));
CvInvoke.Rectangle(mat, rect, new MCvScalar(0, 255, 0), 2);
CvInvoke.Circle(mat, new Point(100, 100), 40,
new MCvScalar(0, 0, 255), 3);
imageBox1.Image = mat;
}
7. 圖像遍歷- 三種方式
// 第二種方式
private void btn_DrawMat_Click(object sender, EventArgs e)
{
Image img = new Image(200, 200,
new Bgr(255, 255, 255));
Mat mat = img.Mat;
CvInvoke.PutText(mat, "Hello world",
new Point(0, 30), Emgu.CV.CvEnum.FontFace.HersheyComplexSmall,
1, new MCvScalar(255, 0, 0), 3);
Rectangle rect = new Rectangle(new Point(80, 80),
new Size(40,40));
CvInvoke.Rectangle(mat, rect, new MCvScalar(0, 255, 0), 2);
CvInvoke.Circle(mat, new Point(100, 100), 40,
new MCvScalar(0, 0, 255), 3);
imageBox1.Image = mat;
}
private void btn_rec_Click(object sender, EventArgs e)
{
Image img_1 = new Image(320, 240,
new Bgr(255, 0, 0));
// 第一種方式
#if false
Byte b1 = 255;
for (int i = 20; i < 60 ; i++)
{
for (int j = 20; j < 60; j++)
{
img_1.Data[i, j, 0] = 0 ; // 第一通道的某個座標進行賦值
img_1.Data[i, j, 1] = b1; // 第二通道的某個座標進行賦值
img_1.Data[i, j, 2] = b1; // 第三通道的某個座標進行賦值
}
}
// 第二種方式
Bgr _Yellow = new Bgr(0, 255, 255);
for (int i = 120; i < 160; i++)
{
for (int j = 20 ; j < 60 ; j++)
{
img_1[i, j] = _Yellow; // 對某個位置進行顏色空間賦值
}
}
#endif
Byte b1 = 255;
byte[, ,] data = img_1.Data; //取得img數據轉成byte三維數組數據.
for (int i = 20; i < 60; i++)
{
for (int j = 100; j < 140 ;j++)
{
data[i, j, 0] = 0;
data[i, j, 1] = b1;
data[i, j, 2] = b1;
}
}
imageBox1.Image = img_1;
}
全站熱搜