Emgucv3.1快速配置教學_開發環境 2015_Emgucv_快速入門
Emgucv3.1快速配置教學
開發環境
2015
Step1.新增WindowsForm專案
Step2.配置emgucv
Tools
-->NuGet Package Manager
-->Manage NuGet Packages for Solution
會自動幫你添加好相關的dll檔案
而且就連以前我們要自己手動去添加emgucv
提供的四個組件都已經幫我們弄好了喔
四個組件依序是
ImageBox、MatrixBox、HistogramBox、PanAndZoomPictureBox
在30秒內我們完成了配置
是不是很方便不用再捨麼系統環境變數一堆拉哩拉紮的東西
好緊接著我們要先來顯示一張靜態圖片
Unit1.靜態圖片的處理
回到WindowsForm 可視化視窗介面布局 地方
引用三個特定命名空間
圖檔開啟
【程式技巧_按下按鈕將圖片A傳遞至圖片B】
介面部分
Step1.改到外面宣告
Step2.Emgucv Clone方法
Unit2.常見的影像處裡
2-1.灰度(單通道)
2-2.BGR三通道分割及合併
寫法1.直觀寫法
private void button2_Click(object sender, EventArgs e)
{
//寫法1.直觀寫法
Image<Bgr, Byte> imgB, imgG, imgR;
imgB = img1.Clone();
imgG = img1.Clone();
imgR = img1.Clone();
for (int i = 0; i < imgB.Cols; i++)
for (int j = 0; j < imgB.Rows; j++)
{
imgB.Data[j, i, 0] = 255; //B
//imgB.Data[j, i, 1] = 0; //G
//imgB.Data[j, i, 2] = 0; //R
}
for (int i = 0; i < imgG.Cols; i++)
for (int j = 0; j < imgG.Rows; j++)
{
//imgG.Data[j, i, 0] = 0; //B
imgG.Data[j, i, 1] = 255;//G
//imgG.Data[j, i, 2] = 0; //R
}
for (int i = 0; i < imgR.Cols; i++)
for (int j = 0; j < imgR.Rows; j++)
{
//imgR.Data[j, i, 0] = 0; //B
//imgR.Data[j, i, 1] = 0; //G
imgR.Data[j, i, 2] =
255; //R
}
pictureBox2.Image = imgB.ToBitmap();
pictureBox3.Image = imgG.ToBitmap();
pictureBox4.Image = imgR.ToBitmap();
}
寫法2.用Emgucv函數
Step1.用CvInvoke.Split Method
通道分割
Split()可以用上面提到Mat陣列,
也可以用Image<Gray, byte> 陣列去接收
Step2.
通道合併
1. 利用img.Split()先丟到channels
2. 將不感興趣的channel設定為零 【感興趣的channel : COI】
3. 宣告一個VectorofMat資料型態, 可以輸入三個Gray level的資料
經過修改
private void button2_Click(object sender, EventArgs e)
{
Image<Bgr, Byte> imgBgr;
imgBgr = img1.Clone();
Image<Gray, byte>[] imageChannels1 = imgBgr.Split();
//imageChannels1[0] Blue,imageChannels1[1] Green,imageChannels1[2] Red
/* remove Green and Red channels, ie. only
Blue channel is saved */
int[] zeroCHs1 = new int[2] { 1, 2 };
for (int i = 0; i < 2; i++)
imageChannels1[zeroCHs1[i]].SetZero();
VectorOfMat c1 = new VectorOfMat();
c1.Push(imageChannels1[0]);
c1.Push(imageChannels1[1]);
c1.Push(imageChannels1[2]);
Image<Bgr, Byte> imgB = new Image<Bgr, byte>(imgBgr.Size);
CvInvoke.Merge(c1, imgB);
pictureBox2.Image = imgB.ToBitmap();//B
Image<Gray, byte>[] imageChannels2 = imgBgr.Split();
//imageChannels1[0] Blue,imageChannels1[1] Green,imageChannels1[2] Red
/*remove Blue and Red channels, ie. only
Green channel is saved*/
int[] zeroCHs2 = new int[2] { 0, 2 };
for (int i = 0; i < 2; i++)
imageChannels2[zeroCHs2[i]].SetZero();
VectorOfMat c2 = new VectorOfMat();
c2.Push(imageChannels2[0]);
c2.Push(imageChannels2[1]);
c2.Push(imageChannels2[2]);
Image<Bgr, Byte> imgG = new Image<Bgr, byte>(imgBgr.Size);
CvInvoke.Merge(c2, imgG);
pictureBox3.Image = imgG.ToBitmap();//G
Image<Gray, byte>[] imageChannels3 = imgBgr.Split();
//imageChannels1[0] Blue,imageChannels1[1] Green,imageChannels1[2] Red
/*remove Blue and Green channels, ie. only
Red channel is saved*/
int[] zeroCHs3 = new int[2] { 0, 1 };
for (int i = 0; i < 2; i++)
imageChannels3[zeroCHs3[i]].SetZero();
VectorOfMat c3 = new VectorOfMat();
c3.Push(imageChannels3[0]);
c3.Push(imageChannels3[1]);
c3.Push(imageChannels3[2]);
Image<Bgr, Byte> imgR = new Image<Bgr, byte>(imgBgr.Size);
CvInvoke.Merge(c3, imgR);
pictureBox4.Image = imgR.ToBitmap();//R
}
2-3.& 2-4. Canny_高斯模糊
private void button4_Click(object sender, EventArgs e) //Smooth
{
Image<Bgr, Byte> imgBgr;
imgBgr = img1.Clone();
/*convolution(捲積)*/
//Image<Bgr, Byte> blur =
imgBgr.SmoothBlur(5,5); // 效果不太明顯
//透過一個5*5的kernek矩陣來計算pixel的值以也就是鄰近25個點的平均值
//每個鄰點都需乘上所對應的權重值然後再加總起來!!
//pictureBox6.Image = blur.ToBitmap();
/*高斯模糊*/
Image<Bgr, Byte> gaussian = imgBgr.SmoothGaussian(7);
pictureBox6.Image = gaussian.ToBitmap();
}
private void button5_Click(object sender, EventArgs e)
{
Image<Bgr, Byte> imgBgr;
imgBgr = img1.Clone();
Image<Gray, byte> canny = imgBgr.Canny(120,180);
pictureBox7.Image = canny.ToBitmap();
}
留言
張貼留言