C# +Halcon 做人脸识别

本项目使用C#和Halcon库进行人脸识别,通过读取图像、转换为灰度、创建窗口、显示图像等步骤,对图像进行预处理。然后,通过区域减少、创建NCC模型等操作,实现对特定人物的识别。最后,利用摄像头实时抓取图像并进行匹配,展示识别结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
                    }
                }
            });
        }

正在识别

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值