Halcon 进阶 一 C#实现Halcon中Matching图形匹配

图像结果

在这里插入图片描述

Bitmap转HObject

 public static HObject HImageConvertFromBitmap32(Bitmap bmp)
        {
            HObject ho_Image;
            HOperatorSet.GenEmptyObj(out ho_Image);
            System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            HOperatorSet.GenImageInterleaved(out ho_Image, bmpData.Scan0, "bgrx", bmp.Width, bmp.Height, -1, "byte", bmp.Width, bmp.Height, 0, 0, -1, 0);
            return ho_Image;
        }

初始化hWindowsControl

  public void Init_Window(HWindow hWindow = null)
        {
            hv_WindowHandle = null;
            if (hWindow != null)
            {
                hv_WindowHandle = hWindow;
                HOperatorSet.SetWindowAttr("background_color", "black");
                set_display_font(hv_WindowHandle, 10, "mono", "false", "false");
                HOperatorSet.SetLineWidth(hv_WindowHandle, 2);
                HOperatorSet.SetColor(hv_WindowHandle, "cyan");
                HDevWindowStack.Push(hv_WindowHandle);
            }
            else
            {
                //HOperatorSet.OpenWindow(0, 0, 1280, 960, 0, "", "", out hv_WindowHandle);
                //HOperatorSet.SetWindowAttr("background_color", "black");
            }

            //HOperatorSet.SetPart(hv_WindowHandle, 0, 0, 1200, 1600);
            //DataCodeSet();
        }

设置HWindowControl.ImagePart

点击Set按钮执行以下代码

hWindowControl1.ImagePart = new Rectangle(0, 0, int.Parse(tbImgPartW.Text), int.Parse(tbImgPartH.Text));

绘制图形

  public void action(HObject ho_Image)
        {


            // Local iconic variables 

            HObject ho_ModelRegion, ho_TemplateImage;
            HObject ho_ModelContours, ho_TransContours;

            // Local control variables 

            HTuple hv_Width = null, hv_Height = null;
            HTuple hv_Row3 = null, hv_Column3 = null, hv_Radius = null;
            HTuple hv_ModelID = null, hv_Row1 = null, hv_Column1 = null;
            HTuple hv_Row2 = null, hv_Column2 = null, hv_RefRow = null;
            HTuple hv_RefColumn = null, hv_HomMat2D = null, hv_Row = null;
            HTuple hv_Column = null, hv_Angle = null, hv_Score = null;
            HTuple hv_I = null;
            // Initialize local and output iconic variables 
            //HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_ModelRegion);
            HOperatorSet.GenEmptyObj(out ho_TemplateImage);
            HOperatorSet.GenEmptyObj(out ho_ModelContours);
            HOperatorSet.GenEmptyObj(out ho_TransContours);
            //
            //Matching 01: ************************************************
            //Matching 01: BEGIN of generated code for model initialization
            //Matching 01: ************************************************
            HOperatorSet.SetSystem("border_shape_models", "false");
            //
            //Matching 01: Obtain the model image
            //ho_Image.Dispose();
            //HOperatorSet.ReadImage(out ho_Image, path);
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            //dev_open_window_fit_image(ho_Image, 0, 0, hv_Width, hv_Height, out hv_WindowHandle);
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
            }

            //
            //Matching 01: Draw an ROI interactively
            //Matching 01: ATTENTION: you have to ensure that the window handle is valid
            //Matching 01: If you want to create a more complex region you have to
            //Matching 01: insert the appropriate operations manually
            HOperatorSet.DrawCircle(hv_WindowHandle, out hv_Row3, out hv_Column3, out hv_Radius);
            ho_ModelRegion.Dispose();
            HOperatorSet.GenCircle(out ho_ModelRegion, hv_Row3, hv_Column3, hv_Radius);
            //
            //Matching 01: Reduce the model template
            
            HOperatorSet.ReduceDomain(ho_Image, ho_ModelRegion, out ho_TemplateImage);
            //
            //Matching 01: Create the shape model
            HOperatorSet.CreateShapeModel(ho_TemplateImage, 6, (new HTuple(0)).TupleRad()
                , (new HTuple(360)).TupleRad(), (new HTuple(0.5746)).TupleRad(), (new HTuple("point_reduction_high")).TupleConcat(
                "no_pregeneration"), "use_polarity", ((new HTuple(28)).TupleConcat(36)).TupleConcat(
                6), 6, out hv_ModelID);
            //
            //Matching 01: Get the model contour for transforming it later into the image
            ho_ModelContours.Dispose();
            HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1);
            //
            //Matching 01: Set a reference position to show the model
            HOperatorSet.SmallestRectangle1Xld(ho_ModelContours, out hv_Row1, out hv_Column1,
                out hv_Row2, out hv_Column2);
            hv_RefRow = ((hv_Row2.TupleMax()) - (hv_Row1.TupleMin())) / 2;
            hv_RefColumn = ((hv_Column2.TupleMax()) - (hv_Column1.TupleMin())) / 2;
            HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out hv_HomMat2D);
            ho_TransContours.Dispose();
            HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat2D);
            //
            //Matching 01: Display the model contours
            if (HDevWindowStack.IsOpen())
            {
                //dev_display (Image)
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_TransContours, HDevWindowStack.GetActive());
            }
            // stop(...); only in hdevelop
            //
            //Matching 01: END of generated code for model initialization
            //Matching 01:  * * * * * * * * * * * * * * * * * * * * * * *
            //Matching 01: BEGIN of generated code for model application
            //Matching 01: The following operations are usually moved into
            //Matching 01: that loop where the acquired images are processed
            //
            //Matching 01: Find the model
            HOperatorSet.FindShapeModel(ho_Image, hv_ModelID, (new HTuple(0)).TupleRad(),
                (new HTuple(360)).TupleRad(), 0.5, 0, 0.5, "least_squares", (new HTuple(6)).TupleConcat(
                1), 0.75, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
            //
            //Matching 01: Transform the model contours into the detected positions
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Score.TupleLength())) - 1); hv_I = (int)hv_I + 1)
            {
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2D);
                HOperatorSet.HomMat2dRotate(hv_HomMat2D, hv_Angle.TupleSelect(hv_I), 0, 0,
                    out hv_HomMat2D);
                HOperatorSet.HomMat2dTranslate(hv_HomMat2D, hv_Row.TupleSelect(hv_I), hv_Column.TupleSelect(
                    hv_I), out hv_HomMat2D);
                ho_TransContours.Dispose();
                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours,
                    hv_HomMat2D);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_TransContours, HDevWindowStack.GetActive());
                }
                // stop(...); only in hdevelop
            }
            //
            //Matching 01: Clear model when done
            // stop(...); only in hdevelop
            HOperatorSet.ClearShapeModel(hv_ModelID);
            //Matching 01: *******************************************
            //Matching 01: END of generated code for model application
            //Matching 01: *******************************************
            //

            //ho_Image.Dispose();
            //ho_ModelRegion.Dispose();
            //ho_TemplateImage.Dispose();
            //ho_ModelContours.Dispose();
            //ho_TransContours.Dispose();

        }

源码下载

下载

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漫伊

感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值