void findshapemodel(HObject newimg)
 {
     HOperatorSet.FindShapeModel(newimg, ModelID, -0.39, 0.79, 0.5, 1, 0.3, "least_squares", new HTuple(4, -2), 0.9, out HTuple Row, out HTuple Column, out HTuple Angle, out HTuple Score);
     HOperatorSet.VectorAngleToRigid(0, 0, 0, Row[0], Column[0], Angle[0], out HTuple HomMat2D);
     HOperatorSet.GetShapeModelContours(out HObject ModelContour, ModelID, 1);
     HOperatorSet.AffineTransContourXld(ModelContour, out HObject contoursAffine, HomMat2D);
     HOperatorSet.GenCrossContourXld(out HObject cross, Row[0], Column[0], 20, 90);
        public bool FindModel(HObject img)
        {
            HTuple  hv_HomMat2D;
            HObject HModelContours;

            HOperatorSet.GenEmptyObj(out HModelContours);
            try
            {
                HModelContours.Dispose();
                HOperatorSet.GetShapeModelContours(out HModelContours, HShapeModelID, 1);
                HOperatorSet.FindScaledShapeModel(img, HShapeModelID, HangleStart * 3.1415926 / 180, HangleExtent * 3.1415926 / 180,
                                                  HscaleMin, HscaleMax, 0.2, H_NumMatches, HmaxOverlap, H_SubPixel, H_NumLevels,
                                                  Hgreediness, out out_row, out out_col, out out_phi, out out_scale, out out_score);
                if (out_score[0].D > HminScore)
                {
                    HOperatorSet.VectorAngleToRigid(0, 0, 0, out_row, out_col, out_phi, out hv_HomMat2D);
                    m_hModelXLD.Dispose();
                    HOperatorSet.AffineTransContourXld(HModelContours, out m_hModelXLD, hv_HomMat2D);
                    Result_Array[0] = out_row;
                    Result_Array[1] = out_col;
                    Result_Array[2] = out_phi;
                    return(true);
                }
                else
                {
                    MessageBox.Show("匹配分值太低");
                    return(false);
                }
            }
            catch (System.Exception ex)
            {
                return(false);
            }
        }
        void create_display_model()
        {
            HObject HImageReduced = null, HModelContours = null;
            HTuple  hv_Area, Htemprow, Htempcol, hv_HomMat2D;

            HOperatorSet.GenEmptyObj(out HImageReduced);
            HOperatorSet.GenEmptyObj(out HModelContours);
            try
            {
                HImageReduced.Dispose();
                HOperatorSet.ReduceDomain(image_show, Hdraw_roi, out HImageReduced);
                HOperatorSet.AreaCenter(Hdraw_roi, out hv_Area, out Htemprow, out Htempcol);

                HOperatorSet.CreateScaledShapeModel(HImageReduced, H_NumLevels, HangleStart * 3.1415926 / 180, HangleExtent * 3.1415926 / 180,
                                                    H_AngleStep, HscaleMin, HscaleMax, H_ScaleStep, H_Optimization, H_Metric,
                                                    Hcontrast, HminContrast, out HShapeModelID);
                HModelContours.Dispose();
                HOperatorSet.GetShapeModelContours(out HModelContours, HShapeModelID, 1);
                HOperatorSet.VectorAngleToRigid(0, 0, 0, Htemprow, Htempcol, 0, out hv_HomMat2D);
                m_hModelXLD.Dispose();
                HOperatorSet.AffineTransContourXld(HModelContours, out m_hModelXLD, hv_HomMat2D);
                //HOperatorSet.SetColor(Hwin, "blue");
                //HOperatorSet.DispObj(m_hModelXLD, Hwin);
                show_pic_roi();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("创建模板失败");
            }
        }
        private async void button1_Click_1(object sender, EventArgs e)
        {
            HObject _image = HardWareManager.Instance.Cmaera.SnapShot();

            VisionManager.Instance.ShapeModle.BackImage = _image.Clone();
            //DelegateControls.Instance.
            ModleFindResult result = VisionManager.Instance.ShapeModle.FindSimple();

            // return;
            if (result != null)
            {
                HTuple hommate2d;
                HOperatorSet.VectorAngleToRigid(0, 0, 0, result.Row, result.Column, result.Angle, out hommate2d);
                HOperatorSet.AffineTransContourXld(result.ModleRegion, out HObject hObject, hommate2d);
                RegionX regionX = new RegionX(hObject, "green");
                DelegateControls.Instance.DelegateHdisplay("FormMain_hDisplay1", _image, new List <HalWindow.RegionX>()
                {
                    regionX
                });
                HTuple calibratedata = VisionManager.Instance.Calibrate.CalibrateData;

                HTuple _x = new HTuple();
                HTuple _y = new HTuple();
                HOperatorSet.AffineTransPoint2d(calibratedata, result.Row, result.Column, out HTuple x, out HTuple y);
                HOperatorSet.AffineTransPoint2d(calibratedata, 1024, 1536, out _x, out _y);
                double   ox       = -x + _x + VisionManager.Instance.Calibrate.ToolOffsetX;
                double   oy       = -y + _y + VisionManager.Instance.Calibrate.ToolOffsetY;
                RobotPos robotPos = await HardWareManager.Instance.YaskawaRobot.GetRobotCPoint();

                double x1 = ox + robotPos.Y;
                double x2 = oy + robotPos.Z;
            }
        }
        /// <summary>
        /// 创建圆形模板
        /// </summary>
        /// <param name="window">绘图窗口</param>
        /// <param name="image">输入图像</param>
        public void DrawCircle(HWindow window, HObject image)
        {
            HObject ho_Circle, ho_targetROI, ho_coutours;
            HTuple  hv_Row2 = null, hv_Column3 = null, hv_Radius3 = null, hv_ModelID = null;;

            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_targetROI);
            HOperatorSet.GenEmptyObj(out ho_coutours);
            HOperatorSet.SetColor(window, "red");
            HOperatorSet.SetLineWidth(window, 2);
            HOperatorSet.SetDraw(window, "margin");

            HOperatorSet.DrawCircle(window, out hv_Row2, out hv_Column3,
                                    out hv_Radius3);
            ho_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_Circle, hv_Row2, hv_Column3, hv_Radius3);
            ho_targetROI.Dispose();
            HOperatorSet.DispObj(ho_Circle, window);
            HTuple  hv_Area, hv_Row4, hv_Column4;
            HTuple  hv_HomMat2DIdentity, hv_HomMat2DTranslate;
            HObject ho_ContoursAffinTrans, ho_Cross;

            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_Cross);
            DialogResult Result = MessageBox.Show("是否保存模板!", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

            if (Result == DialogResult.OK)
            {
                HOperatorSet.ReduceDomain(image, ho_Circle, out ho_targetROI);
                HOperatorSet.AreaCenter(ho_targetROI, out hv_Area, out hv_Row4, out hv_Column4);
                HOperatorSet.SetSystem("border_shape_models", "false");
                //HOperatorSet.CreateShapeModel(ho_targetROI, 4, -0.39, 0.79, "auto", "auto", "ignore_color_polarity",
                //                              "auto", "auto", out hv_ModelID);
                HOperatorSet.CreateShapeModel(ho_targetROI, 4, -0.39, 0.79, "auto", "point_reduction_high", "ignore_global_polarity",
                                              "auto", "auto", out hv_ModelID);
                //if (!Directory.Exists(_filePath))
                //{ Directory.CreateDirectory(_filePath); }
                HOperatorSet.WriteShapeModel(hv_ModelID, /*_filePath + "//" + */ _fileName);

                ho_coutours.Dispose();
                HOperatorSet.GetShapeModelContours(out ho_coutours, hv_ModelID, 1);
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                HOperatorSet.HomMat2dTranslate(hv_HomMat2DIdentity, hv_Row4, hv_Column4, out hv_HomMat2DTranslate);
                ho_ContoursAffinTrans.Dispose();
                HOperatorSet.AffineTransContourXld(ho_coutours, out ho_ContoursAffinTrans, hv_HomMat2DTranslate);

                HOperatorSet.DispObj(image, window);
                HOperatorSet.DispObj(ho_ContoursAffinTrans, window);
                HOperatorSet.SetColor(window, "green");
                HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row4, hv_Column4,
                                                180, 0.785398);
                HOperatorSet.DispObj(ho_Cross, window);
                MessageBox.Show("已保存模板", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                HOperatorSet.DispObj(image, window);
                return;
            }
        }
        void createModel()
        {
            HObject contoursAffine = new HObject();

            if (contoursAffine != null)
            {
                contoursAffine.Dispose();
            }
            HTuple _row       = arr1[0];
            HTuple _comlumn   = arr1[1];
            HTuple _rec_leng1 = arr1[2];
            HTuple _rec_leng2 = arr1[3];
            HTuple _phi       = arr1[4];

            HOperatorSet.GenRectangle2(out HObject rectangleNew, _row, _comlumn, _phi, _rec_leng1, _rec_leng2);
            HOperatorSet.ReduceDomain(Img, rectangleNew, out HObject imgReduced);
            HOperatorSet.CreateShapeModel(imgReduced, "auto", -0.39, 0.79, "auto", "auto", "use_polarity", "auto", "auto", out ModelID);
            HOperatorSet.FindShapeModel(Img, ModelID, -0.39, 0.79, 0.5, 1, 0.5, "least_squares", new HTuple(4, -2), 0.9, out HTuple Row, out HTuple Column, out HTuple Angle, out HTuple Score);
            HOperatorSet.VectorAngleToRigid(0, 0, 0, Row[0], Column[0], Angle[0], out HTuple HomMat2D);
            HOperatorSet.GetShapeModelContours(out HObject ModelContour, ModelID, 1);
            HOperatorSet.AffineTransContourXld(ModelContour, out contoursAffine, HomMat2D);
            window.SetColor("green");
            window.SetDraw("margin");
            window.SetLineWidth(3);
            contoursAffine.DispObj(window);
        }
Exemple #7
0
        private bool FindCorner()
        {
            if (cornerParams.ho_Image == null)
            {
                MessageBox.Show("图像为空");
                return(false);
            }
            try
            {
                Show2HWindow(cornerParams.ho_Image);
                HOperatorSet.ApplyMetrologyModel(cornerParams.ho_Image, cornerParams.hv_MetrologyHandle);

                HOperatorSet.GetMetrologyObjectResult(cornerParams.hv_MetrologyHandle, "all", "all", "result_type", "row_begin", out cornerParams.hv_LineRowBegin);
                HOperatorSet.GetMetrologyObjectResult(cornerParams.hv_MetrologyHandle, "all", "all", "result_type", "column_begin", out cornerParams.hv_LineColumnBegin);
                HOperatorSet.GetMetrologyObjectResult(cornerParams.hv_MetrologyHandle, "all", "all", "result_type", "row_end", out cornerParams.hv_LineRowEnd);
                HOperatorSet.GetMetrologyObjectResult(cornerParams.hv_MetrologyHandle, "all", "all", "result_type", "column_end", out cornerParams.hv_LineColumnEnd);

                if (cornerParams.hv_LineRowBegin.Length != cornerParams.hv_Number)
                {
                    MessageBox.Show("直线数量错误!");
                    return(false);
                }

                HTuple hv_IsOverlapping = null;
                HOperatorSet.IntersectionLines(cornerParams.hv_LineRowBegin[0], cornerParams.hv_LineColumnBegin[0], cornerParams.hv_LineRowEnd[0], cornerParams.hv_LineColumnEnd[0],
                                               cornerParams.hv_LineRowBegin[1], cornerParams.hv_LineColumnBegin[1], cornerParams.hv_LineRowEnd[1], cornerParams.hv_LineColumnEnd[1], out cornerParams.hv_PointRow, out cornerParams.hv_PointColumn,
                                               out hv_IsOverlapping);

                if (cornerParams.hv_PointRow.Length != 1)
                {
                    MessageBox.Show("直线交点错误!");
                    return(false);
                }
                HObject ho_MeasuredCross;
                HOperatorSet.GenCrossContourXld(out ho_MeasuredCross, cornerParams.hv_PointRow, cornerParams.hv_PointColumn, 40, (new HTuple(45)).TupleRad());//在模板中心产生一个x,表示模板中心

                HObject ho_MeasuredLines;
                HOperatorSet.GenEmptyObj(out ho_MeasuredLines);
                HOperatorSet.GetMetrologyObjectResultContour(out ho_MeasuredLines, cornerParams.hv_MetrologyHandle, "all", "all", 1.5);

                HOperatorSet.AffineTransContourXld(ho_MeasuredLines, out ho_MeasuredLines, hv_XldHomMat2D);
                HOperatorSet.AffineTransContourXld(ho_MeasuredCross, out ho_MeasuredCross, hv_XldHomMat2D);

                HOperatorSet.SetColor(hv_ImageWindow, "red");
                HOperatorSet.DispObj(ho_MeasuredLines, hv_ImageWindow);
                HOperatorSet.DispObj(ho_MeasuredCross, hv_ImageWindow);

                MessageBox.Show("查找直线模型成功!");
                return(true);
            }
            catch (Exception exc)
            {
                MessageBox.Show("查找直线模型失败!" + exc.ToString());
                return(false);
            }
        }
Exemple #8
0
        /// <summary>
        /// 这版软件的思想是将新图片模板匹配后,先转到和示教图片一样的位置,完成定位后,再将定位结果转到实际图片位置
        /// </summary>
        /// <param name="HomMat"></param>
        /// <param name="AffinResult"></param>
        public virtual void AffineTransResult(HTuple HomMat, out LocalResult AffinResult)
        {
            AffinResult = new LocalResult();
            MyVisionBase.AffineTransPt(row, col, HomMat, out AffinResult.row, out AffinResult.col);
            HTuple sx = new HTuple(), sy = new HTuple(), phi = new HTuple(), theta = new HTuple(), tx = new HTuple(), ty = new HTuple();

            HOperatorSet.HomMat2dToAffinePar(HomMat, out sx, out sy, out phi, out theta, out tx, out ty);
            AffinResult.angle = angle - phi.D;
            HOperatorSet.AffineTransContourXld(ShowContour, out AffinResult.ShowContour, HomMat);
            AffinResult.IsLocalOk = true;
        }
        private void btnRunmannual_Click(object sender, EventArgs e)
        {
            HOperatorSet.FindScaledShapeModel(Img, ModelID, -0.39, 0.79, 0.8, 1.0, 0.5, 0, 0.5, "least_squares", 5, 0.8, out HTuple Row, out HTuple Column, out HTuple Angle, out HTuple Scales, out HTuple Score);
            HOperatorSet.HomMat2dIdentity(out HTuple HM2D);
            HOperatorSet.HomMat2dTranslate(HM2D, Row[0], Column[0], out HTuple HM2DTranslate);
            HOperatorSet.HomMat2dRotate(HM2DTranslate, Angle[0], Row[0], Column[0], out HTuple Homat2DRotary);
            HOperatorSet.HomMat2dScale(Homat2DRotary, Scales[0], Scales[0], Row[0], Column[0], out HTuple HM2DALL);
            HOperatorSet.GetShapeModelContours(out HObject ModelContour, ModelID, 1);
            HOperatorSet.AffineTransContourXld(ModelContour, out HObject ContourAffine, HM2DALL);
            window.SetColor("green");
            window.SetDraw("margin");
            window.SetLineWidth(3);
            ContourAffine.DispObj(window);
            //ModelContour.DispObj(window);


            //DataTable dt = Lib.GetTableData(string.Format("select * from TrainModel where Roi = '{0}'", "Roi3"));
            //double _X = Lib.ToDouble(dt.Rows[0]["X"]);
            //double _Y = Lib.ToDouble(dt.Rows[0]["Y"]);
            //double _L1 = Lib.ToDouble(dt.Rows[0]["L1"]);
            //double _L2 = Lib.ToDouble(dt.Rows[0]["L2"]);
            //double _Theta = Lib.ToDouble(dt.Rows[0]["Theta"]);

            datatrain dtRec  = lstDataTrain.FirstOrDefault(o => o.Roi == "Roi3");
            double    _X     = dtRec.X;
            double    _Y     = dtRec.Y;
            double    _L1    = dtRec.L1;
            double    _L2    = dtRec.L2;
            double    _Theta = dtRec.Theta;



            datatrain dttMaster    = lstDataTrain.FirstOrDefault(o => o.Roi == "Master");
            double    _XMaster     = dttMaster.X;
            double    _YMaster     = dttMaster.Y;
            double    _L1Master    = dttMaster.L1;
            double    _L2Master    = dttMaster.L2;
            double    _ThetaMaster = dttMaster.Theta;


            HOperatorSet.GenRectangle2(out HObject RoiCheck, _X, _Y, _Theta, _L1, _L2);

            //HOperatorSet.AffineTransRegion(RoiCheck, out HObject RoicheckAffine, HM2DALL, "nearest_neighbor");
            //HOperatorSet.HomMat2dIdentity(out HTuple Homat2D);
            //HOperatorSet.HomMat2dTranslate(Homat2D, -_X, -_Y, out HTuple Homat2DTrans);
            //HOperatorSet.AffineTransRegion(RoicheckAffine, out HObject RoicheckFinal, Homat2DTrans, "nearest_neighbor");
            //RoicheckFinal.DispObj(window);


            HOperatorSet.VectorAngleToRigid(_XMaster, _YMaster, _ThetaMaster, Row[0], Column[0], Angle[0], out HTuple HM2DMaster);
            HOperatorSet.AffineTransRegion(RoiCheck, out HObject RoiCheckTrans, HM2DMaster, "nearest_neighbor");
            RoiCheck.DispObj(window);
            RoiCheckTrans.DispObj(window);
        }
Exemple #10
0
        //--------------------//
        #region Find shape model
        public static result findshapemodle(HObject newimg, HTuple ModelID, HWindow _window)
        {
            result rs = new result();

            rs.result_process = "OK";
            try
            {
                HOperatorSet.FindShapeModel(newimg, ModelID, -0.39, 0.79, 0.5, 1, 0.3, "least_squares", new HTuple(4, -2), 0.9, out HTuple Row, out HTuple Column, out HTuple Angle, out HTuple Score);
                HOperatorSet.VectorAngleToRigid(0, 0, 0, Row[0], Column[0], Angle[0], out HTuple HomMat2D);
                HOperatorSet.GetShapeModelContours(out HObject ModelContour, ModelID, 1);
                HOperatorSet.AffineTransContourXld(ModelContour, out HObject shapemodel_affined, HomMat2D);
                HOperatorSet.GenCrossContourXld(out HObject cross, Row[0], Column[0], 20, 90);
Exemple #11
0
        private void SearchObject(HTuple TempModelID, HObject SearchImage)
        {
            try
            {
                HTuple hv_RefRow = null, hv_RefColumn = null, hv_RefAngle = null, hv_Score = null;
                HOperatorSet.FindShapeModel(SearchImage, TempModelID, (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", (new HTuple(6)).TupleConcat(1), 0.75, out hv_RefRow, out hv_RefColumn, out hv_RefAngle, out hv_Score);

                //获取模版中对象轮廓
                HObject ho_ModelContours;
                HOperatorSet.GenEmptyObj(out ho_ModelContours);
                HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1);

                //构建一个仿射变换矩阵
                HTuple hv_HomMat2D = null;
                HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out hv_HomMat2D);

                //对图像左上角对象轮廓进行仿射变换,将轮廓平移到产品位置
                HObject ho_TransContours;
                HOperatorSet.GenEmptyObj(out ho_TransContours);
                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat2D);


                GraphInteractiveObect.graphic_stackClear();
                VideoWindow_hWindowControl.HalconWindow.ClearWindow();

                HOperatorSet.SetColor(GraphInteractiveObect.halconWindow.HalconWindow, "yellow");
                HOperatorSet.SetDraw(GraphInteractiveObect.halconWindow.HalconWindow, "margin");
                HOperatorSet.DispObj(SearchImage, GraphInteractiveObect.halconWindow.HalconWindow);
                HOperatorSet.DispObj(ho_TransContours, GraphInteractiveObect.halconWindow.HalconWindow);

                HOperatorSet.SetColor(GraphInteractiveObect.halconWindow.HalconWindow, "blue");
                HOperatorSet.SetDraw(GraphInteractiveObect.halconWindow.HalconWindow, "fill");
                HOperatorSet.DispLine(GraphInteractiveObect.halconWindow.HalconWindow, hv_RefRow, hv_RefColumn - 100, hv_RefRow, hv_RefColumn + 100);
                HOperatorSet.DispLine(GraphInteractiveObect.halconWindow.HalconWindow, hv_RefRow - 100, hv_RefColumn, hv_RefRow + 100, hv_RefColumn);

                this.ModelRow_textBox.Text    = hv_RefRow.ToString();
                this.txtRow.Text              = hv_RefRow.ToString();
                this.ModelColumn_textBox.Text = hv_RefColumn.ToString();
                this.txtCol.Text              = hv_RefColumn.ToString();
                this.ModelAngle_textBox.Text  = hv_RefAngle.ToString();
                this.txtAngle.Text            = hv_RefAngle.ToString();

                HOperatorSet.SetColor(GraphInteractiveObect.halconWindow.HalconWindow, "yellow");
                HOperatorSet.SetDraw(GraphInteractiveObect.halconWindow.HalconWindow, "margin");
                saveImage("Pos");
                ho_ModelContours.Dispose();
                ho_TransContours.Dispose();
            }
            catch (HalconException ex)
            {
            }
        }
    public void RunPictureMatching()
    {
        ho_ModelRegion.Dispose();
        HOperatorSet.DrawRegion(out ho_ModelRegion, hv_ExpDefaultWinHandle);

        ho_TemplateImage.Dispose();
        HOperatorSet.ReduceDomain(ho_Dog, ho_ModelRegion, out ho_TemplateImage);

        HOperatorSet.CreateShapeModel(ho_TemplateImage, 2, (new HTuple(0)).TupleRad()
                                      , (new HTuple(360)).TupleRad(), (new HTuple(6.2177)).TupleRad(), (new HTuple("none")).TupleConcat(
                                          "no_pregeneration"), "use_polarity", ((new HTuple(10)).TupleConcat(13)).TupleConcat(
                                          4), 10, 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: END of generated code for model initialization
        //Matching 01: BEGIN of generated code for model application
        //Matching 01: the following operations are usually moved into
        //Matching 01: that loop where the aquired images are processed
        //Matching 01: Find the model
        HOperatorSet.FindShapeModel(ho_Dog, hv_ModelId, (new HTuple(0)).TupleRad(),
                                    (new HTuple(360)).TupleRad(), 0.78, 17, 0.5, "least_squares", (new HTuple(2)).TupleConcat(
                                        1), 0.9, out hv_ModelRow, out hv_ModelColumn, out hv_ModelAngle, out hv_ModelScore);


        for (hv_MatchingObjIdx = 0; (int)hv_MatchingObjIdx <= (int)((new HTuple(hv_ModelScore.TupleLength()
                                                                                )) - 1); hv_MatchingObjIdx = (int)hv_MatchingObjIdx + 1)
        {
            HOperatorSet.HomMat2dIdentity(out hv_HomMat);
            HOperatorSet.HomMat2dRotate(hv_HomMat, hv_ModelAngle.TupleSelect(hv_MatchingObjIdx),
                                        0, 0, out hv_HomMat);
            HOperatorSet.HomMat2dTranslate(hv_HomMat, hv_ModelRow.TupleSelect(hv_MatchingObjIdx),
                                           hv_ModelColumn.TupleSelect(hv_MatchingObjIdx), out hv_HomMat);
            ho_TransContours.Dispose();
            HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours,
                                               hv_HomMat);
            HOperatorSet.DispObj(ho_TransContours, hv_ExpDefaultWinHandle);
        }

        //Matching 01: Clear model when done
        HOperatorSet.ClearShapeModel(hv_ModelId);
        //Matching 01: END of generated code for model application

        ho_Dog.Dispose();
        ho_ModelRegion.Dispose();
        ho_TemplateImage.Dispose();
        ho_ModelContours.Dispose();
        ho_TransContours.Dispose();
    }
Exemple #13
0
        private void ReadMoudle_Click(object sender, RoutedEventArgs e)
        {
            HTuple hv_HomMat, hv_HomMat2D;


            double MoudleWidth, MoudleHeight;

            MoudleHeight = HwindowMoudle.Height;
            MoudleWidth  = HwindowMoudle.Width;

            HOperatorSet.ClearWindow(HwindowMoudle.HalconID);

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter           = "SHM文件 |*.shm*";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.FilterIndex      = 1; //设置对话框属性
            if (openFileDialog1.ShowDialog() == true)
            {
                ImagePath = openFileDialog1.FileName;

                HOperatorSet.ReadShapeModel(ImagePath, out MoudleID);

                HOperatorSet.GetShapeModelContours(out ModelContours, MoudleID, 1);

                HOperatorSet.HomMat2dIdentity(out hv_HomMat);

                HOperatorSet.HomMat2dIdentity(out hv_HomMat2D);

                HOperatorSet.HomMat2dRotate(hv_HomMat, 0, 0, 0, out hv_HomMat);

                HOperatorSet.HomMat2dTranslate(hv_HomMat, 0, 0, out hv_HomMat);

                HOperatorSet.VectorAngleToRigid(0, 0, 0, 100, 100, 0, out hv_HomMat2D);

                // HOperatorSet.AffineTransContourXld(ModelContours, out ModelContours, hv_HomMat);
                HOperatorSet.AffineTransContourXld(ModelContours, out ModelContours, hv_HomMat2D);

                HOperatorSet.SetWindowParam(HwindowMoudle.HalconID, "flush", "false");
                HOperatorSet.ClearWindow(HwindowMoudle.HalconID);
                HOperatorSet.DispObj(ModelContours, HwindowMoudle.HalconID);
                HOperatorSet.SetWindowParam(HwindowMoudle.HalconID, "flush", "true");
                HOperatorSet.FlushBuffer(HwindowMoudle.HalconID);
            }
            else
            {
                return;
            }
        }
Exemple #14
0
        /// <summary>
        /// 由点获取轮廓
        /// </summary>
        /// <param name="isLeft">是否在左边</param>
        /// <param name="IgnorePoints">起始结束忽略点数</param>
        /// <param name="Row">输入轮廓行坐标</param>
        /// <param name="Col">输入轮廓列坐标</param>
        /// <param name="RowNew">输出转换后轮廓列坐标</param>
        /// <param name="ColNew">输出转换后轮廓行坐标</param>
        /// <param name="Region">输处转换后轮廓Region</param>
        /// <returns></returns>
        public string GenProfile(bool isLeft, HTuple Row, HTuple Col, out HTuple RowNew, out HTuple ColNew, out HObject Region, out HObject Contour)
        {
            RowNew  = new HTuple(); ColNew = new HTuple(); Region = new HObject();
            Contour = new HObject();
            try
            {
                HTuple Lenr = Row.Length;
                HTuple Lenc = Col.Length;
                HTuple min  = Lenr.TupleMin2(Lenc);
                Row = -Row.TupleSelectRange(0, min.I - 1);
                Col = Col.TupleSelectRange(0, min.I - 1);
                HOperatorSet.GenContourPolygonXld(out Contour, Row, Col);
                //HOperatorSet.WriteObject(Contour, "1.hobj");
                HTuple hommat2DIdentity = new HTuple(); HTuple Hommat2DRotate = new HTuple();
                HTuple Hommat2DTranslate = new HTuple();
                HOperatorSet.HomMat2dIdentity(out hommat2DIdentity);
                //HOperatorSet.HomMat2dRotate(hommat2DIdentity, 1.57, 0, 0, out Hommat2DRotate);
                //HObject tempContour = new HObject();
                //HOperatorSet.AffineTransContourXld(Contour, out tempContour, Hommat2DRotate);
                //HOperatorSet.GetContourXld(tempContour, out Row, out Col);
                HTuple ColMin = Col.TupleMin();

                HOperatorSet.HomMat2dTranslate(hommat2DIdentity, 500, -ColMin + 500, out Hommat2DTranslate);
                HOperatorSet.AffineTransContourXld(Contour, out Contour, Hommat2DTranslate);
                //HOperatorSet.WriteObject(Contour, "2.hobj");
                HOperatorSet.GetContourXld(Contour, out RowNew, out ColNew);
                HOperatorSet.GenRegionPoints(out Region, RowNew, ColNew);

                if (isLeft == false)
                {
                    //找到最高点进行镜像变换
                    HTuple Rowmin = RowNew.TupleMin();
                    HTuple minInd = RowNew.TupleFindFirst(Rowmin);
                    HTuple HomMat2DId = new HTuple(); HTuple HomMat2dReflect = new HTuple();
                    HOperatorSet.HomMat2dIdentity(out HomMat2DId);
                    HOperatorSet.HomMat2dReflect(HomMat2DId, Rowmin, ColNew[minInd] + 50, Rowmin + 100, ColNew[minInd] + 50, out HomMat2dReflect);
                    HOperatorSet.AffineTransContourXld(Contour, out Contour, HomMat2dReflect);
                    HOperatorSet.AffineTransRegion(Region, out Region, HomMat2dReflect, "nearest_neighbor");
                    HOperatorSet.GetContourXld(Contour, out RowNew, out ColNew);
                    HOperatorSet.GenRegionPoints(out Region, RowNew, ColNew);
                    //HOperatorSet.WriteObject(Region, "4.hobj");
                }
                return("OK");
            }
            catch (Exception ex)
            {
                return("GenProfile error " + ex.Message);
            }
        }
Exemple #15
0
 /// <summary>
 /// 仿射变换
 /// </summary>
 /// <param name="ModelXLD"></param>
 /// <param name="Row"></param>
 /// <param name="Col"></param>
 /// <param name="Angle"></param>
 /// <param name="Scale"></param>
 /// <param name="TransContours"></param>
 public static void AffineModelXld(HObject ModelXLD, HTuple Row, HTuple Col, HTuple Angle, HTuple Scale, out HObject TransContours)
 {
     HOperatorSet.GenEmptyObj(out TransContours);
     for (int i = 0; i < Scale.TupleLength(); i++)
     {
         HObject hobject = new HObject();
         //HOperatorSet.GenEmptyObj(out hobject);
         //hobject.Dispose();
         HOperatorSet.HomMat2dIdentity(out HTuple Mat2DIdentity);
         HOperatorSet.HomMat2dRotate(Mat2DIdentity, Angle[i], 0, 0, out HTuple Mat2DRotate);
         HOperatorSet.HomMat2dTranslate(Mat2DRotate, Row[i], Col[i], out HTuple Mat2DTranslate);
         HOperatorSet.AffineTransContourXld(ModelXLD, out hobject, Mat2DTranslate);
         HOperatorSet.ConcatObj(TransContours, hobject, out TransContours);
     }
 }
Exemple #16
0
        private void get_calibarate_data(HTuple cameraWindow, TextBox x, TextBox y)
        {
            try
            {
                HTuple hv_Row1    = null;
                HTuple hv_Column1 = null;
                HTuple hv_Row2    = null;
                HTuple hv_Column2 = null;
                HOperatorSet.SetColor(cameraWindow, "red");
                HOperatorSet.DrawRectangle1(cameraWindow, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2);

                HTuple roi_hv_Row1    = null;
                HTuple roi_hv_Column1 = null;
                HTuple roi_hv_Row2    = null;
                HTuple roi_hv_Column2 = null;

                roi_hv_Row1    = hv_Row1 / hv_ZoomFactor;
                roi_hv_Column1 = hv_Column1 / hv_ZoomFactor;
                roi_hv_Row2    = hv_Row2 / hv_ZoomFactor;
                roi_hv_Column2 = hv_Column2 / hv_ZoomFactor;

                HObject ho_ROI = new HObject();
                HOperatorSet.GenRectangle1(out ho_ROI, roi_hv_Row1, roi_hv_Column1, roi_hv_Row2, roi_hv_Column2);

                HTuple hv_Row_camera    = null;
                HTuple hv_Column_camera = null;

                if (!findCircle(ho_Image, ho_ROI, ref hv_Row_camera, ref hv_Column_camera))
                {
                    MessageBox.Show("查找圆心失败!");
                    return;
                }
                HObject ho_MeasuredPoint;
                HOperatorSet.GenCrossContourXld(out ho_MeasuredPoint, hv_Row_camera, hv_Column_camera, 40, (new HTuple(45)).TupleRad());

                x.Text = hv_Row_camera.ToString();
                y.Text = hv_Column_camera.ToString();

                HOperatorSet.AffineTransContourXld(ho_MeasuredPoint, out ho_MeasuredPoint, hv_XldHomMat2D);

                HOperatorSet.DispObj(ho_MeasuredPoint, cameraWindow);
            }
            catch (Exception)
            {
                MessageBox.Show("框取区域失败!");
            }
        }
Exemple #17
0
 private void CreateVisualModel(HObject InImage, HObject ModelRegion, out HObject hModelXld, out HTuple ModelID)
 {
     HOperatorSet.ReduceDomain(InImage, ModelRegion, out HObject ho_TemplateImage);
     HOperatorSet.GenEmptyObj(out hModelXld);
     ModelID = new HTuple();
     try
     {
         HTuple optimization = new HTuple("no_pregeneration");
         HOperatorSet.TupleConcat(optimization, DetermineModelParamArr["optimization"].ToString(), out optimization);
         HTuple contrast = new HTuple(Convert.ToInt32(DetermineModelParamArr["contrast_low"]));
         HOperatorSet.TupleConcat(contrast, Convert.ToInt32(DetermineModelParamArr["contrast_high"]), out contrast);
         HOperatorSet.TupleConcat(contrast, Convert.ToInt32(DetermineModelParamArr["min_size"]), out contrast);
         HOperatorSet.CreateScaledShapeModel(ho_TemplateImage
                                             , new HTuple(Convert.ToInt32(DetermineModelParamArr["num_levels"]))
                                             , (new HTuple(Convert.ToDouble(DetermineModelParamArr["angle_start"]))).TupleRad()
                                             , (new HTuple(Convert.ToDouble(DetermineModelParamArr["angle_extent"]))).TupleRad()
                                             , (new HTuple(Convert.ToDouble(DetermineModelParamArr["angle_step"]))).TupleRad()
                                             , new HTuple(Convert.ToDouble(DetermineModelParamArr["scale_min"]))
                                             , new HTuple(Convert.ToDouble(DetermineModelParamArr["scale_max"]))
                                             , new HTuple(Convert.ToDouble(DetermineModelParamArr["scale_step"]))
                                             , optimization
                                             , new HTuple(DetermineModelParamArr["metric"].ToString())
                                             , contrast
                                             , new HTuple(Convert.ToInt32(DetermineModelParamArr["min_contrast"]))
                                             , out ModelID
                                             );
         FindModelParamArr["NumLevels"]   = DetermineModelParamArr["num_levels"];
         FindModelParamArr["AngleStart"]  = DetermineModelParamArr["angle_start"];
         FindModelParamArr["AngleExtent"] = DetermineModelParamArr["angle_extent"];
         FindModelParamArr["ScaleMin"]    = DetermineModelParamArr["scale_min"];
         FindModelParamArr["ScaleMax"]    = DetermineModelParamArr["scale_max"];
         HOperatorSet.GenEmptyObj(out HObject ho_ModelContours);
         ho_ModelContours.Dispose();
         HOperatorSet.GetShapeModelContours(out ho_ModelContours, ModelID, 1);
         //
         //Matching 01: Get the reference position
         HOperatorSet.AreaCenter(ModelRegion, out HTuple hv_ModelRegionArea, out HTuple hv_RefRow,
                                 out HTuple hv_RefColumn);
         HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out HTuple hv_HomMat2D);
         hModelXld.Dispose();
         HOperatorSet.AffineTransContourXld(ho_ModelContours, out hModelXld, hv_HomMat2D);
     }
     catch (HalconException e)
     {
     }
 }
Exemple #18
0
        public static bool modelMatch(HObject image, HTuple modelID, string modelType, HTuple startAngle, HTuple endAngle, HTuple minScore, HTuple matchNum, out HObject modelContour, out HTuple row, out HTuple col, out HTuple angle, out HTuple score)
        {
            HOperatorSet.GenEmptyObj(out modelContour);
            row   = null;
            col   = null;
            angle = null;
            score = null;

            try
            {
                if ("形状模板" == modelType)
                {
                    HOperatorSet.FindShapeModel(image, modelID, startAngle, endAngle, minScore, matchNum, 0.5, "least_squares", 0, 0.9, out row, out col, out angle, out score);
                    if (0 < row.Length)
                    {
                        HObject contour, contourAffined;
                        // HOperatorSet.GenEmptyObj(out contour);
                        HTuple homMat2D;
                        HOperatorSet.GetShapeModelContours(out contour, modelID, 1);
                        //HOperatorSet.VectorAngleToRigid(0, 0, 0, row[0], col[0], angle[0], out homMat2D);
                        //HOperatorSet.AffineTransContourXld(contour, out modelContour, homMat2D);
                        for (int i = 0; i < row.Length; i++)
                        {
                            //  HTuple homMat2D;
                            HOperatorSet.VectorAngleToRigid(0, 0, 0, row[i], col[i], angle[i], out homMat2D);
                            HOperatorSet.AffineTransContourXld(contour, out contourAffined, homMat2D);
                            HOperatorSet.ConcatObj(modelContour, contourAffined, out modelContour);
                        }
                    }
                }
                else if ("灰度模板" == modelType)
                {
                }

                if (matchNum == score.Length)
                {
                    return(true);
                }
            }
            catch (HalconException ex)
            {
                string msg = ex.GetErrorMessage();
            }
            return(false);
        }
Exemple #19
0
        /// <summary>
        /// 查找模板
        /// </summary>
        /// <param name="numfind"> 要查找模板的个数</param>
        /// <returns></returns>
        public List <ModleFindResult> FindMuiltple(uint numfind)
        {
            List <ModleFindResult> resultlist = new List <ModleFindResult>();

            if (m_modleHandle != null)
            {
                try
                {
                    HTuple _column = new HTuple(), _angle = new HTuple(), _score = new HTuple();

                    HOperatorSet.FindShapeModel(m_image, m_modleHandle, m_paramFind.AngleStart, m_paramFind.AngleExtent, m_paramFind.MinScore,
                                                numfind, m_paramFind.MaxOverLap, m_paramFind.SubPixel, m_paramFind.NumLevels, m_paramFind.Greediness, out HTuple _row, out _column, out _angle, out _score);

                    if (_row.TupleLength() > 0)
                    {
                        for (int i = 0; i < _row.Length; i++)
                        {
                            HObject _modlecountor = new HObject();
                            HOperatorSet.GetShapeModelContours(out _modlecountor, m_modleHandle, 1);
                            HTuple _hommat2d;
                            HOperatorSet.VectorAngleToRigid(0, 0, 0, _row.DArr[i], _column.DArr[i], _angle.DArr[i], out _hommat2d);
                            HObject _transcountor;
                            HOperatorSet.AffineTransContourXld(_modlecountor, out _transcountor, _hommat2d);
                            ModleFindResult result = new ModleFindResult();
                            result.Row         = _row.DArr[i];
                            result.Column      = _column.DArr[i];
                            result.Score       = _score.DArr[i];
                            result.Angle       = _angle.DArr[i];
                            result.ModleRegion = _modlecountor;
                            resultlist.Add(result);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return(resultlist);
        }
Exemple #20
0
        public override void AffineTransResult(HTuple HomMat, out LocalResult AffinResult)
        {
            AffinResult = this;
            MyVisionBase.AffineTransPt(row, col, HomMat, out AffinResult.row, out AffinResult.col);
            HTuple sx = new HTuple(), sy = new HTuple(), phi = new HTuple(), theta = new HTuple(), tx = new HTuple(), ty = new HTuple();

            HOperatorSet.HomMat2dToAffinePar(HomMat, out sx, out sy, out phi, out theta, out tx, out ty);
            AffinResult.angle = angle - phi.D;

            HOperatorSet.AffineTransContourXld(this.DetectContour, out ((LineCircRectRlt)AffinResult).DetectContour, HomMat);
            HObject AffineNgCont = new HObject();

            HOperatorSet.GenEmptyObj(out AffineNgCont);

            HObject myCont = new HObject();

            HOperatorSet.AffineTransRegion(this.NgContour, out AffineNgCont, HomMat, "constant");
            HOperatorSet.CopyObj(AffineNgCont, out ((LineCircRectRlt)AffinResult).NgContour, 1, -1);
        }
        void CreateModel(HObject ho_inImage, HObject ho_inRegion, out HObject ho_outContour, out HTuple hv_outModelID)
        {
            HOperatorSet.GenEmptyObj(out ho_outContour);
            HOperatorSet.GenEmptyObj(out HObject ho_TemplateImage);

            HOperatorSet.SetSystem("border_shape_models", "false");
            ho_TemplateImage.Dispose();
            HOperatorSet.ReduceDomain(ho_inImage, ho_inRegion, out ho_TemplateImage);

            HOperatorSet.CreateShapeModel(ho_TemplateImage, 5, (new HTuple(-50)).TupleRad()
                                          , (new HTuple(100)).TupleRad(), (new HTuple(0.2733)).TupleRad(), "auto",
                                          "use_polarity", "auto", "auto", out hv_outModelID);
            HOperatorSet.GetShapeModelContours(out HObject ho_ModelContours, hv_outModelID, 1);
            HOperatorSet.AreaCenter(ho_inRegion, out _, out HTuple hv_RefRow, out HTuple hv_RefColumn);
            HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out HTuple hv_HomMat2D);
            ho_outContour.Dispose();
            HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_outContour, hv_HomMat2D);
            ho_TemplateImage.Dispose();
        }
Exemple #22
0
        private bool FindCorner()
        {
            if (lineParams.ho_Image == null)
            {
                MessageBox.Show("图像为空");
                return(false);
            }
            try
            {
                Show2HWindow(lineParams.ho_Image);
                HOperatorSet.ApplyMetrologyModel(lineParams.ho_Image, lineParams.hv_MetrologyHandle);

                HOperatorSet.GetMetrologyObjectResult(lineParams.hv_MetrologyHandle, "all", "all", "result_type", "row_begin", out lineParams.hv_LineRowBegin);
                HOperatorSet.GetMetrologyObjectResult(lineParams.hv_MetrologyHandle, "all", "all", "result_type", "column_begin", out lineParams.hv_LineColumnBegin);
                HOperatorSet.GetMetrologyObjectResult(lineParams.hv_MetrologyHandle, "all", "all", "result_type", "row_end", out lineParams.hv_LineRowEnd);
                HOperatorSet.GetMetrologyObjectResult(lineParams.hv_MetrologyHandle, "all", "all", "result_type", "column_end", out lineParams.hv_LineColumnEnd);

                if (lineParams.hv_LineRowBegin.Length != 1)
                {
                    MessageBox.Show("直线数量错误!");
                    return(false);
                }

                HObject ho_MeasuredLine;
                HOperatorSet.GenEmptyObj(out ho_MeasuredLine);
                HOperatorSet.GetMetrologyObjectResultContour(out ho_MeasuredLine, lineParams.hv_MetrologyHandle, "all", "all", 1.5);

                HOperatorSet.AffineTransContourXld(ho_MeasuredLine, out ho_MeasuredLine, hv_XldHomMat2D);

                HOperatorSet.SetColor(hv_ImageWindow, "red");
                HOperatorSet.DispObj(ho_MeasuredLine, hv_ImageWindow);

                MessageBox.Show("查找直线模型成功!");
                return(true);
            }
            catch (Exception exc)
            {
                MessageBox.Show("查找直线模型失败!" + exc.ToString());
                return(false);
            }
        }
Exemple #23
0
        public ModleFindResult FindSimple()
        {
            ModleFindResult result = new ModleFindResult();

            if (m_modleHandle != null)
            {
                try
                {
                    HTuple _column = new HTuple(), _angle = new HTuple(), _score = new HTuple();

                    HOperatorSet.FindShapeModel(m_image, m_modleHandle, m_paramFind.AngleStart, m_paramFind.AngleExtent, m_paramFind.MinScore,
                                                1, m_paramFind.MaxOverLap, m_paramFind.SubPixel, m_paramFind.NumLevels, m_paramFind.Greediness, out HTuple _row, out _column, out _angle, out _score);

                    if (_row.TupleLength() > 0)
                    {
                        HObject _modlecountor = new HObject();
                        HOperatorSet.GetShapeModelContours(out _modlecountor, m_modleHandle, 1);
                        HTuple _hommat2d;
                        HOperatorSet.VectorAngleToRigid(0, 0, 0, _row.D, _column.D, _angle.D, out _hommat2d);
                        HObject _transcountor;
                        HOperatorSet.AffineTransContourXld(_modlecountor, out _transcountor, _hommat2d);
                        result.Row         = _row.D;
                        result.Column      = _column.D;
                        result.Score       = _score.D;
                        result.Angle       = _angle.D;
                        result.ModleRegion = _modlecountor;
                        return(result);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return(result);
        }
Exemple #24
0
        private void btn_GrabTraget_Click(object sender, EventArgs e)
        {
            if (m_modleHandle != null)
            {
                try
                {
                    HTuple _column = new HTuple(), _angle = new HTuple(), _score = new HTuple();

                    HOperatorSet.FindShapeModel(m_image, m_modleHandle, m_paramFind.AngleStart, m_paramFind.AngleExtent, m_paramFind.MinScore,
                                                m_paramFind.NumMatch, m_paramFind.MaxOverLap, m_paramFind.SubPixel, m_paramFind.NumLevels, m_paramFind.Greediness, out HTuple _row, out _column, out _angle, out _score);
                    hDisplay1.HRegionXList = null;
                    if (_row.TupleLength() > 0)
                    {
                        List <RegionX> lr = new List <RegionX>();
                        for (int i = 0; i < _row.TupleLength(); i++)
                        {
                            HObject _modlecountor = new HObject();
                            HOperatorSet.GetShapeModelContours(out _modlecountor, m_modleHandle, 1);
                            HTuple _hommat2d;
                            HOperatorSet.VectorAngleToRigid(0, 0, 0, _row.DArr[i], _column.DArr[i], _angle.DArr[i], out _hommat2d);
                            HObject _transcountor;
                            HOperatorSet.AffineTransContourXld(_modlecountor, out _transcountor, _hommat2d);
                            HalWindow.RegionX regionX = new HalWindow.RegionX(_transcountor, "green");
                            lr.Add(regionX);
                        }
                        hDisplay1.HImageX      = m_image;
                        hDisplay1.HRegionXList = lr;
                    }
                    else
                    {
                        hDisplay1.HImageX      = m_image;
                        hDisplay1.HRegionXList = null;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "异常");
                }
            }
        }
Exemple #25
0
        private void btn_TrainModle_Click(object sender, EventArgs e)
        {
            if (hDisplay1.GetTrainRegions().Count == 0)
            {
                MessageBox.Show("请先设置模板区域", "提示");
                return;
            }
            try
            {
                HObject _imagereduced = new HObject();
                HOperatorSet.ReduceDomain(m_image, hDisplay1.GetTrainRegions().ElementAt(0), out _imagereduced);
                HOperatorSet.CreateShapeModel(_imagereduced, m_paramTrain.NumLevels, m_paramTrain.AngleStart, m_paramTrain.AngleExtent,
                                              m_paramTrain.AngleStep, m_paramTrain.Optimization, m_paramTrain.Metric, m_paramTrain.Contrast, m_paramTrain.MinContrast, out m_modleHandle);

                HTuple _column = new HTuple(), _angle = new HTuple(), _score = new HTuple();

                HOperatorSet.FindShapeModel(_imagereduced, m_modleHandle, m_paramFind.AngleStart, m_paramFind.AngleExtent, m_paramFind.MinScore,
                                            m_paramFind.NumMatch, m_paramFind.MaxOverLap, m_paramFind.SubPixel, m_paramFind.NumLevels, m_paramFind.Greediness, out HTuple _row, out _column, out _angle, out _score);
                HOperatorSet.WriteShapeModel(m_modleHandle, AppDomain.CurrentDomain.BaseDirectory + "123.shm");
                if (_row.TupleLength() > 0)
                {
                    HObject _modlecountor = new HObject();
                    HOperatorSet.GetShapeModelContours(out _modlecountor, m_modleHandle, 1);
                    HTuple _hommat2d;
                    HOperatorSet.VectorAngleToRigid(0, 0, 0, _row.D, _column.D, _angle.D, out _hommat2d);
                    HObject _transcountor;
                    HOperatorSet.AffineTransContourXld(_modlecountor, out _transcountor, _hommat2d);
                    HalWindow.RegionX regionX = new HalWindow.RegionX(_transcountor, "green");
                    hDisplay1.HRegionXList = new List <RegionX>()
                    {
                        regionX
                    };
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常");
            }
        }
Exemple #26
0
 /// <summary>
 /// 创建模板
 /// </summary>
 public static void CreateModel(HObject InImage, HObject hRegion, out HTuple hv_ModelID, out HObject ho_TransContours)
 {
     HOperatorSet.GenEmptyObj(out ho_TransContours);
     ho_TransContours.Dispose();
     try
     {
         HOperatorSet.ReduceDomain(InImage, hRegion, out HObject ho_TemplateImage);
         HOperatorSet.CreateShapeModel(ho_TemplateImage, 7, new HTuple(0).TupleRad()
                                       , (new HTuple(360)).TupleRad(), (new HTuple(0.1919)).TupleRad(), (new HTuple("point_reduction_high")).TupleConcat(
                                           "no_pregeneration"), "use_polarity", ((new HTuple(16)).TupleConcat(30)).TupleConcat(
                                           11), 3, out hv_ModelID);
         HOperatorSet.GetShapeModelContours(out HObject ho_ModelContours, hv_ModelID, 1);
         HOperatorSet.AreaCenter(hRegion, out HTuple hv_ModelRegionArea, out HTuple hv_RefRow,
                                 out HTuple hv_RefColumn);
         HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out HTuple hv_HomMat2D);
         HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat2D);
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Exemple #27
0
        private string findModel(int ModelNum)
        {
            if (MyCurCom.ho_Image == null)
            {
                return("-1,-1,-1");
            }
            HTuple hv_RowCheck = null, hv_ColumnCheck = null, hv_AngleCheck = null, hv_Score = null;

            HOperatorSet.FindShapeModel(MyCurCom.ho_Image, MyModel[ModelNum].hv_ModelID, -0.39, 0.79, 0.5, 1, 0.5, "least_squares", 0, 0.7, out hv_RowCheck, out hv_ColumnCheck, out hv_AngleCheck, out hv_Score);
            HTuple  HomMat2D;
            HObject ModelAtNewPosition, ROIAffinTrans;
            int     CheckNum = (int)((new HTuple(hv_Score.TupleLength())));

            if (CheckNum > 0)
            {
                MyCurCom.hv.DispObj(MyCurCom.ho_Image);

                MyCurCom.hv.SetColor("yellow");
                HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RowCheck[0].D, hv_ColumnCheck[0].D, hv_AngleCheck[0].D, out HomMat2D);
                HOperatorSet.AffineTransContourXld(MyModel[ModelNum].ho_ShapeModel, out ModelAtNewPosition, HomMat2D);
                MyCurCom.hv.DispObj(ModelAtNewPosition);

                MyCurCom.hv.SetColor("blue");
                HOperatorSet.VectorAngleToRigid(MyModel[ModelNum].hv_Orgin_Row, MyModel[ModelNum].hv_Orgin_Column, 0, hv_RowCheck[0].D, hv_ColumnCheck[0].D, hv_AngleCheck[0].D, out HomMat2D);
                HOperatorSet.AffineTransRegion(MyModel[ModelNum].h_roi, out ROIAffinTrans, HomMat2D, "constant");
                MyCurCom.hv.DispObj(ROIAffinTrans);

                MyCurCom.hv.SetColor("green");
                HOperatorSet.DispCross(MyCurCom.hv, hv_RowCheck[0].D, hv_ColumnCheck[0].D, MyCurCom.ho_Width / 24, hv_AngleCheck[0].D);
                return(hv_RowCheck[0].D.ToString() + "," + hv_ColumnCheck[0].D.ToString() + "," + hv_AngleCheck[0].D.ToString());
            }
            else
            {
                return("-1,-1,-1");
            }
        }
    public void Matching(HObject Image)
    {
        List <coordinate> Lctest = new List <coordinate>();

        HOperatorSet.GenEmptyObj(out ho_ModelContours);
        HOperatorSet.GenEmptyObj(out ho_TransContours);

        HOperatorSet.CreateShapeModel(Image, 2, (new HTuple(0)).TupleRad(),
                                      (new HTuple(360)).TupleRad(), (new HTuple(6.2177)).TupleRad(), (new HTuple("none")).TupleConcat(
                                          "no_pregeneration"), "use_polarity", ((new HTuple(10)).TupleConcat(13)).TupleConcat(4), 10, out hv_ModelId);

        ho_ModelContours.Dispose();
        HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelId, 1);

        HOperatorSet.FindShapeModel(ho_Dog, hv_ModelId, (new HTuple(0)).TupleRad(),
                                    (new HTuple(360)).TupleRad(), MatchingParameters.Score, 0, 0.5, "least_squares", (new HTuple(2)).TupleConcat(
                                        1), 0.9, out hv_ModelRow, out hv_ModelColumn, out hv_ModelAngle, out hv_ModelScore);

        hv_Deg = hv_ModelAngle.TupleDeg();

        HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red");
        HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 3);

        int totalcount = hv_ModelScore.TupleLength();


        HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");

        HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);

        for (hv_MatchingObjIdx = 0; (int)hv_MatchingObjIdx <= totalcount - 1; hv_MatchingObjIdx = (int)hv_MatchingObjIdx + 1)
        {
            MainWindow.cd.Add(new coordinate
            {
                Number = hv_MatchingObjIdx + 1,
                X      = hv_ModelRow.TupleSelect(hv_MatchingObjIdx),
                Y      = hv_ModelColumn.TupleSelect(hv_MatchingObjIdx),
                Angle  = hv_Deg.TupleSelect(hv_MatchingObjIdx),
            });

            Lctest.Add(new coordinate
            {
                Number = hv_MatchingObjIdx + 1,
                X      = hv_ModelRow.TupleSelect(hv_MatchingObjIdx),
                Y      = hv_ModelColumn.TupleSelect(hv_MatchingObjIdx),
                Angle  = hv_Deg.TupleSelect(hv_MatchingObjIdx),
            });
        }

        foreach (coordinate hs in Lctest)
        {
        }
        for (int i = 0; i < totalcount; i++)
        {
            // HOperatorSet.DispCircle(hv_ExpDefaultWinHandle, hv_ModelRow.TupleSelect(i), hv_ModelColumn.TupleSelect(i), 10);
            //  HOperatorSet.GenCircle(hv_ExpDefaultWinHandle, hv_ModelRow.TupleSelect(i), hv_ModelColumn.TupleSelect(i), 10);
        }

        for (hv_MatchingObjIdx = 0; (int)hv_MatchingObjIdx <= totalcount - 1; hv_MatchingObjIdx = (int)hv_MatchingObjIdx + 1)
        {
            HOperatorSet.DispCross(hv_ExpDefaultWinHandle, hv_ModelRow.TupleSelect(hv_MatchingObjIdx),
                                   hv_ModelColumn.TupleSelect(hv_MatchingObjIdx), 10, hv_ModelAngle.TupleSelect(hv_MatchingObjIdx));

            HOperatorSet.HomMat2dIdentity(out hv_HomMat);
            HOperatorSet.HomMat2dRotate(hv_HomMat, hv_ModelAngle.TupleSelect(hv_MatchingObjIdx),
                                        0, 0, out hv_HomMat);
            HOperatorSet.HomMat2dTranslate(hv_HomMat, hv_ModelRow.TupleSelect(hv_MatchingObjIdx),
                                           hv_ModelColumn.TupleSelect(hv_MatchingObjIdx), out hv_HomMat);
            ho_TransContours.Dispose();
            HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat);



            //HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);
            //HOperatorSet.DispObj(ho_Dog, hv_ExpDefaultWinHandle);

            HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill");


            HOperatorSet.DispObj(ho_TransContours, hv_ExpDefaultWinHandle);


            HOperatorSet.SetWindowParam(hv_ExpDefaultWinHandle, "flush", "true");

            HOperatorSet.FlushBuffer(hv_ExpDefaultWinHandle);
        }



        //Matching 01: Clear model when done
        HOperatorSet.ClearShapeModel(hv_ModelId);
        //Matching 01: END of generated code for model application
    }
    public HTuple process_image(HObject ho_Image, out HObject ho_EdgeAmplitude, HTuple hv_WindowHandle,
                                HTuple hv_DrawID)
    {
        // Local iconic variables
        HObject ho_Region, ho_ImageReduced;

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_EdgeAmplitude);
        HOperatorSet.GenEmptyObj(out ho_Region);
        HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        HOperatorSet.GenEmptyObj(out ho_ModelContours);
        HOperatorSet.GenEmptyObj(out ho_TransContours);
        ho_TransContours.Dispose();
        try
        {
            //Apply an Sobel edge filter on the background
            //image within the region of interest defined
            //by the drawing object.
            ho_Region.Dispose();
            HOperatorSet.GetDrawingObjectIconic(out ho_Region, hv_DrawID);
            ho_ImageReduced.Dispose();
            ho_EdgeAmplitude.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_Region, out ho_EdgeAmplitude);

            try
            {
                HOperatorSet.CreateShapeModel(ho_EdgeAmplitude, 2, (new HTuple(0)).TupleRad(),
                                              (new HTuple(360)).TupleRad(), (new HTuple(6.2177)).TupleRad(), (new HTuple("none")).TupleConcat(
                                                  "no_pregeneration"), "use_polarity", ((new HTuple(10)).TupleConcat(13)).TupleConcat(4), 10, out hv_ModelId);
            }
            catch (Exception ex)
            {
                return(null);
            }
            try
            {
                ho_ModelContours.Dispose();
                HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelId, 1);
            }
            catch (Exception ex)
            {
                return(null);
            }
            try
            {
                HOperatorSet.SetColor(hv_WindowHandle, "blue");
                HOperatorSet.SetLineWidth(hv_WindowHandle, 1);



                HOperatorSet.HomMat2dIdentity(out hv_HomMat);
                HOperatorSet.HomMat2dRotate(hv_HomMat, 0, 0, 0, out hv_HomMat);

                HOperatorSet.HomMat2dTranslate(hv_HomMat, 100, 100, out hv_HomMat);


                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat);



                HOperatorSet.SetWindowParam(hv_WindowHandle, "flush", "false");
                HOperatorSet.ClearWindow(hv_WindowHandle);
                HOperatorSet.DispObj(ho_TransContours, hv_WindowHandle);
                HOperatorSet.SetWindowParam(hv_WindowHandle, "flush", "true");
                HOperatorSet.FlushBuffer(hv_WindowHandle);

                return(hv_ModelId);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return(null);
            }
            ho_Region.Dispose();
            ho_ImageReduced.Dispose();

            return(null);
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_Region.Dispose();
            ho_ImageReduced.Dispose();
            throw HDevExpDefaultException;
        }
    }
        public static void FindFeatureModel(HObject ho_Image, HTuple hv_AngleStart, HTuple hv_AngleExtent,
                                            HTuple hv_AngleStep, HTuple hv_ScaleMin, HTuple hv_ScaleMax, HTuple hv_MinScore,
                                            HTuple hv_NumMatches, HTuple hv_ModelId, out HTuple hv_XldRowsM, out HTuple hv_XldColsM,
                                            out HTuple hv_XldPointCountsM, out HTuple hv_ModelRow, out HTuple hv_ModelColumn,
                                            out HTuple hv_ModelAngle, out HTuple hv_ModelScale, out HTuple hv_ModelScore)
        {
            // Local iconic variables
            HObject ho_ModelContours, ho_TransContours = null;
            HObject ho_ObjectSelected = null;

            // Local control variables

            HTuple hv_AngleExtentOut = null, hv_AngleStartOut = null;
            HTuple hv_AngleStepOut = null, hv_MatchingObjIdx = null;
            HTuple hv_HomMat = new HTuple(), hv_Num = new HTuple();
            HTuple hv_Index = new HTuple(), hv_Rows = new HTuple();
            HTuple hv_Cols = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ModelContours);
            HOperatorSet.GenEmptyObj(out ho_TransContours);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            hv_AngleExtentOut  = hv_AngleExtent.Clone();
            hv_AngleStartOut   = hv_AngleStart.Clone();
            hv_AngleStepOut    = hv_AngleStep.Clone();
            hv_AngleStartOut   = hv_AngleStartOut.TupleRad();
            hv_AngleExtentOut  = hv_AngleExtentOut.TupleRad();
            hv_AngleStepOut    = hv_AngleStepOut.TupleRad();
            hv_XldRowsM        = new HTuple();
            hv_XldColsM        = new HTuple();
            hv_XldPointCountsM = new HTuple();
            HOperatorSet.FindScaledShapeModel(ho_Image, hv_ModelId, hv_AngleStartOut, hv_AngleExtentOut,
                                              hv_ScaleMin, hv_ScaleMax, hv_MinScore, hv_NumMatches, 0.5, "least_squares",
                                              (new HTuple(5)).TupleConcat(1), 0.75, out hv_ModelRow, out hv_ModelColumn,
                                              out hv_ModelAngle, out hv_ModelScale, out hv_ModelScore);//1120.632
            ho_ModelContours.Dispose();
            HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelId, 1);
            for (hv_MatchingObjIdx = 0; (int)hv_MatchingObjIdx <= (int)((new HTuple(hv_ModelScore.TupleLength()
                                                                                    )) - 1); hv_MatchingObjIdx = (int)hv_MatchingObjIdx + 1)
            {
                HOperatorSet.HomMat2dIdentity(out hv_HomMat);
                HOperatorSet.HomMat2dScale(hv_HomMat, hv_ModelScale.TupleSelect(hv_MatchingObjIdx),
                                           hv_ModelScale.TupleSelect(hv_MatchingObjIdx), 0, 0, out hv_HomMat);
                HOperatorSet.HomMat2dRotate(hv_HomMat, hv_ModelAngle.TupleSelect(hv_MatchingObjIdx),
                                            0, 0, out hv_HomMat);
                HOperatorSet.HomMat2dTranslate(hv_HomMat, hv_ModelRow.TupleSelect(hv_MatchingObjIdx),
                                               hv_ModelColumn.TupleSelect(hv_MatchingObjIdx), out hv_HomMat);
                ho_TransContours.Dispose();
                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours,
                                                   hv_HomMat);

                HOperatorSet.CountObj(ho_TransContours, out hv_Num);
                HTuple end_val19  = hv_Num;
                HTuple step_val19 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val19, step_val19); hv_Index = hv_Index.TupleAdd(step_val19))
                {
                    ho_ObjectSelected.Dispose();
                    HOperatorSet.SelectObj(ho_TransContours, out ho_ObjectSelected, hv_Index);
                    HOperatorSet.GetContourXld(ho_ObjectSelected, out hv_Rows, out hv_Cols);
                    hv_XldRowsM        = hv_XldRowsM.TupleConcat(hv_Rows);
                    hv_XldColsM        = hv_XldColsM.TupleConcat(hv_Cols);
                    hv_XldPointCountsM = hv_XldPointCountsM.TupleConcat(new HTuple(hv_Rows.TupleLength()
                                                                                   ));
                }
            }
            ho_ModelContours.Dispose();
            ho_TransContours.Dispose();
            ho_ObjectSelected.Dispose();//1120.224
            return;
        }