Example #1
0
        private bool FitLine(List <HTuple> hv_RowEdges, List <HTuple> hv_ColumnEdges)
        {
            if (hv_RowEdges.Count < 2 || hv_RowEdges.Count != hv_ColumnEdges.Count)
            {
                //MessageBox.Show("样本点数据错误");
                return(false);
            }
            HTuple hv_Mx = null;
            HTuple hv_My = null;

            for (int i = 0; i < hv_RowEdges.Count; i++)
            {
                //HOperatorSet.TupleInsert(hv_Mx,i, hv_RowEdges[i],out hv_Mx);
                //HOperatorSet.TupleInsert(hv_My, i, hv_ColumnEdges[i], out hv_My);

                HOperatorSet.TupleInsert(hv_My, i, hv_RowEdges[i], out hv_My);
                HOperatorSet.TupleInsert(hv_Mx, i, hv_ColumnEdges[i], out hv_Mx);
            }
            HTuple hv_x = null;
            HTuple hv_y = null;

            HOperatorSet.CreateMatrix(hv_RowEdges.Count, 2, 1, out hv_x);
            HOperatorSet.CreateMatrix(hv_RowEdges.Count, 1, hv_My, out hv_y);

            HOperatorSet.SetValueMatrix(hv_x, HTuple.TupleGenSequence(0, hv_RowEdges.Count - 1, 1), HTuple.TupleGenConst(hv_RowEdges.Count, 0), hv_Mx);

            HTuple hv_xtx = null;
            HTuple hv_xty = null;

            HOperatorSet.MultMatrix(hv_x, hv_x, "ATB", out hv_xtx);
            HOperatorSet.MultMatrix(hv_x, hv_y, "ATB", out hv_xty);

            HTuple hv_invxtx = null;

            HOperatorSet.InvertMatrix(hv_xtx, "general", 0, out hv_invxtx);

            HTuple hv_beta = null;

            HOperatorSet.MultMatrix(hv_invxtx, hv_xty, "AB", out hv_beta);

            HTuple hv_Values = null;

            HOperatorSet.GetFullMatrix(hv_beta, out hv_Values);

            HTuple hv_Newy = null;

            hv_Newy = ((hv_Values.TupleSelect(0)) * (new HTuple(10)).TupleConcat(800)) + (hv_Values.TupleSelect(1));


            HObject ho_Contour;

            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_Newy, (new HTuple(10)).TupleConcat(800));


            HTuple hv_XldHomMat2D;

            HOperatorSet.HomMat2dIdentity(out hv_XldHomMat2D);

            HOperatorSet.HomMat2dScale(hv_XldHomMat2D, hv_ZoomFactor, hv_ZoomFactor, 0, 0, out hv_XldHomMat2D);

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

            HOperatorSet.DispObj(ho_Contour, hv_ImageWindow);
            //HOperatorSet.AffineTransPoint2d();
            return(true);
        }
Example #2
0
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image, ho_ModelRegion, ho_TemplateImage;
        HObject ho_ModelContours, ho_RectifiedImage = null, ho_Regions = null;
        HObject ho_ConnectedRegions = null, ho_SelectedRegions = null;
        HObject ho_Contours = null, ho_Regions1 = null, ho_RegionLines = null;

        // Local control variables

        HTuple hv_ModelID = new HTuple(), hv_ModelRegionArea = new HTuple();
        HTuple hv_RefRow = new HTuple(), hv_RefColumn = new HTuple();
        HTuple hv_TestImages = new HTuple(), hv_T = new HTuple();
        HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
        HTuple hv_Angle = new HTuple(), hv_Score = new HTuple();
        HTuple hv_I = new HTuple(), hv_RectificationHomMat2D = new HTuple();
        HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
        HTuple hv_Radius = new HTuple(), hv_MetrologyHandle = new HTuple();
        HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
        HTuple hv_Index = new HTuple(), hv_Row3 = new HTuple();
        HTuple hv_Column3 = new HTuple(), hv_Parameter = new HTuple();
        HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
        HTuple hv_Radius1 = new HTuple(), hv_MetrologyHandle1 = new HTuple();
        HTuple hv_Index1 = new HTuple(), hv_Parameter1 = new HTuple();
        HTuple hv_SmlC = new HTuple(), hv_SR = new HTuple(), hv_SC = new HTuple();
        HTuple hv_BR = new HTuple(), hv_BC = new HTuple(), hv_Distance = new HTuple();

        // 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_RectifiedImage);
        HOperatorSet.GenEmptyObj(out ho_Regions);
        HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
        HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
        HOperatorSet.GenEmptyObj(out ho_Contours);
        HOperatorSet.GenEmptyObj(out ho_Regions1);
        HOperatorSet.GenEmptyObj(out ho_RegionLines);
        //
        //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, "C:/Users/Chanru/Desktop/Class/bd/bd1.png");
        //
        //Matching 01: Build the ROI from basic regions
        ho_ModelRegion.Dispose();
        HOperatorSet.GenRectangle1(out ho_ModelRegion, 445.809, 637.592, 801.311, 1070.62);
        //
        //Matching 01: Reduce the model template
        ho_TemplateImage.Dispose();
        HOperatorSet.ReduceDomain(ho_Image, ho_ModelRegion, out ho_TemplateImage);
        //
        //Matching 01: Create the shape model
        hv_ModelID.Dispose();
        HOperatorSet.CreateShapeModel(ho_TemplateImage, 6, (new HTuple(0)).TupleRad()
                                      , (new HTuple(360)).TupleRad(), (new HTuple(0.4233)).TupleRad(), (new HTuple("point_reduction_medium")).TupleConcat(
                                          "no_pregeneration"), "use_polarity", ((new HTuple(10)).TupleConcat(14)).TupleConcat(
                                          35), 3, 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: Get the reference position
        hv_ModelRegionArea.Dispose(); hv_RefRow.Dispose(); hv_RefColumn.Dispose();
        HOperatorSet.AreaCenter(ho_ModelRegion, out hv_ModelRegionArea, out hv_RefRow,
                                out hv_RefColumn);
        //
        //Matching 01: END of generated code for model initialization
        //Matching 01:  * * * * * * * * * * * * * * * * * * * * * * *
        //Matching 01: BEGIN of generated code for model application
        //
        //Matching 01: Loop over all specified test images
        hv_TestImages.Dispose();
        hv_TestImages    = new HTuple();
        hv_TestImages[0] = "C:/Users/Chanru/Desktop/Class/bd/bd1.png";
        hv_TestImages[1] = "C:/Users/Chanru/Desktop/Class/bd/bd10.png";
        hv_TestImages[2] = "C:/Users/Chanru/Desktop/Class/bd/bd2.png";
        hv_TestImages[3] = "C:/Users/Chanru/Desktop/Class/bd/bd3.png";
        hv_TestImages[4] = "C:/Users/Chanru/Desktop/Class/bd/bd4.png";
        hv_TestImages[5] = "C:/Users/Chanru/Desktop/Class/bd/bd5.png";
        hv_TestImages[6] = "C:/Users/Chanru/Desktop/Class/bd/bd6.png";
        hv_TestImages[7] = "C:/Users/Chanru/Desktop/Class/bd/bd7.png";
        hv_TestImages[8] = "C:/Users/Chanru/Desktop/Class/bd/bd8.png";
        hv_TestImages[9] = "C:/Users/Chanru/Desktop/Class/bd/bd9.png";
        for (hv_T = 0; (int)hv_T <= 9; hv_T = (int)hv_T + 1)
        {
            //
            //Matching 01: Obtain the test image
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, hv_TestImages.TupleSelect(hv_T));
            //
            //Matching 01: Find the model
            hv_Row.Dispose(); hv_Column.Dispose(); hv_Angle.Dispose(); hv_Score.Dispose();
            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: Code for rectification of the image
            //Matching 01: Calculate an inverse hom_mat2d for each of the matching results
            for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Score.TupleLength())) - 1); hv_I = (int)hv_I + 1)
            {
                hv_RectificationHomMat2D.Dispose();
                HOperatorSet.HomMat2dIdentity(out hv_RectificationHomMat2D);
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.HomMat2dTranslate(hv_RectificationHomMat2D, hv_RefRow - (hv_Row.TupleSelect(
                                                                                              hv_I)), hv_RefColumn - (hv_Column.TupleSelect(hv_I)), out ExpTmpOutVar_0);
                    hv_RectificationHomMat2D.Dispose();
                    hv_RectificationHomMat2D = ExpTmpOutVar_0;
                }
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.HomMat2dRotate(hv_RectificationHomMat2D, -(hv_Angle.TupleSelect(
                                                                                hv_I)), hv_RefRow, hv_RefColumn, out ExpTmpOutVar_0);
                    hv_RectificationHomMat2D.Dispose();
                    hv_RectificationHomMat2D = ExpTmpOutVar_0;
                }
                ho_RectifiedImage.Dispose();
                HOperatorSet.AffineTransImage(ho_Image, out ho_RectifiedImage, hv_RectificationHomMat2D,
                                              "constant", "false");
                //
                //Matching 01: Insert your code using the rectified image here

                ho_Regions.Dispose();
                HOperatorSet.Threshold(ho_Image, out ho_Regions, 99, 255);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                                         "and", 150, 800);

                hv_Row1.Dispose(); hv_Column1.Dispose(); hv_Radius.Dispose();
                HOperatorSet.SmallestCircle(ho_SelectedRegions, out hv_Row1, out hv_Column1,
                                            out hv_Radius);
                hv_MetrologyHandle.Dispose();
                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
                hv_Width.Dispose(); hv_Height.Dispose();
                HOperatorSet.GetImageSize(ho_RectifiedImage, out hv_Width, out hv_Height);
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
                hv_Index.Dispose();
                HOperatorSet.AddMetrologyObjectCircleMeasure(hv_MetrologyHandle, hv_Row1,
                                                             hv_Column1, hv_Radius, 20, 5, 1, 30, "num_measures", 30, out hv_Index);
                HOperatorSet.ApplyMetrologyModel(ho_RectifiedImage, hv_MetrologyHandle);
                ho_Contours.Dispose(); hv_Row3.Dispose(); hv_Column3.Dispose();
                HOperatorSet.GetMetrologyObjectMeasures(out ho_Contours, hv_MetrologyHandle,
                                                        "all", "all", out hv_Row3, out hv_Column3);
                hv_Parameter.Dispose();
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type",
                                                      "all_param", out hv_Parameter);
                //*********

                ho_Regions1.Dispose();
                HOperatorSet.Threshold(ho_Image, out ho_Regions1, 22, 78);
                hv_Row2.Dispose(); hv_Column2.Dispose(); hv_Radius1.Dispose();
                HOperatorSet.SmallestCircle(ho_Regions1, out hv_Row2, out hv_Column2, out hv_Radius1);
                hv_MetrologyHandle1.Dispose();
                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle1);
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle1, hv_Width, hv_Height);
                hv_Index1.Dispose();
                HOperatorSet.AddMetrologyObjectCircleMeasure(hv_MetrologyHandle1, hv_Row2,
                                                             hv_Column2, hv_Radius1, 20, 5, 1, 30, new HTuple(), new HTuple(), out hv_Index1);
                HOperatorSet.ApplyMetrologyModel(ho_RectifiedImage, hv_MetrologyHandle1);
                hv_Parameter1.Dispose();
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle1, "all", "all",
                                                      "result_type", "all_param", out hv_Parameter1);
                //*******

                hv_SmlC.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_SmlC = HTuple.TupleGenSequence(
                        0, (new HTuple(hv_Parameter.TupleLength())) - 1, 3);
                }
                hv_SR.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_SR = hv_Parameter.TupleSelect(
                        hv_SmlC);
                }
                hv_SC.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_SC = hv_Parameter.TupleSelect(
                        hv_SmlC + 1);
                }

                hv_BR.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_BR = (hv_SR * 0) + (hv_Parameter1.TupleSelect(
                                               0));
                }
                hv_BC.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_BC = (hv_SC * 0) + (hv_Parameter1.TupleSelect(
                                               1));
                }


                hv_Distance.Dispose();
                HOperatorSet.DistancePp(hv_SR, hv_SC, hv_BR, hv_BC, out hv_Distance);

                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_RectifiedImage, HDevWindowStack.GetActive());
                }
                ho_RegionLines.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines, hv_SR, hv_SC, hv_BR, hv_BC);
                // stop(...); only in hdevelop
            }
        }
        //
        //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_RectifiedImage.Dispose();
        ho_Regions.Dispose();
        ho_ConnectedRegions.Dispose();
        ho_SelectedRegions.Dispose();
        ho_Contours.Dispose();
        ho_Regions1.Dispose();
        ho_RegionLines.Dispose();

        hv_ModelID.Dispose();
        hv_ModelRegionArea.Dispose();
        hv_RefRow.Dispose();
        hv_RefColumn.Dispose();
        hv_TestImages.Dispose();
        hv_T.Dispose();
        hv_Row.Dispose();
        hv_Column.Dispose();
        hv_Angle.Dispose();
        hv_Score.Dispose();
        hv_I.Dispose();
        hv_RectificationHomMat2D.Dispose();
        hv_Row1.Dispose();
        hv_Column1.Dispose();
        hv_Radius.Dispose();
        hv_MetrologyHandle.Dispose();
        hv_Width.Dispose();
        hv_Height.Dispose();
        hv_Index.Dispose();
        hv_Row3.Dispose();
        hv_Column3.Dispose();
        hv_Parameter.Dispose();
        hv_Row2.Dispose();
        hv_Column2.Dispose();
        hv_Radius1.Dispose();
        hv_MetrologyHandle1.Dispose();
        hv_Index1.Dispose();
        hv_Parameter1.Dispose();
        hv_SmlC.Dispose();
        hv_SR.Dispose();
        hv_SC.Dispose();
        hv_BR.Dispose();
        hv_BC.Dispose();
        hv_Distance.Dispose();
    }
Example #3
0
        //加载
        public void LoadXML(string xmlPath)
        {
            // 1216 lw 此处保存产品会出错

            /*await Task.Run(() =>
             * {
             *  while (htWindow.HTWindow.HalconWindow.Handle == (IntPtr)0xffffffffffffffff)
             *  {
             *      Thread.Sleep(500);
             *  }
             * });*/

            IsLoadXML = true;

            try
            {
                XElement root = XElement.Load(xmlPath);
                XElement wireParameterNode = root.Element("WireParameter");
                if (wireParameterNode == null)
                {
                    return;
                }
                XMLHelper.ReadParameters(wireParameterNode, wireParameter);

                //从金线自动生成区域加载参考信息
                (wireAddAutoRegion as WireAddAutoRegion).LoadReferenceData();

                //-----------------------------------------------------------------------------------------------------------------------------
                XElement wireRegionsGroupNode = root.Element("WireRegionsGroup");
                if (wireRegionsGroupNode == null)
                {
                    return;
                }

                wireRegionsGroup.Clear();
                foreach (var groupNode in wireRegionsGroupNode.Elements("Group"))
                {
                    WireRegionsGroup group = new WireRegionsGroup();
                    group.BondOnICUserRegions    = UserRegion.FromXElement(groupNode.Element("BondOnICUserRegions"), false, wireParameter.DieImageRowOffset, wireParameter.DieImageColumnOffset, false, false, false);
                    group.BondOnFrameUserRegions = UserRegion.FromXElement(groupNode.Element("BondOnFrameUserRegions"), false, wireParameter.DieImageRowOffset, wireParameter.DieImageColumnOffset, false, false, false);
                    //group.RefLineUserRegions = UserRegion.FromXElement(groupNode.Element("RefLineUserRegions"), false, wireParameter.DieImageRowOffset, wireParameter.DieImageColumnOffset);
                    XMLHelper.ReadRegion(groupNode, group.LineUserRegions, "LineUserRegions", wireParameter.DieImageRowOffset, wireParameter.DieImageColumnOffset, false, true, false, false);

                    //1212 金线检测区域的通道值设置-生成线
                    for (int i = 0; i < group.LineUserRegions.Count(); i++)
                    {
                        if (group.LineUserRegions.ElementAt(i).ChannelNames.Count() == 0)
                        {
                            group.LineUserRegions.ElementAt(i).ChannelNames = wireParameter.ChannelNames; // currentChannelName
                            int tmp_ind;
                            tmp_ind = group.LineUserRegions.ElementAt(i).ImageIndex;
                        }
                    }
                    wireRegionsGroup.Add(group);
                    group.Index = wireRegionsGroup.Count;
                }
                (wireAddRegion as WireAddRegion).GroupsCount = wireRegionsGroup.Count;

                //-----加载自动生成金线界面
                //加载起始焊点区域归属
                startBondOnRecipes.Clear();
                XMLHelper.ReadOnRecipes(root, startBondOnRecipes, "StartBondOnRecipes");
                XMLHelper.ReadRegion(root, startBallAutoUserRegion, "StartBallAutoUserRegion", wireParameter.DieImageRowOffset,
                                     wireParameter.DieImageColumnOffset, false, false, false, false);

                // add lw
                HTuple hv__filePath = new HTuple(), hv_FileExists = new HTuple();
                HTuple Startreg_index_after_sort = new HTuple();
                hv__filePath = ModelsDirectory + "Startreg_index_after_sort.tup";
                HOperatorSet.FileExists(hv__filePath, out hv_FileExists);
                if ((int)(hv_FileExists) != 0)
                {
                    HOperatorSet.ReadTuple(hv__filePath, out Startreg_index_after_sort);
                }
                else
                {
                    Startreg_index_after_sort = HTuple.TupleGenSequence(1, startBallAutoUserRegion.Count, 1);
                }

                for (int i = 0; i < Startreg_index_after_sort.Length; i++)
                {
                    startBallAutoUserRegion[i].Index_ini = Startreg_index_after_sort[i];
                }

                if (startBondOnRecipes.Count == 0)
                {
                    IsLoadXML = false;
                    if (wireParameter.IsEnableStartVirtualBond == true)
                    {
                        wireParameter.IsEnableStartVirtualBond = false;
                        wireParameter.IsEnableStartVirtualBond = true;
                    }
                    if (wireParameter.IsEnableStartVirtualBond == false)
                    {
                        wireParameter.IsEnableStartVirtualBond = true;
                        wireParameter.IsEnableStartVirtualBond = false;
                    }
                }

                //加载结束焊点归属
                endBondOnRecipes.Clear();
                XMLHelper.ReadOnRecipes(root, endBondOnRecipes, "EndBondOnRecipes");
                XMLHelper.ReadRegion(root, stopBallAutoUserRegion, "StopBallAutoUserRegion", wireParameter.DieImageRowOffset,
                                     wireParameter.DieImageColumnOffset, false, false, false, false);

                // add lw
                HTuple Stopreg_index_after_sort  = new HTuple();
                hv__filePath = ModelsDirectory + "Stopreg_index_after_sort.tup";
                HOperatorSet.FileExists(hv__filePath, out hv_FileExists);
                if ((int)(hv_FileExists) != 0)
                {
                    HOperatorSet.ReadTuple(hv__filePath, out Stopreg_index_after_sort);
                }
                else
                {
                    Stopreg_index_after_sort = HTuple.TupleGenSequence(1, stopBallAutoUserRegion.Count, 1);
                }

                for (int i = 0; i < Stopreg_index_after_sort.Length; i++)
                {
                    stopBallAutoUserRegion[i].Index_ini = Stopreg_index_after_sort[i];
                }

                if (endBondOnRecipes.Count == 0)
                {
                    IsLoadXML = false;
                    if (wireParameter.IsEnableEndVirtualBond == true)
                    {
                        wireParameter.IsEnableEndVirtualBond = false;
                        wireParameter.IsEnableEndVirtualBond = true;
                    }
                    if (wireParameter.IsEnableEndVirtualBond == false)
                    {
                        wireParameter.IsEnableEndVirtualBond = true;
                        wireParameter.IsEnableEndVirtualBond = false;
                    }
                }

                //---------------生成界面中金线
                (wireAddAutoRegion as WireAddAutoRegion).UpdateStartandStopLineRegions(true);

                //金线模板Model
                XElement wireRegionsModelGroupNode = root.Element("WireRegionsModelGroup");
                if (wireRegionsModelGroupNode == null)
                {
                    return;
                }
                wireRegionsModelGroup.Clear();

                foreach (var modelGroupNode in wireRegionsModelGroupNode.Elements("ModelGroup"))
                {
                    WireAutoRegionGroup modelGroup = new WireAutoRegionGroup();
                    modelGroup.ModelStartUserRegions = UserRegion.FromXElement(modelGroupNode.Element("ModelStartUserRegions"),
                                                                               false, wireParameter.DieImageRowOffset, wireParameter.DieImageColumnOffset, false, false, false);
                    modelGroup.ModelStopUserRegions = UserRegion.FromXElement(modelGroupNode.Element("ModelStopUserRegions"),
                                                                              false, wireParameter.DieImageRowOffset, wireParameter.DieImageColumnOffset, false, false, false);
                    modelGroup.RefLineModelRegions = UserRegion.FromXElement(modelGroupNode.Element("RefLineModelRegions"), false,
                                                                             wireParameter.DieImageRowOffset, wireParameter.DieImageColumnOffset, false, false, false);
                    XMLHelper.ReadRegion(modelGroupNode, modelGroup.LineModelRegions, "LineModelRegions", wireParameter.DieImageRowOffset,
                                         wireParameter.DieImageColumnOffset, false, false, true, false);
                    //1212 金线检测区域的通道值设置-模板线
                    for (int i = 0; i < modelGroup.LineModelRegions.Count(); i++)
                    {
                        if (modelGroup.LineModelRegions.ElementAt(i).ChannelNames.Count() == 0)
                        {
                            modelGroup.LineModelRegions.ElementAt(i).ChannelNames = wireParameter.ChannelNames;
                            int tmp_ind;
                            tmp_ind = modelGroup.LineModelRegions.ElementAt(i).ImageIndex;
                        }
                    }
                    wireRegionsModelGroup.Add(modelGroup);
                    modelGroup.Index = wireRegionsModelGroup.Count;

                    modelGroup.SelectModelNumber = (int)modelGroupNode.FirstAttribute.NextAttribute;
                }
                (wireAddAutoRegion as WireAddAutoRegion).ModelGroupsCount = wireRegionsModelGroup.Count;

                //恢复自动生成金线起始区域金线ModelType  add by wj
                for (int i = 0; i < wireParameter.WireRegModelType.Length; i++)
                {
                    startBallAutoUserRegion[i].ModelGroups       = wireRegionsModelGroup;
                    startBallAutoUserRegion[i].CurrentModelGroup = wireRegionsModelGroup.ElementAt(wireParameter.WireRegModelType[i] - 1);
                }

                //1109 赋值初始排序
                if (wireParameter.WireAutoIndex_sorted_start.Length == startBallAutoUserRegion.Count)
                {
                    for (int i = 0; i < wireParameter.WireAutoIndex_sorted_start.Length; i++)
                    {
                        startBallAutoUserRegion[i].Index_ini = wireParameter.WireAutoIndex_sorted_start[i];
                    }
                }
                if (wireParameter.WireAutoIndex_sorted_stop.Length == stopBallAutoUserRegion.Count)
                {
                    for (int i = 0; i < wireParameter.WireAutoIndex_sorted_stop.Length; i++)
                    {
                        stopBallAutoUserRegion[i].Index_ini = wireParameter.WireAutoIndex_sorted_stop[i];
                    }
                }

                //1029 default model setting
                if (wireRegionsModelGroup.Count > 0)
                {
                    (wireAddAutoRegion as WireAddAutoRegion).CurrentModelGroup = (wireAddAutoRegion as WireAddAutoRegion).ModelGroups.ElementAt(0);
                }

                if ((wireAddAutoRegion as WireAddAutoRegion).StartBallAutoUserRegion.Count > 0)
                {
                    (wireAddAutoRegion as WireAddAutoRegion).WireParameter.IsStartPickUp = true;
                }
                if ((wireAddAutoRegion as WireAddAutoRegion).StopBallAutoUserRegion.Count > 0)
                {
                    (wireAddAutoRegion as WireAddAutoRegion).WireParameter.IsStopPickUp = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void train_classifier(HTuple hv_DLClassifierHandle, HTuple hv_FileName,
                                      HTuple hv_NumEpochs, HTuple hv_TrainingImages, HTuple hv_TrainingLabels, HTuple hv_ValidationImages,
                                      HTuple hv_ValidationLabels, HTuple hv_LearningRateStepEveryNthEpoch, HTuple hv_LearningRateStepRatio,
                                      HTuple hv_PlotEveryNthEpoch, HTuple hv_WindowHandle)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_BatchImages = null;

            // Local control variables

            HTuple hv_TrainingErrors = new HTuple(), hv_ValidationErrors = new HTuple();
            HTuple hv_LearningRates = new HTuple(), hv_Epochs = new HTuple();
            HTuple hv_LossByIteration = new HTuple(), hv_BatchSize = new HTuple();
            HTuple hv_MinValidationError = new HTuple(), hv_NumBatchesInEpoch = new HTuple();
            HTuple hv_NumTotalIterations = new HTuple(), hv_PlottedIterations = new HTuple();
            HTuple hv_TrainSequence = new HTuple(), hv_SelectPercentageTrainingImages = new HTuple();
            HTuple hv_TrainingImagesSelected = new HTuple(), hv_TrainingLabelsSelected = new HTuple();
            HTuple hv_Epoch = new HTuple(), hv_Iteration = new HTuple();
            HTuple hv_BatchStart = new HTuple(), hv_BatchEnd = new HTuple();
            HTuple hv_BatchIndices = new HTuple(), hv_BatchImageFiles = new HTuple();
            HTuple hv_BatchLabels = new HTuple(), hv_GenParamName = new HTuple();
            HTuple hv_GenParamValue = new HTuple(), hv_DLClassifierTrainResultHandle = new HTuple();
            HTuple hv_Loss = new HTuple(), hv_CurrentIteration = new HTuple();
            HTuple hv_TrainingDLClassifierResultIDs = new HTuple();
            HTuple hv_ValidationDLClassifierResultIDs = new HTuple();
            HTuple hv_TrainingTop1Error = new HTuple(), hv_ValidationTop1Error = new HTuple();
            HTuple hv_LearningRate = new HTuple();

            HTupleVector hvec_TrainingPredictedLabels   = new HTupleVector(1);
            HTupleVector hvec_TrainingConfidences       = new HTupleVector(1);
            HTupleVector hvec_ValidationPredictedLabels = new HTupleVector(1);
            HTupleVector hvec_ValidationConfidences     = new HTupleVector(1);

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_BatchImages);
            try
            {
                //For the plot during training,
                //we need to concatenate some intermediate results.
                hv_TrainingErrors.Dispose();
                hv_TrainingErrors = new HTuple();
                hv_ValidationErrors.Dispose();
                hv_ValidationErrors = new HTuple();
                hv_LearningRates.Dispose();
                hv_LearningRates = new HTuple();
                hv_Epochs.Dispose();
                hv_Epochs = new HTuple();
                hv_LossByIteration.Dispose();
                hv_LossByIteration = new HTuple();
                hv_BatchSize.Dispose();
                HOperatorSet.GetDlClassifierParam(hv_DLClassifierHandle, "batch_size", out hv_BatchSize);
                hv_MinValidationError.Dispose();
                hv_MinValidationError = 1;
                //
                //Create a tuple that includes all the iterations
                //where the plot should be computed (including the last ieration).
                hv_NumBatchesInEpoch.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_NumBatchesInEpoch = (((((new HTuple(hv_TrainingImages.TupleLength()
                                                           )) / (hv_BatchSize.TupleReal()))).TupleFloor())).TupleInt();
                }
                hv_NumTotalIterations.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_NumTotalIterations = (hv_NumBatchesInEpoch * hv_NumEpochs) - 1;
                }
                hv_PlottedIterations.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_PlottedIterations = ((((hv_NumBatchesInEpoch * HTuple.TupleGenSequence(
                                                   0, hv_NumEpochs - 1, hv_PlotEveryNthEpoch))).TupleConcat(hv_NumTotalIterations))).TupleRound()
                    ;
                }
                //
                //TrainSequence is used for easier indexing of the training data.
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TrainSequence.Dispose();
                    HOperatorSet.TupleGenSequence(0, (new HTuple(hv_TrainingImages.TupleLength()
                                                                 )) - 1, 1, out hv_TrainSequence);
                }
                //
                //Select a subset of the training data set
                //in order to obtain a fast approximation
                //of the training error during training (plotting).
                hv_SelectPercentageTrainingImages.Dispose();
                hv_SelectPercentageTrainingImages = 100;
                hv_TrainingImagesSelected.Dispose(); hv_TrainingLabelsSelected.Dispose();
                select_percentage_dl_classifier_data(hv_TrainingImages, hv_TrainingLabels,
                                                     hv_SelectPercentageTrainingImages, out hv_TrainingImagesSelected, out hv_TrainingLabelsSelected);
                //
                HTuple end_val25  = hv_NumEpochs - 1;
                HTuple step_val25 = 1;
                #region
                for (hv_Epoch = 0; hv_Epoch.Continue(end_val25, step_val25); hv_Epoch = hv_Epoch.TupleAdd(step_val25))
                {
                    //In order to get randomness in each epoch,
                    //the training set is shuffled every epoch.
                    {
                        HTuple ExpTmpOutVar_0;
                        tuple_shuffle(hv_TrainSequence, out ExpTmpOutVar_0);
                        hv_TrainSequence.Dispose();
                        hv_TrainSequence = ExpTmpOutVar_0;
                    }
                    HTuple end_val29  = hv_NumBatchesInEpoch - 1;
                    HTuple step_val29 = 1;
                    for (hv_Iteration = 0; hv_Iteration.Continue(end_val29, step_val29); hv_Iteration = hv_Iteration.TupleAdd(step_val29))
                    {
                        //Select a batch from the training data set.
                        hv_BatchStart.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchStart = hv_Iteration * hv_BatchSize;
                        }
                        hv_BatchEnd.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchEnd = hv_BatchStart + (hv_BatchSize - 1);
                        }
                        hv_BatchIndices.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchIndices = hv_TrainSequence.TupleSelectRange(
                                hv_BatchStart, hv_BatchEnd);
                        }
                        hv_BatchImageFiles.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchImageFiles = hv_TrainingImages.TupleSelect(
                                hv_BatchIndices);
                        }
                        hv_BatchLabels.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchLabels = hv_TrainingLabels.TupleSelect(
                                hv_BatchIndices);
                        }
                        //
                        //Read the image of the current batch.
                        ho_BatchImages.Dispose();
                        HOperatorSet.ReadImage(out ho_BatchImages, hv_BatchImageFiles);
                        //Augment the images to get a better variety of training images.
                        hv_GenParamName.Dispose();
                        hv_GenParamName = "mirror";
                        hv_GenParamValue.Dispose();
                        hv_GenParamValue = "rc";
                        {
                            HObject ExpTmpOutVar_0;
                            augment_images(ho_BatchImages, out ExpTmpOutVar_0, hv_GenParamName, hv_GenParamValue);
                            ho_BatchImages.Dispose();
                            ho_BatchImages = ExpTmpOutVar_0;
                        }
                        //
                        //Train the network with these images and ground truth labels.
                        hv_DLClassifierTrainResultHandle.Dispose();
                        HOperatorSet.TrainDlClassifierBatch(ho_BatchImages, hv_DLClassifierHandle,
                                                            hv_BatchLabels, out hv_DLClassifierTrainResultHandle);
                        //You can access the current value of the loss function,
                        //which should decrease during the training.
                        hv_Loss.Dispose();
                        HOperatorSet.GetDlClassifierTrainResult(hv_DLClassifierTrainResultHandle,
                                                                "loss", out hv_Loss);
                        //Store the loss in a tuple .
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                    ExpTmpLocalVar_LossByIteration = hv_LossByIteration.TupleConcat(
                                    hv_Loss);
                                hv_LossByIteration.Dispose();
                                hv_LossByIteration = ExpTmpLocalVar_LossByIteration;
                            }
                        }
                        //
                        //In regular intervals, we want to evaluate
                        //how well our classifier performs.
                        hv_CurrentIteration.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_CurrentIteration = ((hv_Iteration + (hv_NumBatchesInEpoch * hv_Epoch))).TupleInt()
                            ;
                        }
                        if ((int)(((hv_CurrentIteration.TupleEqualElem(hv_PlottedIterations))).TupleSum()
                                  ) != 0)
                        {
                            //Plot the progress regularly.
                            //Evaluate the current classifier on the training and validation set.
                            hv_TrainingDLClassifierResultIDs.Dispose(); hvec_TrainingPredictedLabels.Dispose(); hvec_TrainingConfidences.Dispose();
                            apply_dl_classifier_batchwise(hv_TrainingImagesSelected, hv_DLClassifierHandle,
                                                          out hv_TrainingDLClassifierResultIDs, out hvec_TrainingPredictedLabels,
                                                          out hvec_TrainingConfidences);
                            hv_ValidationDLClassifierResultIDs.Dispose(); hvec_ValidationPredictedLabels.Dispose(); hvec_ValidationConfidences.Dispose();
                            apply_dl_classifier_batchwise(hv_ValidationImages, hv_DLClassifierHandle,
                                                          out hv_ValidationDLClassifierResultIDs, out hvec_ValidationPredictedLabels,
                                                          out hvec_ValidationConfidences);
                            //Evaluate the top-1 error on each dataset.
                            hv_TrainingTop1Error.Dispose();
                            evaluate_dl_classifier(hv_TrainingLabelsSelected, hv_DLClassifierHandle,
                                                   hv_TrainingDLClassifierResultIDs, "top1_error", "global", out hv_TrainingTop1Error);
                            hv_ValidationTop1Error.Dispose();
                            evaluate_dl_classifier(hv_ValidationLabels, hv_DLClassifierHandle, hv_ValidationDLClassifierResultIDs,
                                                   "top1_error", "global", out hv_ValidationTop1Error);
                            //Concatenate the values for the plot.
                            hv_LearningRate.Dispose();
                            HOperatorSet.GetDlClassifierParam(hv_DLClassifierHandle, "learning_rate",
                                                              out hv_LearningRate);
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_TrainingErrors = hv_TrainingErrors.TupleConcat(
                                        hv_TrainingTop1Error);
                                    hv_TrainingErrors.Dispose();
                                    hv_TrainingErrors = ExpTmpLocalVar_TrainingErrors;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_ValidationErrors = hv_ValidationErrors.TupleConcat(
                                        hv_ValidationTop1Error);
                                    hv_ValidationErrors.Dispose();
                                    hv_ValidationErrors = ExpTmpLocalVar_ValidationErrors;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_LearningRates = hv_LearningRates.TupleConcat(
                                        hv_LearningRate);
                                    hv_LearningRates.Dispose();
                                    hv_LearningRates = ExpTmpLocalVar_LearningRates;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_Epochs = hv_Epochs.TupleConcat(
                                        (hv_PlottedIterations.TupleSelect(new HTuple(hv_Epochs.TupleLength()
                                                                                     ))) / (hv_NumBatchesInEpoch.TupleReal()));
                                    hv_Epochs.Dispose();
                                    hv_Epochs = ExpTmpLocalVar_Epochs;
                                }
                            }

                            //Plot validation and error against the epochs in order to
                            //observe the progress of the training.
                            plot_dl_classifier_training_progress(hv_TrainingErrors, hv_ValidationErrors, hv_LearningRates, hv_Epochs, hv_NumEpochs, hv_WindowHandle);
                            //如果上面绘制坐标系到窗体过程报错,搞不定的话就用下面这句代替,直接显示训练数据到窗体
                            //disp_training_progress_datas(hv_TrainingErrors, hv_ValidationErrors, hv_LearningRates, hv_Epochs, hv_NumEpochs, hv_WindowHandle);


                            if ((int)(new HTuple(hv_ValidationTop1Error.TupleLessEqual(hv_MinValidationError))) != 0)
                            {
                                HOperatorSet.WriteDlClassifier(hv_DLClassifierHandle, hv_FileName);
                                hv_MinValidationError.Dispose();
                                hv_MinValidationError = new HTuple(hv_ValidationTop1Error);
                            }
                        }
                    }
                    //Reduce the learning rate every nth epoch.
                    if ((int)(new HTuple((((hv_Epoch + 1) % hv_LearningRateStepEveryNthEpoch)).TupleEqual(
                                             0))) != 0)
                    {
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HOperatorSet.SetDlClassifierParam(hv_DLClassifierHandle, "learning_rate",
                                                              hv_LearningRate * hv_LearningRateStepRatio);
                        }
                        hv_LearningRate.Dispose();
                        HOperatorSet.GetDlClassifierParam(hv_DLClassifierHandle, "learning_rate",
                                                          out hv_LearningRate);
                    }
                }
                #endregion
                // stop(...); only in hdevelop
                ho_BatchImages.Dispose();

                hv_TrainingErrors.Dispose();
                hv_ValidationErrors.Dispose();
                hv_LearningRates.Dispose();
                hv_Epochs.Dispose();
                hv_LossByIteration.Dispose();
                hv_BatchSize.Dispose();
                hv_MinValidationError.Dispose();
                hv_NumBatchesInEpoch.Dispose();
                hv_NumTotalIterations.Dispose();
                hv_PlottedIterations.Dispose();
                hv_TrainSequence.Dispose();
                hv_SelectPercentageTrainingImages.Dispose();
                hv_TrainingImagesSelected.Dispose();
                hv_TrainingLabelsSelected.Dispose();
                hv_Epoch.Dispose();
                hv_Iteration.Dispose();
                hv_BatchStart.Dispose();
                hv_BatchEnd.Dispose();
                hv_BatchIndices.Dispose();
                hv_BatchImageFiles.Dispose();
                hv_BatchLabels.Dispose();
                hv_GenParamName.Dispose();
                hv_GenParamValue.Dispose();
                hv_DLClassifierTrainResultHandle.Dispose();
                hv_Loss.Dispose();
                hv_CurrentIteration.Dispose();
                hv_TrainingDLClassifierResultIDs.Dispose();
                hv_ValidationDLClassifierResultIDs.Dispose();
                hv_TrainingTop1Error.Dispose();
                hv_ValidationTop1Error.Dispose();
                hv_LearningRate.Dispose();
                hvec_TrainingPredictedLabels.Dispose();
                hvec_TrainingConfidences.Dispose();
                hvec_ValidationPredictedLabels.Dispose();
                hvec_ValidationConfidences.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_BatchImages.Dispose();

                hv_TrainingErrors.Dispose();
                hv_ValidationErrors.Dispose();
                hv_LearningRates.Dispose();
                hv_Epochs.Dispose();
                hv_LossByIteration.Dispose();
                hv_BatchSize.Dispose();
                hv_MinValidationError.Dispose();
                hv_NumBatchesInEpoch.Dispose();
                hv_NumTotalIterations.Dispose();
                hv_PlottedIterations.Dispose();
                hv_TrainSequence.Dispose();
                hv_SelectPercentageTrainingImages.Dispose();
                hv_TrainingImagesSelected.Dispose();
                hv_TrainingLabelsSelected.Dispose();
                hv_Epoch.Dispose();
                hv_Iteration.Dispose();
                hv_BatchStart.Dispose();
                hv_BatchEnd.Dispose();
                hv_BatchIndices.Dispose();
                hv_BatchImageFiles.Dispose();
                hv_BatchLabels.Dispose();
                hv_GenParamName.Dispose();
                hv_GenParamValue.Dispose();
                hv_DLClassifierTrainResultHandle.Dispose();
                hv_Loss.Dispose();
                hv_CurrentIteration.Dispose();
                hv_TrainingDLClassifierResultIDs.Dispose();
                hv_ValidationDLClassifierResultIDs.Dispose();
                hv_TrainingTop1Error.Dispose();
                hv_ValidationTop1Error.Dispose();
                hv_LearningRate.Dispose();
                hvec_TrainingPredictedLabels.Dispose();
                hvec_TrainingConfidences.Dispose();
                hvec_ValidationPredictedLabels.Dispose();
                hvec_ValidationConfidences.Dispose();

                throw HDevExpDefaultException;
            }
        }