本來要學學姊做的範例
因為學姊範例不知道是版本問題還是怎
不能複製貼上
所以又參考了估狗大神
我總共試做了7個
有兩個是失敗的
camera01 可攝影
camera02 失敗
使用VS內emgu套件 無法使用
camera03 可攝影 可截圖 可存檔
camera04 失敗
camera05 照片可找臉
camera06 攝影截圖可找臉
camera07 可攝影同時找臉
重要步驟:
1.使用自己安裝的emgu(emgucv-windows-universal 3.0.0.2157) (之後再試著直接用visualstudio的套件,目前使用套件emguCV是失敗的)
2.更改 專案名稱右鍵->屬性->建置->平台 改為x64
3.增加參考(前三個那個
camera01 可攝影
設計頁面:
程式碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
namespace camera01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Capture _capture;
private bool _captureInProgress;
int i = 0;
private void ProcessFrame(object sender, EventArgs arg)
{
Mat frame= _capture.QueryFrame();
//Image<Bgr, Byte> frame = _capture.QueryFrame();
}
private void button1_Click(object sender, EventArgs e)
{
#region if capture is not created, create it now
if (_capture == null)
{
try
{
_capture = new Capture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
#endregion
if (_capture != null)
{
if (_captureInProgress)
{ //stop the capture
Application.Idle -= new EventHandler(ProcessFrame);
button1.Text = "Start Capture";
timer1.Enabled = false;
}
else
{
//start the capture
button1.Text = "Stop";
Application.Idle += new EventHandler(ProcessFrame);
timer1.Enabled = true;
}
_captureInProgress = !_captureInProgress;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = _capture.QueryFrame().Bitmap;
}
}
}
camera03 可攝影 可截圖 可存檔
設計頁面:
程式碼 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Emgu.CV;
using Emgu.CV.Structure;
namespace camera03
{
public partial class Form1 : Form
{
public Capture cap;
private bool capInProgress;
int i = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox2.Left = pictureBox1.Width + 40;
}
private void ProcessFrame(object sender, EventArgs arg)
{
pictureBox1.Image = cap.QueryFrame().Bitmap;
}
private void button1_Click(object sender, EventArgs e)
{
#region if capture is not created, create it now
if (cap == null)
{
try
{
cap = new Capture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
#endregion
if (cap != null)
{
pictureBox1.Size = cap.QueryFrame().Bitmap.Size;
pictureBox2.Left = pictureBox1.Width + 40;
if (capInProgress)
{ //stop the capture
Application.Idle -= new EventHandler(ProcessFrame);
button1.Text = "開始攝像";
//timer1.Enabled = false;
}
else
{
//start the capture
button1.Text = "停止";
Application.Idle += new EventHandler(ProcessFrame);
//timer1.Enabled = true;
}
capInProgress = !capInProgress;
}
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox2.Size = pictureBox1.Size;
pictureBox2.Image= cap.QueryFrame().Bitmap;
}
private void button3_Click(object sender, EventArgs e)
{
string name = DateTime.Now.ToString("yyyy-MM-dd tt hh-mm-ss") ;
try
{
//SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Bitmap Image|*.bmp";
saveFileDialog1.FileName = name;
saveFileDialog1.Title = "儲存圖片";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();
switch (saveFileDialog1.FilterIndex)
{
case 1:
this.pictureBox1.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
}
fs.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
camera05 照片可找臉
設計頁面 :
程式碼 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//
using System.Diagnostics;
using System.IO;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.UI;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.ML;
using Emgu.CV.Cuda;
namespace camera05
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.ImageLocation = "../01.jpg";
//pictureBox1.Size = pictureBox1.Image.Size;
}
private void button1_Click(object sender, EventArgs e)
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
Run(ref pictureBox1);
}
static void Run(ref PictureBox p)
{
Mat image = new Mat("../02.jpg", LoadImageType.Color); //Read the files as an 8-bit Bgr image
long detectionTime;
List<Rectangle> faces = new List<Rectangle>();
List<Rectangle> eyes = new List<Rectangle>();
//The cuda cascade classifier doesn't seem to be able to load "haarcascade_frontalface_default.xml" file in this release
//disabling CUDA module for now
bool tryUseCuda = false;
bool tryUseOpenCL = true;
Detect(
image, "../haarcascade_frontalface_default.xml", "../haarcascade_eye.xml",
faces, eyes,
tryUseCuda,
tryUseOpenCL,
out detectionTime);
foreach (Rectangle face in faces)
CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
//foreach (Rectangle eye in eyes)
// CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
//display the image
p.Image = image.Bitmap;
}
public static void Detect(Mat image, String faceFileName, String eyeFileName, List<Rectangle> faces,
List<Rectangle> eyes, bool tryUseCuda, bool tryUseOpenCL, out long detectionTime)
{
Stopwatch watch;
#if !(IOS || NETFX_CORE)
if (tryUseCuda && CudaInvoke.HasCuda)
{
using (CudaCascadeClassifier face = new CudaCascadeClassifier(faceFileName))
using (CudaCascadeClassifier eye = new CudaCascadeClassifier(eyeFileName))
{
face.ScaleFactor = 1.1;
face.MinNeighbors = 10;
face.MinObjectSize = Size.Empty;
eye.ScaleFactor = 1.1;
eye.MinNeighbors = 10;
eye.MinObjectSize = Size.Empty;
watch = Stopwatch.StartNew();
using (CudaImage<Bgr, Byte> gpuImage = new CudaImage<Bgr, byte>(image))
using (CudaImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
using (GpuMat region = new GpuMat())
{
face.DetectMultiScale(gpuGray, region);
Rectangle[] faceRegion = face.Convert(region);
faces.AddRange(faceRegion);
foreach (Rectangle f in faceRegion)
{
using (CudaImage<Gray, Byte> faceImg = gpuGray.GetSubRect(f))
{
//For some reason a clone is required.
//Might be a bug of CudaCascadeClassifier in opencv
using (CudaImage<Gray, Byte> clone = faceImg.Clone(null))
using (GpuMat eyeRegionMat = new GpuMat())
{
eye.DetectMultiScale(clone, eyeRegionMat);
Rectangle[] eyeRegion = eye.Convert(eyeRegionMat);
foreach (Rectangle e in eyeRegion)
{
Rectangle eyeRect = e;
eyeRect.Offset(f.X, f.Y);
eyes.Add(eyeRect);
}
}
}
}
}
watch.Stop();
}
}
else
#endif
{
//Many opencl functions require opencl compatible gpu devices.
//As of opencv 3.0-alpha, opencv will crash if opencl is enable and only opencv compatible cpu device is presented
//So we need to call CvInvoke.HaveOpenCLCompatibleGpuDevice instead of CvInvoke.HaveOpenCL (which also returns true on a system that only have cpu opencl devices).
CvInvoke.UseOpenCL = tryUseOpenCL && CvInvoke.HaveOpenCLCompatibleGpuDevice;
//Read the HaarCascade objects
using (CascadeClassifier face = new CascadeClassifier(faceFileName))
using (CascadeClassifier eye = new CascadeClassifier(eyeFileName))
{
watch = Stopwatch.StartNew();
using (UMat ugray = new UMat())
{
CvInvoke.CvtColor(image, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
//normalizes brightness and increases contrast of the image
CvInvoke.EqualizeHist(ugray, ugray);
//Detect the faces from the gray scale image and store the locations as rectangle
//The first dimensional is the channel
//The second dimension is the index of the rectangle in the specific channel
Rectangle[] facesDetected = face.DetectMultiScale(
ugray,
1.1,
10,
new Size(20, 20));
faces.AddRange(facesDetected);
foreach (Rectangle f in facesDetected)
{
//Get the region of interest on the faces
using (UMat faceRegion = new UMat(ugray, f))
{
Rectangle[] eyesDetected = eye.DetectMultiScale(
faceRegion,
1.1,
10,
new Size(20, 20));
foreach (Rectangle e in eyesDetected)
{
Rectangle eyeRect = e;
eyeRect.Offset(f.X, f.Y);
eyes.Add(eyeRect);
}
}
}
}
watch.Stop();
}
}
detectionTime = watch.ElapsedMilliseconds;
}
}
}
camera06 攝影截圖可找臉
設計頁面:
程式碼 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//
using System.Diagnostics;
using System.IO;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.UI;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.ML;
using Emgu.CV.Cuda;
namespace camera06
{
public partial class camera06 : Form
{
public Capture cap;
private bool capInProgress;
Mat mat_face;
public camera06()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn攝影_Click(object sender, EventArgs e)
{
#region if capture is not created, create it now
if (cap == null)
{
try
{
cap = new Capture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
#endregion
if (cap != null)
{
if (capInProgress)
{ //stop the capture
Application.Idle -= new EventHandler(ProcessFrame);
btn攝影.Text = "開始攝像";
//timer1.Enabled = false;
}
else
{
//start the capture
btn攝影.Text = "停止";
Application.Idle += new EventHandler(ProcessFrame);
//timer1.Enabled = true;
}
capInProgress = !capInProgress;
}
}
private void ProcessFrame(object sender, EventArgs arg)
{
pictureBox1.Image = cap.QueryFrame().Bitmap;
}
private void btn截圖_Click(object sender, EventArgs e)
{
pictureBox2.Image = cap.QueryFrame().Bitmap;
mat_face= cap.QueryFrame();
}
private void btn找臉_Click(object sender, EventArgs e)
{
Run(ref mat_face);
pictureBox2.Image = mat_face.Bitmap;
}
static void Run(ref Mat mat_f)
{
Mat image = mat_f; //Read the files as an 8-bit Bgr image
long detectionTime;
List<Rectangle> faces = new List<Rectangle>();
List<Rectangle> eyes = new List<Rectangle>();
//The cuda cascade classifier doesn't seem to be able to load "haarcascade_frontalface_default.xml" file in this release
//disabling CUDA module for now
bool tryUseCuda = false;
bool tryUseOpenCL = true;
Detect(
image, "../haarcascade_frontalface_default.xml", "../haarcascade_eye.xml",
faces, eyes,
tryUseCuda,
tryUseOpenCL,
out detectionTime);
foreach (Rectangle face in faces)
CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
//foreach (Rectangle eye in eyes)
// CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
//display the image
mat_f = image;
}
public static void Detect(Mat image, String faceFileName, String eyeFileName, List<Rectangle> faces,
List<Rectangle> eyes, bool tryUseCuda, bool tryUseOpenCL, out long detectionTime)
{
Stopwatch watch;
#if !(IOS || NETFX_CORE)
if (tryUseCuda && CudaInvoke.HasCuda)
{
using (CudaCascadeClassifier face = new CudaCascadeClassifier(faceFileName))
using (CudaCascadeClassifier eye = new CudaCascadeClassifier(eyeFileName))
{
face.ScaleFactor = 1.1;
face.MinNeighbors = 10;
face.MinObjectSize = Size.Empty;
eye.ScaleFactor = 1.1;
eye.MinNeighbors = 10;
eye.MinObjectSize = Size.Empty;
watch = Stopwatch.StartNew();
using (CudaImage<Bgr, Byte> gpuImage = new CudaImage<Bgr, byte>(image))
using (CudaImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
using (GpuMat region = new GpuMat())
{
face.DetectMultiScale(gpuGray, region);
Rectangle[] faceRegion = face.Convert(region);
faces.AddRange(faceRegion);
foreach (Rectangle f in faceRegion)
{
using (CudaImage<Gray, Byte> faceImg = gpuGray.GetSubRect(f))
{
//For some reason a clone is required.
//Might be a bug of CudaCascadeClassifier in opencv
using (CudaImage<Gray, Byte> clone = faceImg.Clone(null))
using (GpuMat eyeRegionMat = new GpuMat())
{
eye.DetectMultiScale(clone, eyeRegionMat);
Rectangle[] eyeRegion = eye.Convert(eyeRegionMat);
foreach (Rectangle e in eyeRegion)
{
Rectangle eyeRect = e;
eyeRect.Offset(f.X, f.Y);
eyes.Add(eyeRect);
}
}
}
}
}
watch.Stop();
}
}
else
#endif
{
//Many opencl functions require opencl compatible gpu devices.
//As of opencv 3.0-alpha, opencv will crash if opencl is enable and only opencv compatible cpu device is presented
//So we need to call CvInvoke.HaveOpenCLCompatibleGpuDevice instead of CvInvoke.HaveOpenCL (which also returns true on a system that only have cpu opencl devices).
CvInvoke.UseOpenCL = tryUseOpenCL && CvInvoke.HaveOpenCLCompatibleGpuDevice;
//Read the HaarCascade objects
using (CascadeClassifier face = new CascadeClassifier(faceFileName))
using (CascadeClassifier eye = new CascadeClassifier(eyeFileName))
{
watch = Stopwatch.StartNew();
using (UMat ugray = new UMat())
{
CvInvoke.CvtColor(image, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
//normalizes brightness and increases contrast of the image
CvInvoke.EqualizeHist(ugray, ugray);
//Detect the faces from the gray scale image and store the locations as rectangle
//The first dimensional is the channel
//The second dimension is the index of the rectangle in the specific channel
Rectangle[] facesDetected = face.DetectMultiScale(
ugray,
1.1,
10,
new Size(20, 20));
faces.AddRange(facesDetected);
foreach (Rectangle f in facesDetected)
{
//Get the region of interest on the faces
using (UMat faceRegion = new UMat(ugray, f))
{
Rectangle[] eyesDetected = eye.DetectMultiScale(
faceRegion,
1.1,
10,
new Size(20, 20));
foreach (Rectangle e in eyesDetected)
{
Rectangle eyeRect = e;
eyeRect.Offset(f.X, f.Y);
eyes.Add(eyeRect);
}
}
}
}
watch.Stop();
}
}
detectionTime = watch.ElapsedMilliseconds;
}
}
}
camera07 可攝影同時找臉
設計頁面:
程式碼 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//
using System.Diagnostics;
using System.IO;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.UI;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.ML;
using Emgu.CV.Cuda;
namespace camera07
{
public partial class Form1 : Form
{
public Capture cap;
private bool capInProgress;
Mat _mat;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn攝像_Click(object sender, EventArgs e)
{
#region if capture is not created, create it now
if (cap == null)
{
try
{
cap = new Capture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
#endregion
if (cap != null)
{
if (capInProgress)
{ //stop the capture
Application.Idle -= new EventHandler(ProcessFrame);
btn攝像.Text = "開始攝像";
}
else
{
//start the capture
btn攝像.Text = "停止";
Application.Idle += new EventHandler(ProcessFrame);
}
capInProgress = !capInProgress;
}
}
private void ProcessFrame(object sender, EventArgs arg)
{
//pictureBox1.Image = cap.QueryFrame().Bitmap;
_mat = cap.QueryFrame();
Run(ref _mat);
pictureBox1.Image = _mat.Bitmap;
}
static void Run(ref Mat mat_f)
{
Mat image = mat_f; //Read the files as an 8-bit Bgr image
long detectionTime;
List<Rectangle> faces = new List<Rectangle>();
List<Rectangle> eyes = new List<Rectangle>();
//The cuda cascade classifier doesn't seem to be able to load "haarcascade_frontalface_default.xml" file in this release
//disabling CUDA module for now
bool tryUseCuda = false;
bool tryUseOpenCL = true;
Detect(
image, "../haarcascade_frontalface_default.xml", "../haarcascade_eye.xml",
faces, eyes,
tryUseCuda,
tryUseOpenCL,
out detectionTime);
//臉
foreach (Rectangle face in faces)
CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2);
//眼睛
//foreach (Rectangle eye in eyes)
// CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2);
//display the image
mat_f = image;
}
public static void Detect(Mat image, String faceFileName, String eyeFileName, List<Rectangle> faces,
List<Rectangle> eyes, bool tryUseCuda, bool tryUseOpenCL, out long detectionTime)
{
Stopwatch watch;
#if !(IOS || NETFX_CORE)
if (tryUseCuda && CudaInvoke.HasCuda)
{
using (CudaCascadeClassifier face = new CudaCascadeClassifier(faceFileName))
using (CudaCascadeClassifier eye = new CudaCascadeClassifier(eyeFileName))
{
face.ScaleFactor = 1.1;
face.MinNeighbors = 10;
face.MinObjectSize = Size.Empty;
eye.ScaleFactor = 1.1;
eye.MinNeighbors = 10;
eye.MinObjectSize = Size.Empty;
watch = Stopwatch.StartNew();
using (CudaImage<Bgr, Byte> gpuImage = new CudaImage<Bgr, byte>(image))
using (CudaImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())
using (GpuMat region = new GpuMat())
{
face.DetectMultiScale(gpuGray, region);
Rectangle[] faceRegion = face.Convert(region);
faces.AddRange(faceRegion);
foreach (Rectangle f in faceRegion)
{
using (CudaImage<Gray, Byte> faceImg = gpuGray.GetSubRect(f))
{
//For some reason a clone is required.
//Might be a bug of CudaCascadeClassifier in opencv
using (CudaImage<Gray, Byte> clone = faceImg.Clone(null))
using (GpuMat eyeRegionMat = new GpuMat())
{
eye.DetectMultiScale(clone, eyeRegionMat);
Rectangle[] eyeRegion = eye.Convert(eyeRegionMat);
foreach (Rectangle e in eyeRegion)
{
Rectangle eyeRect = e;
eyeRect.Offset(f.X, f.Y);
eyes.Add(eyeRect);
}
}
}
}
}
watch.Stop();
}
}
else
#endif
{
//Many opencl functions require opencl compatible gpu devices.
//As of opencv 3.0-alpha, opencv will crash if opencl is enable and only opencv compatible cpu device is presented
//So we need to call CvInvoke.HaveOpenCLCompatibleGpuDevice instead of CvInvoke.HaveOpenCL (which also returns true on a system that only have cpu opencl devices).
CvInvoke.UseOpenCL = tryUseOpenCL && CvInvoke.HaveOpenCLCompatibleGpuDevice;
//Read the HaarCascade objects
using (CascadeClassifier face = new CascadeClassifier(faceFileName))
using (CascadeClassifier eye = new CascadeClassifier(eyeFileName))
{
watch = Stopwatch.StartNew();
using (UMat ugray = new UMat())
{
CvInvoke.CvtColor(image, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
//normalizes brightness and increases contrast of the image
CvInvoke.EqualizeHist(ugray, ugray);
//Detect the faces from the gray scale image and store the locations as rectangle
//The first dimensional is the channel
//The second dimension is the index of the rectangle in the specific channel
Rectangle[] facesDetected = face.DetectMultiScale(
ugray,
1.1,
10,
new Size(20, 20));
faces.AddRange(facesDetected);
foreach (Rectangle f in facesDetected)
{
//Get the region of interest on the faces
using (UMat faceRegion = new UMat(ugray, f))
{
Rectangle[] eyesDetected = eye.DetectMultiScale(
faceRegion,
1.1,
10,
new Size(20, 20));
foreach (Rectangle e in eyesDetected)
{
Rectangle eyeRect = e;
eyeRect.Offset(f.X, f.Y);
eyes.Add(eyeRect);
}
}
}
}
watch.Stop();
}
}
detectionTime = watch.ElapsedMilliseconds;
}
}
}
因為是寒假寫的程式
現在匆匆整理出來還有很多步驟不足
之後再補充囉