예제 #1
0
        // Local procedures
        public PointH GetMarkCenter(HObject ho_Image)
        {
            // Local iconic variables
            HObject ho_Regions, ho_Regions1, ho_RegionClosing, ho_RegionOpening;
            HObject ho_ConnectedRegions, ho_SelectedRegions2, ho_RegionFillUp1, ho_RegionFillUp2;
            HObject ho_SelectedRegions1;

            // Local control variables
            HTuple hv_AreaMax = null;
            HTuple hv_Y, hv_X;
            HTuple hv_Width, hv_Height, hv_Area;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_Regions1);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp1);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp2);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);

            try
            {
                //Find by region
                ho_Regions.Dispose();
                HOperatorSet.AutoThreshold(ho_Image, out ho_Regions, 2);
                //ho_RegionClosing.Dispose();
                //HOperatorSet.ClosingCircle(ho_Regions, out ho_RegionClosing, 3.5);
                //ho_RegionOpening.Dispose();
                //HOperatorSet.OpeningCircle(ho_RegionClosing, out ho_RegionOpening, 3.5);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);
                ho_RegionFillUp1.Dispose();
                HOperatorSet.FillUp(ho_ConnectedRegions, out ho_RegionFillUp1);
                ho_SelectedRegions2.Dispose();
                HOperatorSet.SelectShape(ho_RegionFillUp1, out ho_SelectedRegions2, "area", "and", hv_MinAreaMark, hv_MaxAreaMark);
                ho_RegionFillUp2.Dispose();
                HOperatorSet.FillUp(ho_SelectedRegions2, out ho_RegionFillUp2);

                ho_RegionOpening.Dispose();
                HOperatorSet.ClosingCircle(ho_RegionFillUp2, out ho_RegionOpening, 3.5);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningCircle(ho_RegionFillUp2, out ho_RegionOpening, 3.5);
                ho_RegionFillUp1.Dispose();
                HOperatorSet.FillUp(ho_RegionOpening, out ho_RegionFillUp1);

                ho_SelectedRegions1.Dispose();
                HOperatorSet.SelectShapeStd(ho_RegionFillUp1, out ho_SelectedRegions1, "max_area", 70);
                HOperatorSet.AreaCenter(ho_SelectedRegions1, out hv_AreaMax, out hv_Y, out hv_X);
                HOperatorSet.RegionFeatures(ho_SelectedRegions1, "width", out hv_Width);
                HOperatorSet.RegionFeatures(ho_SelectedRegions1, "height", out hv_Height);
                HOperatorSet.RegionFeatures(ho_SelectedRegions1, "area", out hv_Area);
                HOperatorSet.SetColor(hv_DispWindown, "red");
                HalconHelp halconHelp = new HalconHelp();
                string     strArea    = "";
                for (int i = 0; i < hv_Area.Length; i++)
                {
                    strArea += hv_Area[i].D.ToString() + ",";
                }
                halconHelp.disp_message(hv_DispWindown, "Area:" + strArea.TrimEnd(','), "image", 10, 1000, "red", "true");
                if (hv_X.Length <= 0 || hv_Y.Length <= 0)
                {
                    //未找到Mark点
                    return(new PointH());
                }
                double dd = hv_Width[0].D - hv_Height[0].D;
                if (Math.Abs(dd) > 3)
                {
                    //Mark点在视野的边界
                    return(new PointH());
                }
                HOperatorSet.DispObj(ho_SelectedRegions1, hv_DispWindown);
                return(new PointH(hv_X[0], hv_Y[0]));
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Regions.Dispose();
                ho_RegionClosing.Dispose();
                ho_RegionOpening.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionFillUp2.Dispose();
                ho_SelectedRegions1.Dispose();

                throw HDevExpDefaultException;
            }
            finally
            {
                ho_Regions.Dispose();
                ho_RegionClosing.Dispose();
                ho_RegionOpening.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionFillUp2.Dispose();
                ho_SelectedRegions1.Dispose();
            }
        }
예제 #2
0
        public PointH GetMarkCenterXLD(HObject ho_Image)
        {
            // Local iconic variables
            HObject ho_Regions, ho_Regions1, ho_RegionClosing, ho_RegionOpening;
            HObject ho_ConnectedRegions, ho_SelectedRegions2, ho_RegionFillUp1, ho_RegionFillUp2;
            HObject ho_SelectedRegions1;
            HObject ho_ROIOuter, ho_ROIInner, ho_ROI, ho_ROIEdges, ho_RimReduced;
            HObject ho_Edges, ho_ContoursSplit, ho_RelEdges, ho_UnionContours;
            HObject ho_ContCircle1;
            HTuple  hv_Number = null, hv_Row3 = null, hv_Column3 = null;
            HTuple  hv_Radius2 = null, hv_StartPhi2 = null, hv_EndPhi2 = null;
            HTuple  hv_PointOrder3 = null;

            // Local control variables
            HTuple hv_AreaMax = null;
            HTuple hv_Y, hv_X;
            HTuple hv_Width, hv_Height, hv_Area;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_Regions1);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp1);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp2);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);

            HOperatorSet.GenEmptyObj(out ho_ROIOuter);
            HOperatorSet.GenEmptyObj(out ho_ROIInner);
            HOperatorSet.GenEmptyObj(out ho_ROI);
            HOperatorSet.GenEmptyObj(out ho_ROIEdges);
            HOperatorSet.GenEmptyObj(out ho_RimReduced);
            HOperatorSet.GenEmptyObj(out ho_Edges);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_RelEdges);
            HOperatorSet.GenEmptyObj(out ho_UnionContours);
            HOperatorSet.GenEmptyObj(out ho_ContCircle1);

            try
            {
                //Find by region
                ho_Regions.Dispose();
                HOperatorSet.AutoThreshold(ho_Image, out ho_Regions, 2);
                //ho_RegionClosing.Dispose();
                //HOperatorSet.ClosingCircle(ho_Regions, out ho_RegionClosing, 3.5);
                //ho_RegionOpening.Dispose();
                //HOperatorSet.OpeningCircle(ho_RegionClosing, out ho_RegionOpening, 3.5);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);
                ho_RegionFillUp1.Dispose();
                HOperatorSet.FillUp(ho_ConnectedRegions, out ho_RegionFillUp1);
                ho_SelectedRegions2.Dispose();
                HOperatorSet.SelectShape(ho_RegionFillUp1, out ho_SelectedRegions2, "area", "and", hv_MinAreaMark, hv_MaxAreaMark);
                ho_RegionFillUp2.Dispose();
                HOperatorSet.FillUp(ho_SelectedRegions2, out ho_RegionFillUp2);

                ho_RegionOpening.Dispose();
                HOperatorSet.ClosingCircle(ho_RegionFillUp2, out ho_RegionOpening, 3.5);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningCircle(ho_RegionFillUp2, out ho_RegionOpening, 3.5);
                ho_RegionFillUp1.Dispose();
                HOperatorSet.FillUp(ho_RegionOpening, out ho_RegionFillUp1);

                ho_SelectedRegions1.Dispose();
                HOperatorSet.SelectShapeStd(ho_RegionFillUp1, out ho_SelectedRegions1, "max_area", 70);
                HOperatorSet.AreaCenter(ho_SelectedRegions1, out hv_AreaMax, out hv_Y, out hv_X);
                HOperatorSet.RegionFeatures(ho_SelectedRegions1, "width", out hv_Width);
                HOperatorSet.RegionFeatures(ho_SelectedRegions1, "height", out hv_Height);
                HOperatorSet.RegionFeatures(ho_SelectedRegions1, "area", out hv_Area);
                HalconHelp halconHelp = new HalconHelp();
                HOperatorSet.SetColor(hv_DispWindown, "red");
                string strArea = "";
                for (int i = 0; i < hv_Area.Length; i++)
                {
                    strArea += hv_Area[i].D.ToString() + ",";
                }
                halconHelp.disp_message(hv_DispWindown, "Area:" + strArea.TrimEnd(','), "image", 10, 1000, "red", "true");
                //HOperatorSet.SetColor(hv_DispWindown, "red");
                if (hv_X.Length <= 0 || hv_Y.Length <= 0)
                {
                    //未找到Mark点
                    return(new PointH());
                }
                double dd = hv_Width[0].D - hv_Height[0].D;
                if (Math.Abs(dd) > 9)
                {
                    //Mark点在视野的边界
                    return(new PointH());
                }


                //Find by XLD
                HOperatorSet.DilationCircle(ho_SelectedRegions1, out ho_ROIOuter, 8.5);
                ho_ROIInner.Dispose();
                HOperatorSet.ErosionCircle(ho_SelectedRegions1, out ho_ROIInner, 8.5);
                ho_ROI.Dispose();
                HOperatorSet.Difference(ho_ROIOuter, ho_ROIInner, out ho_ROI);
                ho_ROIEdges.Dispose();
                HOperatorSet.Union1(ho_ROI, out ho_ROIEdges);
                //Reduce the region of interest (domain) to the extracted
                //regions containing the edges.
                ho_RimReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_ROIEdges, out ho_RimReduced);
                //Extract subpixel precise edges
                ho_Edges.Dispose();
                HOperatorSet.EdgesSubPix(ho_RimReduced, out ho_Edges, "canny", 4, 20, 40);
                //分割线段
                ho_ContoursSplit.Dispose();
                HOperatorSet.SegmentContoursXld(ho_Edges, out ho_ContoursSplit, "lines_circles", 5, 4, 2);
                //Select only the contours with length larger than 30 pixels
                ho_RelEdges.Dispose();
                HOperatorSet.SelectContoursXld(ho_ContoursSplit, out ho_RelEdges, "length", 30, 999999, 0, 0);
                ho_UnionContours.Dispose();
                HOperatorSet.UnionAdjacentContoursXld(ho_RelEdges, out ho_UnionContours, 100, 1, "attr_keep");
                HOperatorSet.CountObj(ho_UnionContours, out hv_Number);
                if ((int)(new HTuple(hv_Number.TupleGreater(1))) != 0)
                {
                    //未找到Mark点
                    return(new PointH());
                }
                HOperatorSet.FitCircleContourXld(ho_UnionContours, "algebraic", -1, 0, 0, 3, 2,
                                                 out hv_Row3, out hv_Column3, out hv_Radius2, out hv_StartPhi2, out hv_EndPhi2, out hv_PointOrder3);
                ho_ContCircle1.Dispose();
                HOperatorSet.GenCircleContourXld(out ho_ContCircle1, hv_Row3, hv_Column3, hv_Radius2, 0, 6.28318, "positive", 1);

                HOperatorSet.SetColor(hv_DispWindown, "red");
                HOperatorSet.DispCircle(hv_DispWindown, hv_Row3, hv_Column3, hv_Radius2);
                //HOperatorSet.SetLineWidth(hv_DispWindown, 3);
                //HOperatorSet.DispObj(ho_ContCircle1, hv_DispWindown);
                return(new PointH(hv_Column3[0], hv_Row3[0]));
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Regions.Dispose();
                ho_RegionClosing.Dispose();
                ho_RegionOpening.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionFillUp2.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_ROIOuter.Dispose();
                ho_ROIInner.Dispose();
                ho_ROI.Dispose();
                ho_ROIEdges.Dispose();
                ho_RimReduced.Dispose();
                ho_Edges.Dispose();
                ho_ContoursSplit.Dispose();
                ho_RelEdges.Dispose();
                ho_UnionContours.Dispose();
                ho_ContCircle1.Dispose();
                throw HDevExpDefaultException;
            }
            finally
            {
                ho_Regions.Dispose();
                ho_RegionClosing.Dispose();
                ho_RegionOpening.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionFillUp2.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_ROIOuter.Dispose();
                ho_ROIInner.Dispose();
                ho_ROI.Dispose();
                ho_ROIEdges.Dispose();
                ho_RimReduced.Dispose();
                ho_Edges.Dispose();
                ho_ContoursSplit.Dispose();
                ho_RelEdges.Dispose();
                ho_UnionContours.Dispose();
                ho_ContCircle1.Dispose();
            }
        }