C# +Halcon 做人脸识别
需要完整项目邮件:Georgios@163.com
核心代码
private void Start_button_Click(object sender, EventArgs e)
{
//开启正常测试线程
Task.Run(() =>
{
Dictionary<string, HTuple> dic = new Dictionary<string, HTuple>();
HTuple phi = new HTuple(), Row = new HTuple(),
Column = new HTuple(), window = new HTuple(),
widt = new HTuple(), heig = new HTuple();
HObject Rectangle = new HObject();
List<HTuple> list = new List<HTuple>();
for (int i = 1; i <= 3; i++)
{
HOperatorSet.ReadImage(out HObject rimage, i + ".jpg");
HOperatorSet.GetImageSize(rimage, out widt, out heig);
HOperatorSet.Rgb1ToGray(rimage, out HObject image);
HOperatorSet.OpenWindow(0, 0, widt, heig, this.DisplayVideo_pictureBox.Handle, "", "", out window);
HOperatorSet.ClearWindow(window);
HOperatorSet.DispObj(image, window);
HOperatorSet.SetDraw(window, "margin");
HOperatorSet.SetLineWidth(window, 4);
HOperatorSet.SetColor(window, "red");
HOperatorSet.DrawRectangle1(window, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2);
HOperatorSet.GenRectangle1(out Rectangle, row1, col1, row2, col2);
HOperatorSet.AreaCenter(Rectangle, out HTuple Area, out Row, out Column);
HOperatorSet.OrientationRegion(Rectangle, out phi);
HOperatorSet.ReduceDomain(image, Rectangle, out HObject imgreduce);
HOperatorSet.CreateNccModel(imgreduce, "auto", -3.14, 3.14, "auto", "ignore_global_polarity", out HTuple modelid);
if (i == 2)
dic.Add("王世正", modelid);
else if (i == 3)
dic.Add("李鑫", modelid);
else
dic.Add("郭富城", modelid);
HOperatorSet.CloseWindow(window);
}
HOperatorSet.OpenWindow(0, 0, widt, heig, this.DisplayVideo_pictureBox.Handle, "", "", out HTuple window1);
HObject ho_Image;
HTuple Wid, Hei;
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8,
"gray", -1, "false", "default", "[0] ", 0, -1,
out HTuple hv_AcqHandle);
HOperatorSet.ClearWindow(window1);
while (result)
{
HOperatorSet.SetDraw(window1, "margin");
HOperatorSet.SetLineWidth(window1, 4);
HOperatorSet.SetColor(window1, "red");
foreach (var item in dic)
{
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
HOperatorSet.FindNccModel(ho_Image, item.Value, -3.14, 3.14, 0.1, 1, 0.5, "true", 0,
out HTuple CurrentRow, out HTuple CurrentColum,
out HTuple CurrentAngle, out HTuple Score);
//if (CurrentRow.Length == 0)
//{
// return;
//}
//if (Score != null && Score > 0.66)
//{
// Thread.Sleep(3000);
// MessageBox.Show($"你的图片是{item.Key},你的识别率{Score}");
// result = false;
//}
HOperatorSet.VectorAngleToRigid(Row, Column, phi, CurrentRow, CurrentColum,
CurrentAngle, out HTuple Home2D);
HOperatorSet.AffineTransRegion(Rectangle, out HObject region, Home2D, "nearest_neighbor");
HOperatorSet.DispObj(region, window1);
HOperatorSet.DispObj(ho_Image, window1);
region.Dispose();
ho_Image.Dispose();
}
}
});
}