Exemplo n.º 1
0
        public void add_samples(HObject ho_Regions, HTuple hv_GMMHandle, HTuple hv_Class)
        {
            // Local iconic variables
            HObject ho_ConnectedRegions = null, ho_SelectedRegions = null;
            HObject ho_Region = null;
            // Local control variables
            HTuple hv_Number = null, hv_J = null, hv_Features = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.CountObj(ho_Regions, out hv_Number);
            HTuple end_val1  = hv_Number;
            HTuple step_val1 = 1;

            for (hv_J = 1; hv_J.Continue(end_val1, step_val1); hv_J = hv_J.TupleAdd(step_val1))
            {
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 1000, 100000);
                ho_Region.Dispose();
                HOperatorSet.SelectObj(ho_SelectedRegions, out ho_Region, hv_J);
                get_features(ho_Region, out hv_Features);
                HOperatorSet.AddSampleClassGmm(hv_GMMHandle, hv_Features, hv_Class, 0);
            }
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();
            ho_Region.Dispose();
            return;
        }
Exemplo n.º 2
0
        public void disp_obj_class(HObject ho_Regions, HTuple hv_Classes)
        {
            // Local iconic variables
            HObject ho_Region = null;
            // Local control variables
            HTuple hv_Number = null, hv_Colors = null;
            HTuple hv_J = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.CountObj(ho_Regions, out hv_Number);
            hv_Colors    = new HTuple();
            hv_Colors[0] = "yellow";
            hv_Colors[1] = "magenta";
            hv_Colors[2] = "green";
            HTuple end_val2  = hv_Number;
            HTuple step_val2 = 1;

            for (hv_J = 1; hv_J.Continue(end_val2, step_val2); hv_J = hv_J.TupleAdd(step_val2))
            {
                ho_Region.Dispose();
                HOperatorSet.SelectObj(ho_Regions, out ho_Region, hv_J);
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Colors.TupleSelect(hv_Classes.TupleSelect(hv_J - 1)));
                HOperatorSet.DispObj(ho_Region, hv_ExpDefaultWinHandle);
            }
            ho_Region.Dispose();

            return;
        }
Exemplo n.º 3
0
        private void ModelSearch(HTuple hv_ModelID, int i, out HTuple hv_Row, out HTuple hv_Column)
        {
            // Local iconic variables

            HTuple hv_num = new HTuple(), hv_a = new HTuple();
            HTuple hv_Seconds1 = new HTuple();
            HTuple hv_Angle = new HTuple(), hv_Score = new HTuple();
            HTuple hv_Seconds2 = new HTuple(), hv_Time = new HTuple();
            HTuple hv_HeightWin = null, hv_WidthWin = null, hv_n = 1;

            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_Image);

            hv_Row    = null;
            hv_Column = null;

            // stop(); only in hdevelop
            //**********************************************
            //**********************************************
            //设置模板完成开始匹配

            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, ("acA2500-14gc0" + i) + ".bmp");
            ho_GrayImage.Dispose();
            HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
            HOperatorSet.CountSeconds(out hv_Seconds1);
            HOperatorSet.FindShapeModel(ho_Image, hv_ModelID, (new HTuple(0)).TupleRad()
                                        , (new HTuple(360)).TupleRad(), 0.8, 15, 0.2, "none", 0, 0.9, out hv_Row,
                                        out hv_Column, out hv_Angle, out hv_Score);
            HOperatorSet.CountSeconds(out hv_Seconds2);
            hv_Time = 1000.0 * (hv_Seconds2 - hv_Seconds1);
            HOperatorSet.GetImageSize(ho_Image, out hv_HeightWin, out hv_WidthWin);              // 获取输入图像的尺寸
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, hv_WidthWin, hv_HeightWin); //将获得的图像铺满整个窗口
            HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "yellow");
            HOperatorSet.SetLineWidth(hWindowControl1.HalconWindow, 1);
            HOperatorSet.SetLineWidth(hWindowControl1.HalconWindow, 3);
            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "green");
            HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");

            hv_num = new HTuple(hv_Row.TupleLength());
            HTuple end_val35  = hv_num - 1;
            HTuple step_val35 = 1;

            for (hv_a = 0; hv_a.Continue(end_val35, step_val35); hv_a = hv_a.TupleAdd(step_val35))
            {
                ho_Circle.Dispose();
                HOperatorSet.GenCircle(out ho_Circle, hv_Row.TupleSelect(hv_a), hv_Column.TupleSelect(
                                           hv_a), 220);
                HOperatorSet.DispObj(ho_Circle, hWindowControl1.HalconWindow);
            }
            double d = hv_Time[0];

            txtNumber.Text = hv_num.ToString();
            txtTime.Text   = d.ToString("0.##");
            ho_GrayImage.Dispose();
            ho_Image.Dispose();
            ho_Circle.Dispose();
        }
Exemplo n.º 4
0
        private void btn_extract_Click(object sender, EventArgs e)
        {
            if (hDisplay1.GetSearchRegions().Count == 0)
            {
                MessageBox.Show("请先添加搜索区域");
                return;
            }
            HTuple  hv_Number;
            HObject _roi              = hDisplay1.GetSearchRegions().ElementAt(0);
            HObject _region           = new HObject();
            HObject _imagereduced     = new HObject();
            HObject ho_ObjectSelected = new HObject();

            HOperatorSet.ReduceDomain(m_image, _roi, out _imagereduced);
            HOperatorSet.Threshold(_imagereduced, out _region, m_OcrParam.GrayMin, m_OcrParam.GrayMax);
            HOperatorSet.Connection(_region, out _region);
            HOperatorSet.SelectShape(_region, out _region, "area", "and", m_OcrParam.AreaMin, m_OcrParam.AreaMax);

            HOperatorSet.CountObj(_region, out hv_Number);
            HTuple         hv_Index     = new HTuple();
            List <RegionX> _listregionx = new List <RegionX>();

            for (hv_Index = 1; hv_Index.Continue(hv_Number, 1); hv_Index = hv_Index.TupleAdd(1))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(_region, out ho_ObjectSelected, hv_Index);
                RegionX _hregion = new RegionX(ho_ObjectSelected.CopyObj(1, -1), "green");
                _listregionx.Add(_hregion);
            }
            DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, _listregionx, null);
        }
Exemplo n.º 5
0
        public void GetRegionPoints(HObject ho_Regions, HTuple hv_Tolerance, out HTuple hv_Rows,
                                    out HTuple hv_Cols, out HTuple hv_BreakPoints)
        {
            // Local iconic variables

            HObject ho_RegionUnion, ho_ConnectedRegions;
            HObject ho_ObjectSelected = null;

            // Local control variables

            HTuple hv_Num = null, hv_Index = null, hv_Rows0 = new HTuple();
            HTuple hv_Columns0 = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            try
            {
                ho_RegionUnion.Dispose();
                HOperatorSet.Union1(ho_Regions, out ho_RegionUnion);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_RegionUnion, out ho_ConnectedRegions);
                HOperatorSet.CountObj(ho_ConnectedRegions, out hv_Num);

                hv_Rows        = new HTuple();
                hv_Cols        = new HTuple();
                hv_BreakPoints = new HTuple();

                HTuple end_val8  = hv_Num;
                HTuple step_val8 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val8, step_val8); hv_Index = hv_Index.TupleAdd(step_val8))
                {
                    ho_ObjectSelected.Dispose();
                    HOperatorSet.SelectObj(ho_ConnectedRegions, out ho_ObjectSelected, hv_Index);
                    HOperatorSet.GetRegionPolygon(ho_ObjectSelected, hv_Tolerance, out hv_Rows0,
                                                  out hv_Columns0);
                    hv_Rows        = hv_Rows.TupleConcat(hv_Rows0);
                    hv_Cols        = hv_Cols.TupleConcat(hv_Columns0);
                    hv_BreakPoints = hv_BreakPoints.TupleConcat(new HTuple(hv_Rows0.TupleLength()
                                                                           ));
                }
                ho_RegionUnion.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_ObjectSelected.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_RegionUnion.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_ObjectSelected.Dispose();

                throw HDevExpDefaultException;
            }
        }
    // Main procedure
    private void action()
    {
        // Stack for temporary objects
        HObject[] OTemp = new HObject[20];

        // Local iconic variables

        HObject ho_NewImage, ho_Image, ho_ImageSelected = null;
        HObject ho_Image1 = null;

        // Local control variables

        HTuple hv_AcqHandle = null, hv_ChannelCount = null;
        HTuple hv_Index = null, hv_Pointer = new HTuple(), hv_Type = new HTuple();
        HTuple hv_Width = new HTuple(), hv_Height = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_NewImage);
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_ImageSelected);
        HOperatorSet.GenEmptyObj(out ho_Image1);
        //Image Acquisition 01: Code generated by Image Acquisition 01
        ho_NewImage.Dispose();
        HOperatorSet.GenEmptyObj(out ho_NewImage);
        HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
                                      -1, "false", "default", "[0] Integrated Camera", 0, -1, out hv_AcqHandle);
        HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
        //Image Acquisition 01: Do something
        ho_Image.Dispose();
        HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
        HOperatorSet.CountChannels(ho_Image, out hv_ChannelCount);
        HTuple end_val7  = hv_ChannelCount;
        HTuple step_val7 = 1;

        for (hv_Index = 1; hv_Index.Continue(end_val7, step_val7); hv_Index = hv_Index.TupleAdd(step_val7))
        {
            ho_ImageSelected.Dispose();
            HOperatorSet.AccessChannel(ho_Image, out ho_ImageSelected, hv_Index);
            HOperatorSet.GetImagePointer1(ho_ImageSelected, out hv_Pointer, out hv_Type,
                                          out hv_Width, out hv_Height);
            ho_Image1.Dispose();
            HOperatorSet.GenImage1(out ho_Image1, "byte", hv_Width, hv_Height, hv_Pointer);
            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.AppendChannel(ho_NewImage, ho_Image1, out ExpTmpOutVar_0);
                ho_NewImage.Dispose();
                ho_NewImage = ExpTmpOutVar_0;
            }
        }
        HOperatorSet.CloseFramegrabber(hv_AcqHandle);
        ho_NewImage.Dispose();
        ho_Image.Dispose();
        ho_ImageSelected.Dispose();
        ho_Image1.Dispose();
    }
Exemplo n.º 7
0
        /// <summary>
        /// 测试小球OKNG的方法
        /// </summary>
        /// <param name="hv_liudao">小球所在位置</param>
        /// <param name="hv_channels">流道信息</param>
        /// <param name="hv_liudaoR">小球直径(像素值)</param>
        /// <param name="hv_myLength">小球标准直径(像素值)</param>
        /// <param name="hv_myThreashod">小球阈值范围</param>
        /// <param name="hv_myResult">是否都是好的</param>
        /// <param name="hv_NGLengths">NG小球直径</param>
        /// <param name="hv_NGChls">NG小球所在流道</param>
        public void testOKNG2(HTuple hv_liudao, HTuple hv_channels, HTuple hv_liudaoR,
                              HTuple hv_myLength, HTuple hv_myThreashod, out HTuple hv_myResult, out HTuple hv_NGLengths,
                              out HTuple hv_NGChls)
        {
            // Local iconic variables

            // Local control variables

            HTuple hv_num = null, hv_num2 = null, hv_Index1 = null;
            HTuple hv_Index2 = new HTuple(), hv_lll = new HTuple();
            HTuple hv_ff1 = new HTuple(), hv_ff2 = new HTuple(), hv_ff = new HTuple();

            // Initialize local and output iconic variables
            hv_myResult = 1;
            hv_num      = new HTuple(hv_liudao.TupleLength());
            hv_num2     = new HTuple(hv_channels.TupleLength());

            hv_NGChls    = new HTuple();
            hv_NGLengths = new HTuple();


            HTuple end_val8  = hv_num - 1;
            HTuple step_val8 = 1;

            for (hv_Index1 = 0; hv_Index1.Continue(end_val8, step_val8); hv_Index1 = hv_Index1.TupleAdd(step_val8))
            {
                HTuple end_val9  = hv_num2;
                HTuple step_val9 = 1;
                for (hv_Index2 = 1; hv_Index2.Continue(end_val9, step_val9); hv_Index2 = hv_Index2.TupleAdd(step_val9))
                {
                    if ((int)(new HTuple(((hv_liudao.TupleSelect(hv_Index1))).TupleLess(hv_channels.TupleSelect(
                                                                                            hv_Index2)))) != 0)
                    {
                        break;
                    }
                }
                hv_lll = hv_Index2 - 1;
                hv_ff1 = new HTuple(((hv_liudaoR.TupleSelect(hv_Index1))).TupleGreater((hv_myLength.TupleSelect(
                                                                                            hv_lll)) - (hv_myThreashod.TupleSelect(hv_lll))));
                hv_ff2 = new HTuple(((hv_liudaoR.TupleSelect(hv_Index1))).TupleLess((hv_myLength.TupleSelect(
                                                                                         hv_lll)) + (hv_myThreashod.TupleSelect(hv_lll))));
                hv_ff = hv_ff1.TupleAnd(hv_ff2);
                if ((int)(hv_ff) != 0)
                {
                }
                else
                {
                    hv_NGChls    = hv_NGChls.TupleConcat(hv_Index2);
                    hv_NGLengths = hv_NGLengths.TupleConcat(hv_liudaoR.TupleSelect(hv_Index1));
                }
                hv_myResult = hv_myResult.TupleAnd(hv_ff);
            }

            return;
        }
Exemplo n.º 8
0
        public void classify(HObject ho_Regions, HTuple hv_GMMHandle, out HTuple hv_Classes1)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];
            // Local iconic variables
            HObject ho_Region = null, ho_ConnectedRegions = null;
            HObject ho_SelectedRegions = null;
            // Local control variables
            HTuple hv_Number = null, hv_J = null, hv_Features = new HTuple();
            HTuple hv_ClassID = new HTuple(), hv_ClassProb = new HTuple();
            HTuple hv_Density = new HTuple(), hv_KSigmaProb = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.CountObj(ho_Regions, out hv_Number);
            hv_Classes1 = new HTuple();
            HTuple end_val2  = hv_Number;
            HTuple step_val2 = 1;

            for (hv_J = 1; hv_J.Continue(end_val2, step_val2); hv_J = hv_J.TupleAdd(step_val2))
            {
                ho_Region.Dispose();
                HOperatorSet.SelectObj(ho_Regions, out ho_Region, hv_J);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 1000, 100000);
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.SelectObj(ho_Region, out ExpTmpOutVar_0, hv_J);
                    ho_Region.Dispose();
                    ho_Region = ExpTmpOutVar_0;
                }
                get_features(ho_SelectedRegions, out hv_Features);
                HOperatorSet.ClassifyClassGmm(hv_GMMHandle, hv_Features, 1, out hv_ClassID,
                                              out hv_ClassProb, out hv_Density, out hv_KSigmaProb);
                HOperatorSet.EvaluateClassGmm(hv_GMMHandle, hv_Features, out hv_ClassProb,
                                              out hv_Density, out hv_KSigmaProb);
                hv_Classes1 = hv_Classes1.TupleConcat(hv_ClassID);
            }
            ho_Region.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();
            return;
        }
        public void myStringToRegular(HTuple hv_standardText, out HTuple hv_getRegular)
        {
            HTuple hv_regularModel = null, hv_Length = null;
            HTuple hv_Index2 = null, hv_Index3 = new HTuple(), hv_Selected = new HTuple();
            HTuple hv_Matches = new HTuple(), hv_Length2 = new HTuple();

            // Initialize local and output iconic variables
            hv_getRegular = "";

            hv_regularModel    = new HTuple();
            hv_regularModel[0] = "[0-9]";
            hv_regularModel[1] = "[A-Z]";
            hv_regularModel[2] = "[a-z]";
            hv_regularModel[3] = ".";
            HOperatorSet.TupleStrlen(hv_standardText, out hv_Length);

            HTuple end_val5  = hv_Length - 1;
            HTuple step_val5 = 1;

            for (hv_Index2 = 0; hv_Index2.Continue(end_val5, step_val5); hv_Index2 = hv_Index2.TupleAdd(step_val5))
            {
                for (hv_Index3 = 0; (int)hv_Index3 <= 3; hv_Index3 = (int)hv_Index3 + 1)
                {
                    HOperatorSet.TupleStrBitSelect(hv_standardText, hv_Index2, out hv_Selected);
                    HOperatorSet.TupleRegexpMatch(hv_Selected, hv_regularModel.TupleSelect(hv_Index3),
                                                  out hv_Matches);

                    HOperatorSet.TupleStrlen(hv_Matches, out hv_Length2);
                    if ((int)(new HTuple(hv_Length2.TupleEqual(1))) != 0)
                    {
                        if ((int)(new HTuple(((hv_regularModel.TupleSelect(hv_Index3))).TupleNotEqual("."))) != 0)
                        {
                            hv_getRegular = hv_getRegular + (hv_regularModel.TupleSelect(hv_Index3));
                            break;
                        }
                        else
                        {
                            hv_getRegular = hv_getRegular + hv_Selected;
                        }
                    }
                }
            }
            return;
        }
        private FitLineResult fitline(HObject contoursSplit)
        {
            FitLineResult result = new FitLineResult()
            {
                Col1 = new HTuple(),
                Row1 = new HTuple(),
                Col2 = new HTuple(),
                Row2 = new HTuple(),
            };

            //fitLine
            HTuple  number, rowBegin, colBegin, rowEnd, colEnd, nr, nc, dist;
            HObject objectSelected;

            HOperatorSet.GenEmptyObj(out objectSelected);

            HTuple distance, preDistance = 0.0;

            var fitlineContourAlgorithm = (mMeasAssist != null) ? new HTuple(mMeasAssist.FitLineAlgorithm) : _algorithm;

            HOperatorSet.CountObj(contoursSplit, out number);
            for (HTuple hv_i = 1; hv_i.Continue(number, 1); hv_i = hv_i.TupleAdd(1))
            {
                HOperatorSet.SelectObj(contoursSplit, out objectSelected, hv_i);
                HOperatorSet.FitLineContourXld(objectSelected
                                               , fitlineContourAlgorithm, _maxNumPoints, _clippingEndPoints, _iterations, _clippingFactor,
                                               out rowBegin, out colBegin, out rowEnd, out colEnd, out nr, out nc, out dist);

                HOperatorSet.DistancePp(rowBegin, colBegin, rowEnd, colEnd, out distance);
                if (distance > preDistance)
                {
                    preDistance = new HTuple(distance);
                    //Answer
                    result = new FitLineResult()
                    {
                        Row1 = new HTuple(rowBegin),
                        Col1 = new HTuple(colBegin),
                        Row2 = new HTuple(rowEnd),
                        Col2 = new HTuple(colEnd),
                    };
                }
            }
            return(result);
        }
Exemplo n.º 11
0
        private static void scale_image_range(HObject ho_Image, out HObject ho_ImageScaled, HTuple hv_Min,
                                              HTuple hv_Max)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_SelectedChannel = null, ho_LowerRegion = null;
            HObject ho_UpperRegion = null;

            // Local copy input parameter variables
            HObject ho_Image_COPY_INP_TMP;

            ho_Image_COPY_INP_TMP = ho_Image.CopyObj(1, -1);



            // Local control variables

            HTuple hv_LowerLimit = new HTuple(), hv_UpperLimit = new HTuple();
            HTuple hv_Mult = null, hv_Add = null, hv_Channels = null;
            HTuple hv_Index = null, hv_MinGray = new HTuple(), hv_MaxGray = new HTuple();
            HTuple hv_Range            = new HTuple();
            HTuple hv_Max_COPY_INP_TMP = hv_Max.Clone();
            HTuple hv_Min_COPY_INP_TMP = hv_Min.Clone();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            HOperatorSet.GenEmptyObj(out ho_SelectedChannel);
            HOperatorSet.GenEmptyObj(out ho_LowerRegion);
            HOperatorSet.GenEmptyObj(out ho_UpperRegion);
            //Convenience procedure to scale the gray values of the
            //input image Image from the interval [Min,Max]
            //to the interval [0,255] (default).
            //Gray values < 0 or > 255 (after scaling) are clipped.
            //
            //If the image shall be scaled to an interval different from [0,255],
            //this can be achieved by passing tuples with 2 values [From, To]
            //as Min and Max.
            //Example:
            //scale_image_range(Image:ImageScaled:[100,50],[200,250])
            //maps the gray values of Image from the interval [100,200] to [50,250].
            //All other gray values will be clipped.
            //
            //input parameters:
            //Image: the input image
            //Min: the minimum gray value which will be mapped to 0
            //     If a tuple with two values is given, the first value will
            //     be mapped to the second value.
            //Max: The maximum gray value which will be mapped to 255
            //     If a tuple with two values is given, the first value will
            //     be mapped to the second value.
            //
            //output parameter:
            //ImageScale: the resulting scaled image
            //
            if ((int)(new HTuple((new HTuple(hv_Min_COPY_INP_TMP.TupleLength())).TupleEqual(
                                     2))) != 0)
            {
                hv_LowerLimit       = hv_Min_COPY_INP_TMP[1];
                hv_Min_COPY_INP_TMP = hv_Min_COPY_INP_TMP[0];
            }
            else
            {
                hv_LowerLimit = 0.0;
            }
            if ((int)(new HTuple((new HTuple(hv_Max_COPY_INP_TMP.TupleLength())).TupleEqual(
                                     2))) != 0)
            {
                hv_UpperLimit       = hv_Max_COPY_INP_TMP[1];
                hv_Max_COPY_INP_TMP = hv_Max_COPY_INP_TMP[0];
            }
            else
            {
                hv_UpperLimit = 255.0;
            }
            //
            //Calculate scaling parameters
            hv_Mult = (((hv_UpperLimit - hv_LowerLimit)).TupleReal()) / (hv_Max_COPY_INP_TMP - hv_Min_COPY_INP_TMP);
            hv_Add  = ((-hv_Mult) * hv_Min_COPY_INP_TMP) + hv_LowerLimit;
            //
            //Scale image
            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.ScaleImage(ho_Image_COPY_INP_TMP, out ExpTmpOutVar_0, hv_Mult, hv_Add);
                ho_Image_COPY_INP_TMP.Dispose();
                ho_Image_COPY_INP_TMP = ExpTmpOutVar_0;
            }
            //
            //Clip gray values if necessary
            //This must be done for each channel separately
            HOperatorSet.CountChannels(ho_Image_COPY_INP_TMP, out hv_Channels);
            HTuple end_val48  = hv_Channels;
            HTuple step_val48 = 1;

            for (hv_Index = 1; hv_Index.Continue(end_val48, step_val48); hv_Index = hv_Index.TupleAdd(step_val48))
            {
                ho_SelectedChannel.Dispose();
                HOperatorSet.AccessChannel(ho_Image_COPY_INP_TMP, out ho_SelectedChannel, hv_Index);
                HOperatorSet.MinMaxGray(ho_SelectedChannel, ho_SelectedChannel, 0, out hv_MinGray,
                                        out hv_MaxGray, out hv_Range);
                ho_LowerRegion.Dispose();
                HOperatorSet.Threshold(ho_SelectedChannel, out ho_LowerRegion, ((hv_MinGray.TupleConcat(
                                                                                     hv_LowerLimit))).TupleMin(), hv_LowerLimit);
                ho_UpperRegion.Dispose();
                HOperatorSet.Threshold(ho_SelectedChannel, out ho_UpperRegion, hv_UpperLimit,
                                       ((hv_UpperLimit.TupleConcat(hv_MaxGray))).TupleMax());
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.PaintRegion(ho_LowerRegion, ho_SelectedChannel, out ExpTmpOutVar_0,
                                             hv_LowerLimit, "fill");
                    ho_SelectedChannel.Dispose();
                    ho_SelectedChannel = ExpTmpOutVar_0;
                }
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.PaintRegion(ho_UpperRegion, ho_SelectedChannel, out ExpTmpOutVar_0,
                                             hv_UpperLimit, "fill");
                    ho_SelectedChannel.Dispose();
                    ho_SelectedChannel = ExpTmpOutVar_0;
                }
                if ((int)(new HTuple(hv_Index.TupleEqual(1))) != 0)
                {
                    ho_ImageScaled.Dispose();
                    HOperatorSet.CopyObj(ho_SelectedChannel, out ho_ImageScaled, 1, 1);
                }
                else
                {
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.AppendChannel(ho_ImageScaled, ho_SelectedChannel, out ExpTmpOutVar_0
                                                   );
                        ho_ImageScaled.Dispose();
                        ho_ImageScaled = ExpTmpOutVar_0;
                    }
                }
            }
            ho_Image_COPY_INP_TMP.Dispose();
            ho_SelectedChannel.Dispose();
            ho_LowerRegion.Dispose();
            ho_UpperRegion.Dispose();

            return;
        }
Exemplo n.º 12
0
        //DateTime t1, t2, t3, t4,t5,t6,t7;
        private void action()
        {
            // Local iconic variables

            HObject ho_GrayImage, ho_Image1;
            HObject ho_Image2, ho_Image3, ho_ImageResult1, ho_ImageResult2;
            HObject ho_ImageResult3, ho_im, ho_Region, ho_RegionConnection;
            HObject ho_RegionSelected, ho_RegionDilation, ho_RegionFillUp;
            HObject ho_RegionDifference, ho_ConnectedRegions1, ho_SelectedRegions1;
            HObject ho_Contour_JiaoQuanOuter, ho_Circle, ho_Contour_JiaoQuanInner;
            HObject ho_Region_WaiKuang, ho_RegionFillUp_WaiKuang, ho_ConnectedRegions_WaiKuang;
            HObject ho_SelectedRegions_WaiKuang, ho_Contour_WaiKuang;
            HObject ho_Image33;

            // Local control variables

            HTuple hv_Area2 = null, hv_Row = null, hv_Column = null;
            HTuple hv_AreaRegionSelected = null, hv_RowRegionSelected = null;
            HTuple hv_ColumnRegionSelected = null, hv_NumRegion = null;
            HTuple hv_ContourCount_JiaoQuanOuter = null, hv_RowFit_JiaoQuanOuter = null;
            HTuple hv_ColumnFit_JiaoQuanOuter = null, hv_RadiusFit_JiaoQuanOuter = null;
            HTuple hv_StartPhi = null, hv_EndPhi = null, hv_PointOrder = null;
            HTuple hv_RowSmallest_JiaoQuanOuter = null, hv_ColumnSmallest_JiaoQuanOuter = null;
            HTuple hv_RadiusSmallest_JiaoQuanOuter = null, hv_RowInner_JiaoQuanOuter = null;
            HTuple hv_ColumnInner_JiaoQuanOuter = null, hv_RadiusInner_JiaoQuanOuter = null;
            HTuple hv_RadiusRatio_JiaoQuanOuter = null, hv_Row_JiaoQuanOuter = null;
            HTuple hv_Col_JiaoQuanOuter = null, hv_GapAbsAll_JiaoQuanOuter = null;
            HTuple hv_i = null, hv_Distance1 = new HTuple(), hv_GapAbs = new HTuple();
            HTuple hv_Sorted = new HTuple(), hv_MaxGap_JiaoQuanOuter = null;
            HTuple hv_ContourCount_JiaoQuanInner = null, hv_RowFit_JiaoQuanInner = null;
            HTuple hv_ColumnFit_JiaoQuanInner = null, hv_RadiusFit_JiaoQuanInner = null;
            HTuple hv_RowSmallest_JiaoQuanInner = null, hv_ColumnSmallest_JiaoQuanInner = null;
            HTuple hv_RadiusSmallest_JiaoQuanInner = null, hv_RowInner_JiaoQuanInner = null;
            HTuple hv_ColumnInner_JiaoQuanInner = null, hv_RadiusInner_JiaoQuanInner = null;
            HTuple hv_RadiusRatio_JiaoQuanInner = null, hv_Row_JiaoQuanInner = null;
            HTuple hv_Col_JiaoQuanInner = null, hv_GapAbsAll_JiaoQuanInner = null;
            HTuple hv_MaxGap_JiaoQuanInner = null, hv_ContourCount_WaiKuang = null;
            HTuple hv_RowFit_WaiKuang = null, hv_ColumnFit_WaiKuang = null;
            HTuple hv_RadiusFit_WaiKuang = null, hv_RowSmallest_WaiKuang = null;
            HTuple hv_ColumnSmallest_WaiKuang = null, hv_RadiusSmallest_WaiKuang = null;
            HTuple hv_RowInner_WaiKuang = null, hv_ColumnInner_WaiKuang = null;
            HTuple hv_RadiusInner_WaiKuang = null, hv_RadiusRatio_WaiKuang = null;
            HTuple hv_Row_WaiKuang = null, hv_Col_WaiKuang = null;
            HTuple hv_GapAbsAll_WaiKuang = null, hv_MaxGap_WaiKuang = null;
            HTuple hv_NumConnected = null, hv_NumHoles = null, hv_Area_JiaoQuan = null;
            HTuple hv_Row_JiaoQuan = null, hv_Column_JiaoQuan = null;
            HTuple hv_Area_JiaoQuanOuter = null, hv_Column_JiaoQuanOuter = null;
            HTuple hv_Area_JiaoQuanInner = null, hv_Column_JiaoQuanInner = null;
            HTuple hv_a = null, hv_hd = null, hv_Deviation = null;

            //HTuple hv_result = new HTuple();
            // Initialize local and output iconic variables
            //HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_Image1);
            HOperatorSet.GenEmptyObj(out ho_Image2);
            HOperatorSet.GenEmptyObj(out ho_Image3);
            HOperatorSet.GenEmptyObj(out ho_ImageResult1);
            HOperatorSet.GenEmptyObj(out ho_ImageResult2);
            HOperatorSet.GenEmptyObj(out ho_ImageResult3);
            HOperatorSet.GenEmptyObj(out ho_im);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionConnection);
            HOperatorSet.GenEmptyObj(out ho_RegionSelected);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_Contour_JiaoQuanOuter);
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_Contour_JiaoQuanInner);
            HOperatorSet.GenEmptyObj(out ho_Region_WaiKuang);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp_WaiKuang);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions_WaiKuang);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions_WaiKuang);
            HOperatorSet.GenEmptyObj(out ho_Contour_WaiKuang);
            HOperatorSet.GenEmptyObj(out ho_Image33);

            //t3 = DateTime.Now;
            try
            {
                ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose();
                HOperatorSet.Decompose3(Image, out ho_Image1, out ho_Image2, out ho_Image3);
                ho_ImageResult1.Dispose(); ho_ImageResult2.Dispose(); ho_ImageResult3.Dispose();
                HOperatorSet.TransFromRgb(ho_Image1, ho_Image2, ho_Image3, out ho_ImageResult1, out ho_ImageResult2, out ho_ImageResult3, "hls");
                ho_ImageResult1.Dispose();
                HOperatorSet.SubImage(ho_Image1, ho_Image2, out ho_ImageResult1, 1, 0);
                ho_im.Dispose();
                HOperatorSet.MeanImage(ho_ImageResult1, out ho_im, 9, 9);
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageResult1, out ho_Region, 40, 255);
                //closing_circle (Region, Region, 3.5)
                ho_RegionConnection.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_RegionConnection);
                //select_shape (RegionConnection, RegionSelected, 'area', 'and', 10000, 9999999)
                ho_RegionSelected.Dispose();
                HOperatorSet.SelectShapeStd(ho_RegionConnection, out ho_RegionSelected, "max_area", 70);
                ho_RegionDilation.Dispose();
                HOperatorSet.DilationCircle(ho_RegionSelected, out ho_RegionDilation, 1.5);
                HOperatorSet.AreaCenter(ho_RegionDilation, out hv_Area2, out hv_Row, out hv_Column);
                ho_RegionFillUp.Dispose();
                HOperatorSet.FillUp(ho_RegionDilation, out ho_RegionFillUp);
                ho_RegionDifference.Dispose();
                HOperatorSet.Difference(ho_RegionFillUp, ho_RegionDilation, out ho_RegionDifference);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionDifference, out ho_ConnectedRegions1);
                ho_SelectedRegions1.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_SelectedRegions1, "max_area", 70);

                //JiaoQuanOuter胶圈外圆:圆度、直径
                HOperatorSet.AreaCenter(ho_RegionFillUp, out hv_AreaRegionSelected, out hv_RowRegionSelected, out hv_ColumnRegionSelected);
                hv_NumRegion = new HTuple(hv_AreaRegionSelected.TupleLength());

                ho_Contour_JiaoQuanOuter.Dispose();
                HOperatorSet.GenContourRegionXld(ho_RegionFillUp, out ho_Contour_JiaoQuanOuter, "border");
                HOperatorSet.ContourPointNumXld(ho_Contour_JiaoQuanOuter, out hv_ContourCount_JiaoQuanOuter);

                HOperatorSet.FitCircleContourXld(ho_Contour_JiaoQuanOuter, "atukey", -1, 2, 0,
                                                 10, 1, out hv_RowFit_JiaoQuanOuter, out hv_ColumnFit_JiaoQuanOuter, out hv_RadiusFit_JiaoQuanOuter,
                                                 out hv_StartPhi, out hv_EndPhi, out hv_PointOrder);

                HOperatorSet.SmallestCircleXld(ho_Contour_JiaoQuanOuter, out hv_RowSmallest_JiaoQuanOuter,
                                               out hv_ColumnSmallest_JiaoQuanOuter, out hv_RadiusSmallest_JiaoQuanOuter);

                HOperatorSet.InnerCircle(ho_RegionFillUp, out hv_RowInner_JiaoQuanOuter, out hv_ColumnInner_JiaoQuanOuter,
                                         out hv_RadiusInner_JiaoQuanOuter);
                //*半径比值 of 外接圆和内切圆
                hv_RadiusRatio_JiaoQuanOuter = hv_RadiusInner_JiaoQuanOuter / hv_RadiusSmallest_JiaoQuanOuter;

                //**Contour Point
                HOperatorSet.GetContourXld(ho_Contour_JiaoQuanOuter, out hv_Row_JiaoQuanOuter, out hv_Col_JiaoQuanOuter);
                hv_GapAbsAll_JiaoQuanOuter = new HTuple();
                HTuple end_val51  = hv_ContourCount_JiaoQuanOuter - 1;
                HTuple step_val51 = 3;
                for (hv_i = 0; hv_i.Continue(end_val51, step_val51); hv_i = hv_i.TupleAdd(step_val51))
                {
                    HOperatorSet.DistancePp(hv_Row_JiaoQuanOuter.TupleSelect(hv_i), hv_Col_JiaoQuanOuter.TupleSelect(
                                                hv_i), hv_RowFit_JiaoQuanOuter, hv_ColumnFit_JiaoQuanOuter, out hv_Distance1);
                    HOperatorSet.TupleAbs(hv_Distance1 - hv_RadiusFit_JiaoQuanOuter, out hv_GapAbs);
                    hv_GapAbsAll_JiaoQuanOuter = hv_GapAbsAll_JiaoQuanOuter.TupleConcat(hv_GapAbs);
                    HOperatorSet.TupleSort(hv_GapAbsAll_JiaoQuanOuter, out hv_Sorted);
                }

                hv_MaxGap_JiaoQuanOuter = hv_Sorted.TupleSelect(hv_ContourCount_JiaoQuanOuter - 1);

                //JiaoQuanInner胶圈内圆:圆度、直径
                HOperatorSet.AreaCenter(ho_SelectedRegions1, out hv_AreaRegionSelected, out hv_RowRegionSelected, out hv_ColumnRegionSelected);
                hv_NumRegion = new HTuple(hv_AreaRegionSelected.TupleLength());

                ho_Contour_JiaoQuanInner.Dispose();
                HOperatorSet.GenContourRegionXld(ho_SelectedRegions1, out ho_Contour_JiaoQuanInner, "border");
                HOperatorSet.ContourPointNumXld(ho_Contour_JiaoQuanInner, out hv_ContourCount_JiaoQuanInner);

                HOperatorSet.FitCircleContourXld(ho_Contour_JiaoQuanInner, "atukey", -1, 2, 0,
                                                 10, 1, out hv_RowFit_JiaoQuanInner, out hv_ColumnFit_JiaoQuanInner, out hv_RadiusFit_JiaoQuanInner,
                                                 out hv_StartPhi, out hv_EndPhi, out hv_PointOrder);

                ho_Circle.Dispose();
                HOperatorSet.GenCircle(out ho_Circle, hv_RowFit_JiaoQuanInner, hv_ColumnFit_JiaoQuanInner,
                                       hv_RadiusFit_JiaoQuanInner);

                HOperatorSet.SmallestCircleXld(ho_Contour_JiaoQuanInner, out hv_RowSmallest_JiaoQuanInner, out hv_ColumnSmallest_JiaoQuanInner, out hv_RadiusSmallest_JiaoQuanInner);
                //gen_circle (Circle1, RowSmallest_JiaoQuanInner, ColumnSmallest_JiaoQuanInner, RadiusSmallest_JiaoQuanInner)

                HOperatorSet.InnerCircle(ho_SelectedRegions1, out hv_RowInner_JiaoQuanInner, out hv_ColumnInner_JiaoQuanInner, out hv_RadiusInner_JiaoQuanInner);
                //gen_circle (Circle2, RowInner_JiaoQuanInner, ColumnInner_JiaoQuanInner, RadiusInner_JiaoQuanInner)
                //*半径比值 of 外接圆和内切圆
                hv_RadiusRatio_JiaoQuanInner = hv_RadiusInner_JiaoQuanInner / hv_RadiusSmallest_JiaoQuanInner;

                //**Contour Point
                HOperatorSet.GetContourXld(ho_Contour_JiaoQuanInner, out hv_Row_JiaoQuanInner, out hv_Col_JiaoQuanInner);
                hv_GapAbsAll_JiaoQuanInner = new HTuple();
                HTuple end_val89  = hv_ContourCount_JiaoQuanInner - 1;
                HTuple step_val89 = 3;
                for (hv_i = 0; hv_i.Continue(end_val89, step_val89); hv_i = hv_i.TupleAdd(step_val89))
                {
                    HOperatorSet.DistancePp(hv_Row_JiaoQuanInner.TupleSelect(hv_i), hv_Col_JiaoQuanInner.TupleSelect(
                                                hv_i), hv_RowFit_JiaoQuanInner, hv_ColumnFit_JiaoQuanInner, out hv_Distance1);
                    HOperatorSet.TupleAbs(hv_Distance1 - hv_RadiusFit_JiaoQuanInner, out hv_GapAbs);
                    hv_GapAbsAll_JiaoQuanInner = hv_GapAbsAll_JiaoQuanInner.TupleConcat(hv_GapAbs);
                    HOperatorSet.TupleSort(hv_GapAbsAll_JiaoQuanInner, out hv_Sorted);
                }

                hv_MaxGap_JiaoQuanInner = hv_Sorted.TupleSelect(hv_ContourCount_JiaoQuanInner - 1);


                //外框外圆:圆度、直径
                ho_GrayImage.Dispose();
                HOperatorSet.Rgb1ToGray(Image, out ho_GrayImage);
                ho_Region_WaiKuang.Dispose();
                HOperatorSet.Threshold(ho_GrayImage, out ho_Region_WaiKuang, 90, 255);
                ho_RegionFillUp_WaiKuang.Dispose();
                HOperatorSet.FillUp(ho_Region_WaiKuang, out ho_RegionFillUp_WaiKuang);
                ho_ConnectedRegions_WaiKuang.Dispose();
                HOperatorSet.Connection(ho_RegionFillUp_WaiKuang, out ho_ConnectedRegions_WaiKuang);
                ho_SelectedRegions_WaiKuang.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions_WaiKuang, out ho_SelectedRegions_WaiKuang, "max_area", 70);
                //smallest_circle (RegionFillUp_WaiKuang, Row2, Column2, Radius)
                //inner_circle (RegionFillUp_WaiKuang, Row3, Column3, Radius1)

                HOperatorSet.AreaCenter(ho_SelectedRegions_WaiKuang, out hv_AreaRegionSelected, out hv_RowRegionSelected, out hv_ColumnRegionSelected);
                hv_NumRegion = new HTuple(hv_AreaRegionSelected.TupleLength());

                ho_Contour_WaiKuang.Dispose();
                HOperatorSet.GenContourRegionXld(ho_SelectedRegions_WaiKuang, out ho_Contour_WaiKuang, "border");
                HOperatorSet.ContourPointNumXld(ho_Contour_WaiKuang, out hv_ContourCount_WaiKuang);

                HOperatorSet.FitCircleContourXld(ho_Contour_WaiKuang, "atukey", -1, 2, 0, 10,
                                                 1, out hv_RowFit_WaiKuang, out hv_ColumnFit_WaiKuang, out hv_RadiusFit_WaiKuang,
                                                 out hv_StartPhi, out hv_EndPhi, out hv_PointOrder);

                ho_Circle.Dispose();
                HOperatorSet.GenCircle(out ho_Circle, hv_RowFit_WaiKuang, hv_ColumnFit_WaiKuang,
                                       hv_RadiusFit_WaiKuang);

                HOperatorSet.SmallestCircleXld(ho_Contour_WaiKuang, out hv_RowSmallest_WaiKuang, out hv_ColumnSmallest_WaiKuang, out hv_RadiusSmallest_WaiKuang);
                //gen_circle (Circle1, RowSmallest_WaiKuang, ColumnSmallest_WaiKuang, RadiusSmallest_WaiKuang)

                HOperatorSet.InnerCircle(ho_SelectedRegions_WaiKuang, out hv_RowInner_WaiKuang, out hv_ColumnInner_WaiKuang, out hv_RadiusInner_WaiKuang);
                //gen_circle (Circle2, RowInner_WaiKuang, ColumnInner_WaiKuang, RadiusInner_WaiKuang)
                //*半径比值 of 外接圆和内切圆
                hv_RadiusRatio_WaiKuang = hv_RadiusInner_WaiKuang / hv_RadiusSmallest_WaiKuang;

                //**Contour Point
                HOperatorSet.GetContourXld(ho_Contour_WaiKuang, out hv_Row_WaiKuang, out hv_Col_WaiKuang);
                hv_GapAbsAll_WaiKuang = new HTuple();
                HTuple end_val135  = hv_ContourCount_WaiKuang - 1;
                HTuple step_val135 = 3;
                for (hv_i = 0; hv_i.Continue(end_val135, step_val135); hv_i = hv_i.TupleAdd(step_val135))
                {
                    HOperatorSet.DistancePp(hv_Row_WaiKuang.TupleSelect(hv_i), hv_Col_WaiKuang.TupleSelect(
                                                hv_i), hv_RowFit_WaiKuang, hv_ColumnFit_WaiKuang, out hv_Distance1);
                    HOperatorSet.TupleAbs(hv_Distance1 - hv_RadiusFit_WaiKuang, out hv_GapAbs);
                    hv_GapAbsAll_WaiKuang = hv_GapAbsAll_WaiKuang.TupleConcat(hv_GapAbs);
                    HOperatorSet.TupleSort(hv_GapAbsAll_WaiKuang, out hv_Sorted);
                }

                hv_MaxGap_WaiKuang = hv_Sorted.TupleSelect(hv_ContourCount_WaiKuang - 1);


                //脏污
                HOperatorSet.ConnectAndHoles(ho_RegionSelected, out hv_NumConnected, out hv_NumHoles);

                HOperatorSet.AreaCenter(ho_RegionFillUp, out hv_Area_JiaoQuan, out hv_Row_JiaoQuan, out hv_Column_JiaoQuan);
                HOperatorSet.AreaCenter(ho_RegionSelected, out hv_Area_JiaoQuanOuter, out hv_Row_JiaoQuanOuter, out hv_Column_JiaoQuanOuter);
                HOperatorSet.AreaCenter(ho_SelectedRegions1, out hv_Area_JiaoQuanInner, out hv_Row_JiaoQuanInner, out hv_Column_JiaoQuanInner);
                hv_a = (hv_Area_JiaoQuan - hv_Area_JiaoQuanOuter) - hv_Area_JiaoQuanInner;

                ho_Image33.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_SelectedRegions1, out ho_Image33);
                HOperatorSet.Intensity(ho_SelectedRegions1, ho_Image33, out hv_hd, out hv_Deviation);

                HOperatorSet.Union1(ho_RegionDilation, out RegionToDisp);

                //Result
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("橡皮圆度-半径比");
                hv_result = hv_result.TupleConcat(hv_RadiusRatio_JiaoQuanOuter.D);
                hv_result = hv_result.TupleConcat("橡皮圆度-最大值");
                hv_result = hv_result.TupleConcat(hv_MaxGap_JiaoQuanOuter.D);

                hv_result = hv_result.TupleConcat("橡皮内孔圆度-半径比");
                hv_result = hv_result.TupleConcat(hv_RadiusRatio_JiaoQuanInner.D);
                hv_result = hv_result.TupleConcat("橡皮内孔圆度-最大值");
                hv_result = hv_result.TupleConcat(hv_MaxGap_JiaoQuanInner.D);
                hv_result = hv_result.TupleConcat("橡皮内孔面积");
                hv_result = hv_result.TupleConcat(hv_Area_JiaoQuanInner.D);

                hv_result = hv_result.TupleConcat("橡皮面积");
                hv_result = hv_result.TupleConcat(hv_Area_JiaoQuanOuter.D);

                hv_result = hv_result.TupleConcat("橡皮灰尘");
                hv_result = hv_result.TupleConcat(hv_a.D);

                hv_result = hv_result.TupleConcat("外圆圆度-半径比");
                hv_result = hv_result.TupleConcat(hv_RadiusRatio_WaiKuang.D);
                hv_result = hv_result.TupleConcat("外圆圆度-最大值");
                hv_result = hv_result.TupleConcat(hv_MaxGap_WaiKuang.D);
                hv_result = hv_result.TupleConcat("外圆内圆直径");
                hv_result = hv_result.TupleConcat(hv_RadiusInner_WaiKuang.D * pixeldist * 2);
                hv_result = hv_result.TupleConcat("外圆外圆直径");
                hv_result = hv_result.TupleConcat(hv_RadiusSmallest_WaiKuang.D * pixeldist * 2);

                hv_result = hv_result.TupleConcat("内孔灰度");
                hv_result = hv_result.TupleConcat(hv_hd.D);
                result    = hv_result.Clone();

                //t4 = DateTime.Now;
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("橡皮圆度-半径比");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("橡皮圆度-最大值");
                hv_result = hv_result.TupleConcat(0);

                hv_result = hv_result.TupleConcat("橡皮内孔圆度-半径比");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("橡皮内孔圆度-最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("橡皮内孔面积");
                hv_result = hv_result.TupleConcat(0);

                hv_result = hv_result.TupleConcat("橡皮面积");
                hv_result = hv_result.TupleConcat(0);

                hv_result = hv_result.TupleConcat("橡皮灰尘");
                hv_result = hv_result.TupleConcat(0);

                hv_result = hv_result.TupleConcat("外圆圆度-半径比");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("外圆圆度-最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("外圆内圆直径");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("外圆外圆直径");
                hv_result = hv_result.TupleConcat(0);

                hv_result = hv_result.TupleConcat("内孔灰度");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();
            }
            finally
            {
                //ho_Image.Dispose();
                ho_GrayImage.Dispose();
                ho_Image1.Dispose();
                ho_Image2.Dispose();
                ho_Image3.Dispose();
                ho_ImageResult1.Dispose();
                ho_ImageResult2.Dispose();
                ho_ImageResult3.Dispose();
                ho_im.Dispose();
                ho_Region.Dispose();
                ho_RegionConnection.Dispose();
                ho_RegionSelected.Dispose();
                ho_RegionDilation.Dispose();
                ho_RegionFillUp.Dispose();
                ho_RegionDifference.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_Contour_JiaoQuanOuter.Dispose();
                ho_Circle.Dispose();
                ho_Contour_JiaoQuanInner.Dispose();
                ho_Region_WaiKuang.Dispose();
                ho_RegionFillUp_WaiKuang.Dispose();
                ho_ConnectedRegions_WaiKuang.Dispose();
                ho_SelectedRegions_WaiKuang.Dispose();
                ho_Contour_WaiKuang.Dispose();
                ho_Image33.Dispose();
                algorithm.Region.Dispose();
            }
        }
Exemplo n.º 13
0
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image = null, ho_ImageAffinTrans = null;

        // Local control variables

        HTuple hv_WindowHandle = new HTuple(), hv_DeviceIdentifier = null;
        HTuple hv_Index = null, hv_DeviceName = new HTuple(), hv_DeviceVendor = new HTuple();
        HTuple hv_Message = new HTuple(), hv_DeviceIndex = null;
        HTuple hv_DeviceHandle = new HTuple(), hv_HomMat2DIdentity = new HTuple();
        HTuple hv_HomMat2DScale = new HTuple(), hv_HomMat2D = new HTuple();
        HTuple hv_Loops = new HTuple(), hv_Before = new HTuple();
        HTuple hv_After = new HTuple(), hv_TimeGPU = new HTuple();
        HTuple hv_Grayval = new HTuple(), hv_Pointer = new HTuple();
        HTuple hv_Type = new HTuple(), hv_Width = new HTuple();
        HTuple hv_Height = new HTuple(), hv_TimeGPUinclTransfer = new HTuple();
        HTuple hv_TimeCPU = new HTuple(), hv_SpeedUp = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_ImageAffinTrans);
        try
        {
            //This example shows how to use compute devices with HALCON.
            //
            dev_update_off();
            //dev_close_window(...);
            dev_open_window_fit_size(0, 0, 640, 480, -1, -1, out hv_WindowHandle);
            set_display_font(hv_ExpDefaultWinHandle, 16, "mono", "true", "false");
            //
            //Get list of all available compute devices.
            HOperatorSet.QueryAvailableComputeDevices(out hv_DeviceIdentifier);
            //
            //End example if no device could be found.
            if ((int)(new HTuple((new HTuple(hv_DeviceIdentifier.TupleLength())).TupleEqual(
                                     0))) != 0)
            {
                ho_Image.Dispose();
                ho_ImageAffinTrans.Dispose();

                return;
            }
            //
            //Display basic information on detected devices.
            disp_message(hv_ExpDefaultWinHandle, new HTuple(new HTuple("Found ") + (new HTuple(hv_DeviceIdentifier.TupleLength()
                                                                                               ))) + " Compute Device(s):", "window", 12, 12, "black", "true");
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_DeviceIdentifier.TupleLength()
                                                                  )) - 1); hv_Index = (int)hv_Index + 1)
            {
                HOperatorSet.GetComputeDeviceInfo(hv_DeviceIdentifier.TupleSelect(hv_Index),
                                                  "name", out hv_DeviceName);
                HOperatorSet.GetComputeDeviceInfo(hv_DeviceIdentifier.TupleSelect(hv_Index),
                                                  "vendor", out hv_DeviceVendor);
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[hv_Index] = (((("Device #" + hv_Index) + ": ") + hv_DeviceVendor) + " ") + hv_DeviceName;
            }
            disp_message(hv_ExpDefaultWinHandle, hv_Message, "window", 42, 12, "white",
                         "false");
            disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");
            HDevelopStop();
            //
            //Perform a small benchmark for all devices.
            for (hv_DeviceIndex = 0; (int)hv_DeviceIndex <= (int)((new HTuple(hv_DeviceIdentifier.TupleLength()
                                                                              )) - 1); hv_DeviceIndex = (int)hv_DeviceIndex + 1)
            {
                HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);
                disp_message(hv_ExpDefaultWinHandle, ("Activating Device #" + hv_DeviceIndex) + " and performing benchmark...",
                             "window", 12, 12, "black", "true");
                //Open device.
                HOperatorSet.OpenComputeDevice(hv_DeviceIdentifier.TupleSelect(hv_DeviceIndex),
                                               out hv_DeviceHandle);
                //Deactivate asynchronous execution to obtain reliable benchmarks.
                HOperatorSet.SetComputeDeviceParam(hv_DeviceHandle, "asynchronous_execution",
                                                   "false");
                //Call affine_trans_image on this device and measure the obtained speed-up.
                //Activate the compute device and perform the initialization of affine_trans_image.
                //If this is omitted, the initialization is performed with the first
                //call of affine_trans_image.
                HOperatorSet.InitComputeDevice(hv_DeviceHandle, "affine_trans_image");
                HOperatorSet.ActivateComputeDevice(hv_DeviceHandle);
                //create benchmark input data
                ho_Image.Dispose();
                HOperatorSet.ReadImage(out ho_Image, "rings_and_nuts");
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                HOperatorSet.HomMat2dScale(hv_HomMat2DIdentity, 0.9, 0.9, 320, 240, out hv_HomMat2DScale);
                HOperatorSet.HomMat2dRotate(hv_HomMat2DScale, 0.78, 320, 240, out hv_HomMat2D);
                //Set number of benchmark loops.
                hv_Loops = 200;
                //One call to fill caches.
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                              "constant", "false");
                HOperatorSet.CountSeconds(out hv_Before);
                HTuple end_val50  = hv_Loops;
                HTuple step_val50 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val50, step_val50); hv_Index = hv_Index.TupleAdd(step_val50))
                {
                    ho_ImageAffinTrans.Dispose();
                    HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                                  "constant", "false");
                }
                HOperatorSet.CountSeconds(out hv_After);
                hv_TimeGPU = ((hv_After - hv_Before) * 1000.0) / hv_Loops;
                //
                //Perform a slightly modified version of this benchmark to include
                //host->device and device->host transfer.
                HOperatorSet.GetGrayval(ho_Image, 0, 0, out hv_Grayval);
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                              "constant", "false");
                HOperatorSet.CountSeconds(out hv_Before);
                HTuple end_val61  = hv_Loops;
                HTuple step_val61 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val61, step_val61); hv_Index = hv_Index.TupleAdd(step_val61))
                {
                    //set_grayval ensures that Image must be transferred to the device again.
                    HOperatorSet.SetGrayval(ho_Image, 0, 0, hv_Grayval);
                    ho_ImageAffinTrans.Dispose();
                    HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                                  "constant", "false");
                    //get_image_pointer1 ensures that ImageAffinTrans is transferred back to host.
                    HOperatorSet.GetImagePointer1(ho_ImageAffinTrans, out hv_Pointer, out hv_Type,
                                                  out hv_Width, out hv_Height);
                }
                HOperatorSet.CountSeconds(out hv_After);
                hv_TimeGPUinclTransfer = ((hv_After - hv_Before) * 1000.0) / hv_Loops;
                //
                //Deactivate the device and perform the same benchmark on the CPU.
                HOperatorSet.DeactivateComputeDevice(hv_DeviceHandle);
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                              "constant", "false");
                HOperatorSet.CountSeconds(out hv_Before);
                HTuple end_val75  = hv_Loops;
                HTuple step_val75 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val75, step_val75); hv_Index = hv_Index.TupleAdd(step_val75))
                {
                    ho_ImageAffinTrans.Dispose();
                    HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                                  "constant", "false");
                }
                HOperatorSet.CountSeconds(out hv_After);
                hv_TimeCPU = ((hv_After - hv_Before) * 1000.0) / hv_Loops;
                hv_SpeedUp = hv_TimeCPU / hv_TimeGPU;
                hv_Message = "affine_trans_image runtimes:";
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[1] = ((("Compute Device #" + hv_DeviceIndex) + " (excl. transfer): ") + (hv_TimeGPU.TupleString(
                                                                                                        ".2f"))) + " ms";
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[2] = ((("Compute Device #" + hv_DeviceIndex) + " (incl. transfer): ") + (hv_TimeGPUinclTransfer.TupleString(
                                                                                                        ".2f"))) + " ms";
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[3] = ("CPU:                                " + (hv_TimeCPU.TupleString(
                                                                               ".2f"))) + " ms";
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[4] = " ";
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[5] = "Potential speedup: " + (hv_SpeedUp.TupleString(".1f"));
                disp_message(hv_ExpDefaultWinHandle, hv_Message, "window", 42, 12, "white",
                             "false");
                if ((int)(new HTuple(hv_DeviceIndex.TupleLess((new HTuple(hv_DeviceIdentifier.TupleLength()
                                                                          )) - 1))) != 0)
                {
                    disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");
                    HDevelopStop();
                }
            }
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_Image.Dispose();
            ho_ImageAffinTrans.Dispose();

            throw HDevExpDefaultException;
        }
        ho_Image.Dispose();
        ho_ImageAffinTrans.Dispose();
    }
Exemplo n.º 14
0
        // Local procedures
        //public void FitCirCle(HObject ho_image, HTuple hv_startangle, HTuple hv_endangle,
        //    HTuple hv_bw, HTuple hv_num, HTuple hv_Radius, HTuple hv_colcenter, HTuple hv_rowcenter,
        //    HTuple hv_len1, HTuple hv_len2, HTuple hv_sigma, HTuple hv_thresholdval, HTuple hv_transition,
        //    HTuple hv_select, HTuple hv_windowhandle, HTuple hv_showrow, HTuple hv_showcol,
        //    out HTuple hv_RowCircleCenterFit, out HTuple hv_ColumnCircleCenterFit, out HTuple hv_RadiusCircleCenterFit,
        //    out HTuple hv_StartPhiCircleCenterFit, out HTuple hv_EndPhiCircleCenterFit,
        //    out HTuple hv_PointOrderCircleCenterFit)
        //{
        //    // Local iconic variables

        //    HObject ho_Rectangle = null, ho_RectangleSM = null;
        //    HObject ho_ImageReduced = null, ho_Contour = null;

        //    // Local control variables

        //    HTuple hv_t1 = null, hv_rowedgearr = null;
        //    HTuple hv_coledgearr = null, hv_step = null, hv_angle = null;
        //    HTuple hv_x = new HTuple(), hv_y = new HTuple(), hv_anglerad = new HTuple();
        //    HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
        //    HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
        //    HTuple hv_width = new HTuple(), hv_high = new HTuple();
        //    HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
        //    HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge = new HTuple();
        //    HTuple hv_ColumnEdge = new HTuple(), hv_Amplitude = new HTuple();
        //    HTuple hv_Distance = new HTuple(), hv_t2 = new HTuple();
        //    HTuple hv_t = new HTuple(), hv_msg = new HTuple();
        //    // Initialize local and output iconic variables
        //    HOperatorSet.GenEmptyObj(out ho_Rectangle);
        //    HOperatorSet.GenEmptyObj(out ho_RectangleSM);
        //    HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        //    HOperatorSet.GenEmptyObj(out ho_Contour);
        //    hv_RowCircleCenterFit = new HTuple();
        //    hv_ColumnCircleCenterFit = new HTuple();
        //    hv_RadiusCircleCenterFit = new HTuple();
        //    hv_StartPhiCircleCenterFit = new HTuple();
        //    hv_EndPhiCircleCenterFit = new HTuple();
        //    hv_PointOrderCircleCenterFit = new HTuple();
        //    try
        //    {
        //        HOperatorSet.CountSeconds(out hv_t1);
        //        hv_rowedgearr = new HTuple();
        //        hv_coledgearr = new HTuple();
        //        hv_step = (((hv_startangle - hv_endangle)).TupleAbs()) / hv_num;
        //        HTuple end_val4 = hv_endangle;
        //        HTuple step_val4 = hv_step;
        //        for (hv_angle = hv_startangle; hv_angle.Continue(end_val4, step_val4); hv_angle = hv_angle.TupleAdd(step_val4))
        //        {
        //            HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");
        //            hv_x = hv_colcenter + (hv_Radius * (((hv_angle.TupleRad())).TupleCos()));
        //            hv_y = hv_rowcenter - (hv_Radius * (((hv_angle.TupleRad())).TupleSin()));
        //            if ((int)(new HTuple(hv_bw.TupleEqual(1))) != 0)
        //            {
        //                hv_anglerad = ((hv_angle + 180)).TupleRad();
        //            }
        //            else
        //            {
        //                hv_anglerad = hv_angle.TupleRad();
        //            }

        //            ho_Rectangle.Dispose();
        //            HOperatorSet.GenRectangle2(out ho_Rectangle, hv_y, hv_x, hv_anglerad, hv_len1,
        //                hv_len2);
        //            HOperatorSet.SmallestRectangle1(ho_Rectangle, out hv_Row1, out hv_Column1,
        //                out hv_Row2, out hv_Column2);
        //            ho_RectangleSM.Dispose();
        //            HOperatorSet.GenRectangle1(out ho_RectangleSM, hv_Row1 - 1, hv_Column1 - 1, hv_Row2 + 1,
        //                hv_Column2 + 1);
        //            hv_width = (((hv_Column2 - hv_Column1)).TupleAbs()) + 2;
        //            hv_high = (((hv_Row2 - hv_Row1)).TupleAbs()) + 2;
        //            ho_ImageReduced.Dispose();
        //            HOperatorSet.ReduceDomain(ho_image, ho_RectangleSM, out ho_ImageReduced);
        //            HOperatorSet.GetImageSize(ho_ImageReduced, out hv_Width, out hv_Height);

        //            HOperatorSet.GenMeasureRectangle2(hv_y, hv_x, hv_anglerad, hv_len1, hv_len2,
        //                hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
        //            HOperatorSet.MeasurePos(ho_image, hv_MeasureHandle, hv_sigma, hv_thresholdval,
        //                hv_transition, hv_select, out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude,
        //                out hv_Distance);
        //            HOperatorSet.CloseMeasure(hv_MeasureHandle);
        //            HOperatorSet.DispRectangle2(hv_ExpDefaultWinHandle, hv_y, hv_x, hv_anglerad,
        //                hv_len1, hv_len2);
        //            if ((int)(new HTuple((new HTuple(hv_RowEdge.TupleLength())).TupleGreater(
        //                0))) != 0)
        //            {
        //                HOperatorSet.DispCross(hv_ExpDefaultWinHandle, hv_RowEdge, hv_ColumnEdge,
        //                    36, 0);
        //                hv_rowedgearr = hv_rowedgearr.TupleConcat(hv_RowEdge);
        //                hv_coledgearr = hv_coledgearr.TupleConcat(hv_ColumnEdge);
        //            }

        //        }
        //        if ((int)(new HTuple((new HTuple(hv_rowedgearr.TupleLength())).TupleGreater(
        //            3))) != 0)
        //        {
        //            ho_Contour.Dispose();
        //            HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_rowedgearr, hv_coledgearr);
        //            HOperatorSet.FitCircleContourXld(ho_Contour, "atukey", -1, 0, 0, 3, 2, out hv_RowCircleCenterFit,
        //                out hv_ColumnCircleCenterFit, out hv_RadiusCircleCenterFit, out hv_StartPhiCircleCenterFit,
        //                out hv_EndPhiCircleCenterFit, out hv_PointOrderCircleCenterFit);
        //            HOperatorSet.DispCircle(hv_ExpDefaultWinHandle, hv_RowCircleCenterFit, hv_ColumnCircleCenterFit,
        //                hv_RadiusCircleCenterFit);

        //            HOperatorSet.CountSeconds(out hv_t2);
        //            hv_t = hv_t2 - hv_t1;
        //            hv_msg = (((hv_t + ",x:") + hv_ColumnCircleCenterFit) + ",y:") + hv_RowCircleCenterFit;
        //            disp_message(hv_ExpDefaultWinHandle, hv_msg, "window", hv_showrow, hv_showcol,
        //                "black", "true");
        //        }
        //        ho_Rectangle.Dispose();
        //        ho_RectangleSM.Dispose();
        //        ho_ImageReduced.Dispose();
        //        ho_Contour.Dispose();

        //        return;
        //    }
        //    catch (HalconException HDevExpDefaultException)
        //    {
        //        ho_Rectangle.Dispose();
        //        ho_RectangleSM.Dispose();
        //        ho_ImageReduced.Dispose();
        //        ho_Contour.Dispose();

        //        throw HDevExpDefaultException;
        //    }
        //}

        public bool FitCirCle(HObject ho_image, HTuple hv_startangle, HTuple hv_endangle,
                              HTuple hv_bw, HTuple hv_num, HTuple hv_Radius, HTuple hv_colcenter, HTuple hv_rowcenter,
                              HTuple hv_len1, HTuple hv_len2, HTuple hv_sigma, HTuple hv_thresholdval, HTuple hv_transition,
                              HTuple hv_select, HTuple hv_windowhandle, HTuple hv_showrow, HTuple hv_showcol,
                              out HTuple hv_RowCircleCenterFit, out HTuple hv_ColumnCircleCenterFit, out HTuple hv_RadiusCircleCenterFit,
                              out HTuple hv_StartPhiCircleCenterFit, out HTuple hv_EndPhiCircleCenterFit,
                              out HTuple hv_PointOrderCircleCenterFit)
        {
            // Local iconic variables
            HObject ho_Rectangle = null, ho_RectangleSM = null;
            HObject ho_ImageReduced = null, ho_Contour = null;

            // Local control variables

            HTuple hv_t1 = null, hv_rowedgearr = null;
            HTuple hv_coledgearr = null, hv_step = null, hv_angle = null;
            HTuple hv_anglerad = new HTuple(), hv_x = new HTuple();
            HTuple hv_y = new HTuple(), hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
            HTuple hv_width = new HTuple(), hv_high = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge = new HTuple();
            HTuple hv_ColumnEdge = new HTuple(), hv_Amplitude = new HTuple();
            HTuple hv_Distance = new HTuple(), hv_t2 = new HTuple();
            HTuple hv_t = new HTuple(), hv_msg = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_RectangleSM);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            hv_RowCircleCenterFit        = new HTuple();
            hv_ColumnCircleCenterFit     = new HTuple();
            hv_RadiusCircleCenterFit     = new HTuple();
            hv_StartPhiCircleCenterFit   = new HTuple();
            hv_EndPhiCircleCenterFit     = new HTuple();
            hv_PointOrderCircleCenterFit = new HTuple();
            hv_RowCircleCenterFit        = 0;
            hv_ColumnCircleCenterFit     = 0;
            hv_RadiusCircleCenterFit     = 0;
            try
            {
                if (ho_image == null || !ho_image.IsInitialized())
                {
                    return(false);
                }
                HOperatorSet.CountSeconds(out hv_t1);
                hv_rowedgearr = new HTuple();
                hv_coledgearr = new HTuple();
                hv_step       = (((hv_startangle - hv_endangle)).TupleAbs()) / hv_num;
                HTuple end_val4  = hv_endangle;
                HTuple step_val4 = hv_step;
                for (hv_angle = hv_startangle; hv_angle.Continue(end_val4, step_val4); hv_angle = hv_angle.TupleAdd(step_val4))
                {
                    HOperatorSet.SetDraw(hv_windowhandle, "margin");
                    try
                    {
                        hv_x = hv_colcenter + (hv_Radius * (((hv_angle.TupleRad())).TupleCos()));
                        hv_y = hv_rowcenter - (hv_Radius * (((hv_angle.TupleRad())).TupleSin()));
                        if ((int)(new HTuple(hv_bw.TupleEqual(1))) != 0)
                        {
                            hv_anglerad = ((hv_angle + 180)).TupleRad();
                        }
                        else
                        {
                            hv_anglerad = hv_angle.TupleRad();
                        }
                        ho_Rectangle.Dispose();
                        HOperatorSet.GenRectangle2(out ho_Rectangle, hv_y, hv_x, hv_anglerad, hv_len1,
                                                   hv_len2);
                        HOperatorSet.SmallestRectangle1(ho_Rectangle, out hv_Row1, out hv_Column1,
                                                        out hv_Row2, out hv_Column2);
                        ho_RectangleSM.Dispose();
                        HOperatorSet.GenRectangle1(out ho_RectangleSM, hv_Row1 - 1, hv_Column1 - 1, hv_Row2 + 1,
                                                   hv_Column2 + 1);
                        hv_width = (((hv_Column2 - hv_Column1)).TupleAbs()) + 2;
                        hv_high  = (((hv_Row2 - hv_Row1)).TupleAbs()) + 2;
                        ho_ImageReduced.Dispose();
                        HOperatorSet.ReduceDomain(ho_image, ho_RectangleSM, out ho_ImageReduced);
                        HOperatorSet.GetImageSize(ho_ImageReduced, out hv_Width, out hv_Height);

                        HOperatorSet.GenMeasureRectangle2(hv_y, hv_x, hv_anglerad, hv_len1, hv_len2,
                                                          hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
                        HOperatorSet.MeasurePos(ho_ImageReduced, hv_MeasureHandle, hv_sigma, hv_thresholdval,
                                                hv_transition, hv_select, out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude,
                                                out hv_Distance);
                        HOperatorSet.CloseMeasure(hv_MeasureHandle);
                        //disp_rectangle2 (windowhandle, y, x, anglerad, len1, len2)
                        if ((int)(new HTuple((new HTuple(hv_RowEdge.TupleLength())).TupleGreater(
                                                 0))) != 0)
                        {
                            //disp_cross (windowhandle, RowEdge, ColumnEdge, 36, 0)
                            hv_rowedgearr = hv_rowedgearr.TupleConcat(hv_RowEdge);
                            hv_coledgearr = hv_coledgearr.TupleConcat(hv_ColumnEdge);
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
                bool brtn = true;
                if ((int)(new HTuple((new HTuple(hv_rowedgearr.TupleLength())).TupleGreater(
                                         3))) != 0)
                {
                    if (hv_rowedgearr.Length * 0.7 > 3)
                    {
                        ho_Contour.Dispose();

                        HOperatorSet.SetColor(hv_windowhandle, "green");
                        HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_rowedgearr, hv_coledgearr);
                        HOperatorSet.FitCircleContourXld(ho_Contour, "atukey", -1, 0, 0, 15, 6, out hv_RowCircleCenterFit,
                                                         out hv_ColumnCircleCenterFit, out hv_RadiusCircleCenterFit, out hv_StartPhiCircleCenterFit,
                                                         out hv_EndPhiCircleCenterFit, out hv_PointOrderCircleCenterFit);
                        HOperatorSet.DispCircle(hv_windowhandle, hv_RowCircleCenterFit, hv_ColumnCircleCenterFit,
                                                hv_RadiusCircleCenterFit);

                        HOperatorSet.CountSeconds(out hv_t2);
                        hv_t   = hv_t2 - hv_t1;
                        hv_msg = (((hv_t + ",x:") + hv_ColumnCircleCenterFit) + ",y:") + hv_RowCircleCenterFit;
                        //disp_message(hv_ExpDefaultWinHandle, hv_msg, "window", hv_showrow, hv_showcol,
                        //    "black", "true");
                        HalconExternFunExport.disp_message(hv_windowhandle, hv_msg, "window", 100, 200, "green", "false");
                        _logger.Warn(m_strStepName + "找圆成功:" + hv_msg.S);
                        brtn = true;
                    }
                    else
                    {
                        brtn = false;
                    }
                }
                else
                {
                    brtn = false;
                }
                ho_Rectangle.Dispose();
                ho_RectangleSM.Dispose();
                ho_ImageReduced.Dispose();
                ho_Contour.Dispose();

                return(brtn);
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Rectangle.Dispose();
                ho_RectangleSM.Dispose();
                ho_ImageReduced.Dispose();
                ho_Contour.Dispose();
                _logger.Warn(m_strStepName + "找圆失败:" + HDevExpDefaultException.Message);
                // throw HDevExpDefaultException;
                return(false);
            }
        }
Exemplo n.º 15
0
        private void btn_TrainOcr_Click(object sender, EventArgs e)
        {
            if (hDisplay1.GetSearchRegions().Count == 0)
            {
                MessageBox.Show("请先添加搜索区域");
                return;
            }
            if (string.IsNullOrEmpty(txt_CharacterTrained.Text))
            {
                MessageBox.Show("请先填写要训练的字符");
                return;
            }
            HTuple  hv_Number;
            HObject _roi              = hDisplay1.GetSearchRegions().ElementAt(0);
            HObject _region           = new HObject();
            HObject _imagereduced     = new HObject();
            HObject ho_ObjectSelected = new HObject();

            HOperatorSet.ReduceDomain(m_image, _roi, out _imagereduced);
            HOperatorSet.Threshold(_imagereduced, out _region, m_OcrParam.GrayMin, m_OcrParam.GrayMax);
            HOperatorSet.Connection(_region, out _region);
            HOperatorSet.SelectShape(_region, out _region, "area", "and", m_OcrParam.AreaMin, m_OcrParam.AreaMax);
            if (cklb_SortRegionModle.GetItemChecked(0))
            {
                HOperatorSet.SortRegion(_region, out _region, "character", "true", "row");
            }
            else
            {
                HOperatorSet.SortRegion(_region, out _region, "character", "true", "column");
            }
            HOperatorSet.CountObj(_region, out hv_Number);

            if (hv_Number.I == 0)
            {
                MessageBox.Show("分割字符为空");
                return;
            }
            string[] _characterTrained = new string[] { };
            _characterTrained = txt_CharacterTrained.Text.Split(',');
            if (_characterTrained.Length != hv_Number.I)
            {
                MessageBox.Show("字符区域与字符个数不相等");
                return;
            }

            HTuple         hv_Index     = new HTuple();
            List <RegionX> _listregionx = new List <RegionX>();

            for (hv_Index = 1; hv_Index.Continue(hv_Number, 1); hv_Index = hv_Index.TupleAdd(1))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(_region, out ho_ObjectSelected, hv_Index);
                RegionX _hregion = new RegionX(ho_ObjectSelected.CopyObj(1, -1), "green");
                _listregionx.Add(_hregion);
            }
            string path             = string.Empty;
            string Foldername       = string.Empty;
            string trffilepath      = string.Empty;
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            fbd.ShowNewFolderButton = true;
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                path = fbd.SelectedPath;
                if (!Directory.Exists(path))
                {
                    MessageBox.Show($"不存在 '{path}' 路径!");
                    return;
                }
                string[] _f = path.Split('\\');
                Foldername  = _f[_f.Length - 1];
                trffilepath = path + "\\" + Foldername + ".trf";
            }
            else
            {
                return;
            }
            HObject _characterRegion = new HObject();

            HOperatorSet.GenEmptyObj(out _characterRegion);
            for (int i = 0; i < hv_Number.I; i++)
            {
                HOperatorSet.AppendOcrTrainf(_region[i + 1], m_image, _characterTrained[i], trffilepath);
            }

            HTuple _ocrhandle = new HTuple();
            HTuple error      = new HTuple();
            HTuple errorlog   = new HTuple();

            HOperatorSet.CreateOcrClassMlp(m_TrainParam.CharacterWidth, m_TrainParam.CharacterHeiht, m_TrainParam.ZoomModle, "default", _characterTrained,
                                           80, "none", 10, 42, out _ocrhandle);
            HOperatorSet.TrainfOcrClassMlp(_ocrhandle, trffilepath, 200, 1, 0.01, out error, out errorlog);
            HOperatorSet.WriteOcrTrainf(_region, m_image, _characterTrained, trffilepath);
            HOperatorSet.WriteOcrClassMlp(_ocrhandle, path + "\\" + Foldername);
            DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, _listregionx, null);
        }
Exemplo n.º 16
0
        private void action()
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_Rectangle, ho_ImageReduced;
            HObject ho_Region, ho_ConnectedRegions, ho_SelectedRegions;
            HObject ho_Rectangle1, ho_ImageRotate, ho_Region1, ho_RegionAffineTrans;
            HObject ho_Region2, ho_RegionUnionu, ho_RegionUniond, ho_Contours;
            HObject ho_SelectedContours, ho_SmoothedContours1, ho_ClippedContoursu;
            HObject ho_ClippedContoursd, ho_Rectangle2, ho_ClippedContoursuObjectSelected = null;
            HObject ho_ClippedContoursdObjectSelected = null, ho_Circle = null;
            HObject ho_Circle1 = null, ho_ConnectedRegions1, ho_SelectedRegions1;
            HObject ho_SortedRegionssdj, ho_Region3, ho_ConnectedRegions2;
            HObject ho_SelectedRegions2, ho_SortedRegionsxdj, ho_RegionLines = null;
            HObject ho_RegionAffineTrans1;

            // Local control variables

            HTuple hv_Width = null, hv_Height = null;
            HTuple hv_jd = null, hv_Row = null, hv_Column = null, hv_Phi = null;
            HTuple hv_Length1 = null, hv_Length2 = null, hv_HomMat2DIdentity = null;
            HTuple hv_HomMat2DRotate = null, hv_Row1 = null, hv_Column1 = null;
            HTuple hv_Row2 = null, hv_Column2 = null, hv_Rown = null;
            HTuple hv_Rowu = null, hv_Columnu = null, hv_Phiu = null;
            HTuple hv_Length1u = null, hv_Length2u = null, hv_MeasureHandleu = null;
            HTuple hv_GrayValuesu = null, hv_Sigmau = null, hv_Functionu = null;
            HTuple hv_SmoothedFunctionu = null, hv_FirstDerivativeu = null;
            HTuple hv_ZeroCrossingsu = null, hv_RowStartu = null, hv_ColStartu = null;
            HTuple hv_RowEndu = null, hv_ColEndu = null, hv_iu = null;
            HTuple hv_Rowd = null, hv_Columnd = null, hv_Phid = null;
            HTuple hv_Length1d = null, hv_Length2d = null, hv_MeasureHandled = null;
            HTuple hv_GrayValuesd = null, hv_Sigmad = null, hv_Functiond = null;
            HTuple hv_SmoothedFunctiond = null, hv_FirstDerivatived = null;
            HTuple hv_ZeroCrossingsd = null, hv_RowStartd = null, hv_ColStartd = null;
            HTuple hv_RowEndd = null, hv_ColEndd = null, hv_id = null;
            HTuple hv_RowIntuAll = null, hv_ColumnIntuAll = null, hv_u1 = null;
            HTuple hv_i = null, hv_Length = new HTuple(), hv_Indices1 = new HTuple();
            HTuple hv_RowIntu = new HTuple(), hv_ColumnIntu = new HTuple();
            HTuple hv_IsOverlappingu = new HTuple(), hv_RowIntdAll = null;
            HTuple hv_ColumnIntdAll = null, hv_j = null, hv_RowIntd = new HTuple();
            HTuple hv_ColumnIntd = new HTuple(), hv_IsOverlappingd = new HTuple();
            HTuple hv_Sortedu = null, hv_Sortedd = null, hv_RowIntuu = null;
            HTuple hv_ColumnIntuu = null, hv_RowIntud = null, hv_ColumnIntud = null;
            HTuple hv_uu = null, hv_ud = null, hv_ub1 = null, hv_RowIntdu = null;
            HTuple hv_ColumnIntdu = null, hv_RowIntdd = null, hv_ColumnIntdd = null;
            HTuple hv_du = null, hv_dd = null, hv_db1 = null, hv_Areasdj = null;
            HTuple hv_Rowsdj = null, hv_Columnsdj = null, hv_sdj = null;
            HTuple hv_Areaxdj = null, hv_Rowxdj = null, hv_Columnxdj = null;
            HTuple hv_xdj = null, hv_djsl1 = null, hv_djsl = null;
            HTuple hv_Mean1 = null, hv_Mean2 = null, hv_Mean3 = null;
            HTuple hv_Mean4 = null, hv_Columnuu = null, hv_Columndd = null;
            HTuple hv_AngleAll = null, hv_l = null, hv_Angle = new HTuple();
            HTuple hv_DegAll = null, hv_Mean5 = null, hv_Mean8 = null;
            HTuple hv_Columnuum = null, hv_uum1 = null, hv_uus = null;
            HTuple hv_Columnddm = null, hv_ddm = null, hv_dds = null;
            HTuple hv_ColumnmArr = null, hv_Mean9 = null, hv_Mean10 = null;
            HTuple hv_Sorteda = null, hv_Ai = null, hv_Ax = null, hv_pixeldist = null;
            HTuple hv_L = null, hv_D = null, hv_Dmax = null, hv_Dmin = null;
            HTuple hv_d = null, hv_dmax = null, hv_dmin = null, hv_LS = null;
            HTuple hv_HomMat2DIdentity1 = null, hv_HomMat2DRotate1 = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_ImageRotate);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans);
            HOperatorSet.GenEmptyObj(out ho_Region2);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionu);
            HOperatorSet.GenEmptyObj(out ho_RegionUniond);
            HOperatorSet.GenEmptyObj(out ho_Contours);
            HOperatorSet.GenEmptyObj(out ho_SelectedContours);
            HOperatorSet.GenEmptyObj(out ho_SmoothedContours1);
            HOperatorSet.GenEmptyObj(out ho_ClippedContoursu);
            HOperatorSet.GenEmptyObj(out ho_ClippedContoursd);
            HOperatorSet.GenEmptyObj(out ho_Rectangle2);
            HOperatorSet.GenEmptyObj(out ho_ClippedContoursuObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_ClippedContoursdObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SortedRegionssdj);
            HOperatorSet.GenEmptyObj(out ho_Region3);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_SortedRegionsxdj);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans1);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            try
            {
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle, DRow1m, DCol1m, DRow2m, DCol2m);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced);

                //*
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 0, 128);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area",
                                            70);
                //hv_jd = 0;
                HOperatorSet.SmallestRectangle2(ho_SelectedRegions, out hv_Row, out hv_Column,
                                                out hv_Phi, out hv_Length1, out hv_Length2);
                ho_Rectangle1.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row, hv_Column, hv_Phi, hv_Length1,
                                           hv_Length2);
                ho_ImageRotate.Dispose();
                HOperatorSet.RotateImage(Image, out ho_ImageRotate, -(hv_Phi.TupleDeg()),
                                         "constant");
                //*
                ho_Region1.Dispose();
                HOperatorSet.Threshold(ho_ImageRotate, out ho_Region1, 0, 128);
                //*
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, -hv_Phi, hv_Height / 2, hv_Width / 2,
                                            out hv_HomMat2DRotate);
                ho_RegionAffineTrans.Dispose();
                HOperatorSet.AffineTransRegion(ho_Rectangle1, out ho_RegionAffineTrans, hv_HomMat2DRotate,
                                               "nearest_neighbor");



                HOperatorSet.SmallestRectangle1(ho_RegionAffineTrans, out hv_Row1, out hv_Column1,
                                                out hv_Row2, out hv_Column2);

                //***
                ho_Region2.Dispose();
                HOperatorSet.GenRegionPoints(out ho_Region2, hv_Row1, hv_Column1);
                ho_RegionUnionu.Dispose();
                HOperatorSet.Union1(ho_Region2, out ho_RegionUnionu);
                ho_RegionUniond.Dispose();
                HOperatorSet.Union1(ho_Region2, out ho_RegionUniond);



                ho_Contours.Dispose();
                HOperatorSet.GenContourRegionXld(ho_Region1, out ho_Contours, "border");
                ho_SelectedContours.Dispose();
                HOperatorSet.SelectContoursXld(ho_Contours, out ho_SelectedContours, "contour_length",
                                               20, 2000000, -1, 1);
                ho_SmoothedContours1.Dispose();
                HOperatorSet.SmoothContoursXld(ho_SelectedContours, out ho_SmoothedContours1,
                                               13);
                //*
                hv_Rown = (hv_Row1 + hv_Row2) / 2;
                ho_ClippedContoursu.Dispose();
                HOperatorSet.ClipContoursXld(ho_SmoothedContours1, out ho_ClippedContoursu, hv_Row1,
                                             hv_Column1, hv_Rown, hv_Column2);
                ho_ClippedContoursd.Dispose();
                HOperatorSet.ClipContoursXld(ho_SmoothedContours1, out ho_ClippedContoursd, hv_Rown,
                                             hv_Column1, hv_Row2, hv_Column2);
                hv_Rowu     = (hv_Row1 + hv_Rown) / 2;
                hv_Columnu  = (hv_Column1 + hv_Column2) / 2;
                hv_Phiu     = 0;
                hv_Length1u = (hv_Column2 - hv_Column1) / 2;
                hv_Length2u = (hv_Rown - hv_Row1) / 2;
                //dev_set_draw ('margin')
                HOperatorSet.GenMeasureRectangle2(hv_Rowu, hv_Columnu, hv_Phiu, hv_Length1u,
                                                  hv_Length2u, hv_Width, hv_Height, "bilinear", out hv_MeasureHandleu);
                ho_Rectangle2.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle2, hv_Rowu, hv_Columnu, hv_Phiu, hv_Length1u,
                                           hv_Length2u);

                HOperatorSet.MeasureProjection(ho_ImageRotate, hv_MeasureHandleu, out hv_GrayValuesu);
                hv_Sigmau = 5;
                HOperatorSet.CreateFunct1dArray(hv_GrayValuesu, out hv_Functionu);
                HOperatorSet.SmoothFunct1dGauss(hv_Functionu, hv_Sigmau, out hv_SmoothedFunctionu);
                HOperatorSet.DerivateFunct1d(hv_SmoothedFunctionu, "first", out hv_FirstDerivativeu);
                HOperatorSet.ZeroCrossingsFunct1d(hv_FirstDerivativeu, out hv_ZeroCrossingsu);

                hv_RowStartu = hv_Rowu - (hv_Length2u / 2);
                hv_ColStartu = (hv_Columnu - hv_Length1u) + hv_ZeroCrossingsu;
                hv_RowEndu   = hv_Rowu + (hv_Length2u / 2);
                hv_ColEndu   = (hv_Columnu - hv_Length1u) + hv_ZeroCrossingsu;
                hv_iu        = new HTuple(hv_ZeroCrossingsu.TupleLength());


                hv_Rowd     = (hv_Rown + hv_Row2) / 2;
                hv_Columnd  = (hv_Column1 + hv_Column2) / 2;
                hv_Phid     = 0;
                hv_Length1d = (hv_Column2 - hv_Column1) / 2;
                hv_Length2d = (hv_Row2 - hv_Rown) / 2;
                HOperatorSet.GenMeasureRectangle2(hv_Rowd, hv_Columnd, hv_Phid, hv_Length1d,
                                                  hv_Length2d, hv_Width, hv_Height, "bilinear", out hv_MeasureHandled);
                HOperatorSet.MeasureProjection(ho_ImageRotate, hv_MeasureHandled, out hv_GrayValuesd);
                //
                hv_Sigmad = 5;
                HOperatorSet.CreateFunct1dArray(hv_GrayValuesd, out hv_Functiond);
                HOperatorSet.SmoothFunct1dGauss(hv_Functiond, hv_Sigmad, out hv_SmoothedFunctiond);
                HOperatorSet.DerivateFunct1d(hv_SmoothedFunctiond, "first", out hv_FirstDerivatived);
                HOperatorSet.ZeroCrossingsFunct1d(hv_FirstDerivatived, out hv_ZeroCrossingsd);

                HOperatorSet.CloseMeasure(hv_MeasureHandleu);
                HOperatorSet.CloseMeasure(hv_MeasureHandled);

                hv_RowStartd = hv_Rowd - (hv_Length2d / 2);
                hv_ColStartd = (hv_Columnd - hv_Length1d) + hv_ZeroCrossingsd;
                hv_RowEndd   = hv_Rowd + (hv_Length2d / 2);
                hv_ColEndd   = (hv_Columnd - hv_Length1d) + hv_ZeroCrossingsd;

                hv_id = new HTuple(hv_ZeroCrossingsd.TupleLength());


                hv_RowIntuAll    = new HTuple();
                hv_ColumnIntuAll = new HTuple();
                hv_u1            = 0;
                HTuple end_val96  = hv_iu - 1;
                HTuple step_val96 = 1;
                for (hv_i = 0; hv_i.Continue(end_val96, step_val96); hv_i = hv_i.TupleAdd(step_val96))
                {
                    HOperatorSet.LengthXld(ho_ClippedContoursu, out hv_Length);
                    HOperatorSet.TupleFind(hv_Length, hv_Length.TupleMax(), out hv_Indices1);
                    ho_ClippedContoursuObjectSelected.Dispose();
                    HOperatorSet.SelectObj(ho_ClippedContoursu, out ho_ClippedContoursuObjectSelected,
                                           hv_Indices1 + 1);
                    HOperatorSet.IntersectionLineContourXld(ho_ClippedContoursuObjectSelected,
                                                            hv_RowStartu, hv_ColStartu.TupleSelect(hv_i), hv_RowEndu, hv_ColEndu.TupleSelect(
                                                                hv_i), out hv_RowIntu, out hv_ColumnIntu, out hv_IsOverlappingu);
                    //*gen_cross_contour_xld (Cross1u, RowIntu, ColumnIntu, 12, 0)//根据每个输入点交叉的形状创键一个XLD轮廓(contour)
                    hv_RowIntuAll    = hv_RowIntuAll.TupleConcat(hv_RowIntu);
                    hv_ColumnIntuAll = hv_ColumnIntuAll.TupleConcat(hv_ColumnIntu);
                    hv_u1            = hv_u1 + 1;
                }

                hv_RowIntdAll    = new HTuple();
                hv_ColumnIntdAll = new HTuple();
                HTuple end_val109  = hv_id - 1;
                HTuple step_val109 = 1;
                for (hv_j = 0; hv_j.Continue(end_val109, step_val109); hv_j = hv_j.TupleAdd(step_val109))
                {
                    HOperatorSet.LengthXld(ho_ClippedContoursd, out hv_Length);
                    HOperatorSet.TupleFind(hv_Length, hv_Length.TupleMax(), out hv_Indices1);
                    ho_ClippedContoursdObjectSelected.Dispose();
                    HOperatorSet.SelectObj(ho_ClippedContoursd, out ho_ClippedContoursdObjectSelected,
                                           hv_Indices1 + 1);
                    HOperatorSet.IntersectionLineContourXld(ho_ClippedContoursdObjectSelected,
                                                            hv_RowStartd, hv_ColStartd.TupleSelect(hv_j), hv_RowEndd, hv_ColEndd.TupleSelect(
                                                                hv_j), out hv_RowIntd, out hv_ColumnIntd, out hv_IsOverlappingd);
                    //*gen_cross_contour_xld (Cross1d, RowIntd, ColumnIntd, 12, 0)//根据每个输入点交叉的形状创键一个XLD轮廓(contour)
                    hv_RowIntdAll    = hv_RowIntdAll.TupleConcat(hv_RowIntd);
                    hv_ColumnIntdAll = hv_ColumnIntdAll.TupleConcat(hv_ColumnIntd);
                }

                //stop ()

                HOperatorSet.TupleSort(hv_RowIntuAll, out hv_Sortedu);
                HOperatorSet.TupleSort(hv_RowIntdAll, out hv_Sortedd);
                hv_RowIntuu    = new HTuple();
                hv_ColumnIntuu = new HTuple();
                hv_RowIntud    = new HTuple();
                hv_ColumnIntud = new HTuple();
                hv_uu          = 0;
                hv_ud          = 0;
                for (hv_ub1 = 0; (int)hv_ub1 <= (int)((new HTuple(hv_Sortedu.TupleLength())) - 1); hv_ub1 = (int)hv_ub1 + 1)
                {
                    if ((int)(new HTuple(((hv_RowIntuAll.TupleSelect(hv_ub1))).TupleLess(((((hv_Sortedu.TupleSelect(
                                                                                                 0)) * 2) + (hv_Sortedu.TupleSelect((new HTuple(hv_Sortedu.TupleLength())) - 1))) / 3) + 3))) != 0)
                    {
                        hv_RowIntuu    = hv_RowIntuu.TupleConcat(hv_RowIntuAll.TupleSelect(hv_ub1));
                        hv_ColumnIntuu = hv_ColumnIntuu.TupleConcat(hv_ColumnIntuAll.TupleSelect(
                                                                        hv_ub1));

                        ho_Circle.Dispose();
                        HOperatorSet.GenCircle(out ho_Circle, hv_RowIntuAll.TupleSelect(hv_ub1),
                                               hv_ColumnIntuAll.TupleSelect(hv_ub1), 1);
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_RegionUnionu, ho_Circle, out ExpTmpOutVar_0);
                            ho_RegionUnionu.Dispose();
                            ho_RegionUnionu = ExpTmpOutVar_0;
                        }
                        hv_uu = hv_uu + 1;
                    }

                    if ((int)(new HTuple(((hv_RowIntuAll.TupleSelect(hv_ub1))).TupleGreater((((hv_Sortedu.TupleSelect(
                                                                                                   0)) + ((hv_Sortedu.TupleSelect((new HTuple(hv_Sortedu.TupleLength())) - 1)) * 2)) / 3) - 3))) != 0)
                    {
                        hv_RowIntud    = hv_RowIntud.TupleConcat(hv_RowIntuAll.TupleSelect(hv_ub1));
                        hv_ColumnIntud = hv_ColumnIntud.TupleConcat(hv_ColumnIntuAll.TupleSelect(
                                                                        hv_ub1));
                        hv_ud = hv_ud + 1;
                    }
                }

                //stop ()

                hv_RowIntdu    = new HTuple();
                hv_ColumnIntdu = new HTuple();
                hv_RowIntdd    = new HTuple();
                hv_ColumnIntdd = new HTuple();
                hv_du          = 0;
                hv_dd          = 0;
                for (hv_db1 = 0; (int)hv_db1 <= (int)((new HTuple(hv_Sortedd.TupleLength())) - 1); hv_db1 = (int)hv_db1 + 1)
                {
                    if ((int)(new HTuple(((hv_RowIntdAll.TupleSelect(hv_db1))).TupleLess(((((hv_Sortedd.TupleSelect(
                                                                                                 0)) * 2) + (hv_Sortedd.TupleSelect((new HTuple(hv_Sortedd.TupleLength())) - 1))) / 3) + 3))) != 0)
                    {
                        hv_RowIntdu    = hv_RowIntdu.TupleConcat(hv_RowIntdAll.TupleSelect(hv_db1));
                        hv_ColumnIntdu = hv_ColumnIntdu.TupleConcat(hv_ColumnIntdAll.TupleSelect(
                                                                        hv_db1));
                        hv_du = hv_du + 1;
                    }
                    if ((int)(new HTuple(((hv_RowIntdAll.TupleSelect(hv_db1))).TupleGreater((((hv_Sortedd.TupleSelect(
                                                                                                   0)) + ((hv_Sortedd.TupleSelect((new HTuple(hv_Sortedd.TupleLength())) - 1)) * 2)) / 3) - 3))) != 0)
                    {
                        hv_RowIntdd    = hv_RowIntdd.TupleConcat(hv_RowIntdAll.TupleSelect(hv_db1));
                        hv_ColumnIntdd = hv_ColumnIntdd.TupleConcat(hv_ColumnIntdAll.TupleSelect(
                                                                        hv_db1));
                        ho_Circle1.Dispose();
                        HOperatorSet.GenCircle(out ho_Circle1, hv_RowIntdAll.TupleSelect(hv_db1),
                                               hv_ColumnIntdAll.TupleSelect(hv_db1), 1);
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_RegionUniond, ho_Circle1, out ExpTmpOutVar_0);
                            ho_RegionUniond.Dispose();
                            ho_RegionUniond = ExpTmpOutVar_0;
                        }
                        hv_dd = hv_dd + 1;
                    }
                }

                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionUnionu, out ho_ConnectedRegions1);
                ho_SelectedRegions1.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions1, "area",
                                         "and", 3, 99999);
                ho_SortedRegionssdj.Dispose();
                HOperatorSet.SortRegion(ho_SelectedRegions1, out ho_SortedRegionssdj, "upper_left",
                                        "true", "column");
                HOperatorSet.AreaCenter(ho_SortedRegionssdj, out hv_Areasdj, out hv_Rowsdj, out hv_Columnsdj);
                ho_Region3.Dispose();
                HOperatorSet.GenRegionPoints(out ho_Region3, hv_Rowsdj.TupleSelect(0), hv_Columnsdj.TupleSelect(
                                                 0));
                hv_sdj = new HTuple(hv_Columnsdj.TupleLength());

                ho_ConnectedRegions2.Dispose();
                HOperatorSet.Connection(ho_RegionUniond, out ho_ConnectedRegions2);
                ho_SelectedRegions2.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions2, out ho_SelectedRegions2, "area",
                                         "and", 3, 99999);
                ho_SortedRegionsxdj.Dispose();
                HOperatorSet.SortRegion(ho_SelectedRegions2, out ho_SortedRegionsxdj, "upper_left",
                                        "true", "column");
                HOperatorSet.AreaCenter(ho_SortedRegionsxdj, out hv_Areaxdj, out hv_Rowxdj, out hv_Columnxdj);
                hv_xdj   = new HTuple(hv_Columnxdj.TupleLength());
                hv_djsl1 = new HTuple();
                hv_djsl1 = hv_djsl1.TupleConcat(hv_sdj);
                hv_djsl1 = hv_djsl1.TupleConcat(hv_xdj);
                HOperatorSet.TupleMin(hv_djsl1, out hv_djsl);
                HOperatorSet.TupleMean(hv_RowIntuu, out hv_Mean1);
                HOperatorSet.TupleMean(hv_RowIntud, out hv_Mean2);
                HOperatorSet.TupleMean(hv_RowIntdu, out hv_Mean3);
                HOperatorSet.TupleMean(hv_RowIntdd, out hv_Mean4);

                hv_Columnuu = new HTuple();
                hv_Columndd = new HTuple();
                hv_AngleAll = new HTuple();
                HTuple end_val191  = hv_djsl - 1;
                HTuple step_val191 = 1;
                for (hv_l = 0; hv_l.Continue(end_val191, step_val191); hv_l = hv_l.TupleAdd(step_val191))
                {
                    ho_RegionLines.Dispose();
                    HOperatorSet.GenRegionLine(out ho_RegionLines, hv_Rowsdj.TupleSelect(hv_l),
                                               hv_Columnsdj.TupleSelect(hv_l), hv_Rowxdj.TupleSelect(hv_l), hv_Columnxdj.TupleSelect(
                                                   hv_l));
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.Union2(ho_Region3, ho_RegionLines, out ExpTmpOutVar_0);
                        ho_Region3.Dispose();
                        ho_Region3 = ExpTmpOutVar_0;
                    }
                    HOperatorSet.AngleLx(hv_Rowsdj.TupleSelect(hv_l), hv_Columnsdj.TupleSelect(
                                             hv_l), hv_Rowxdj.TupleSelect(hv_l), hv_Columnxdj.TupleSelect(hv_l), out hv_Angle);
                    hv_AngleAll = hv_AngleAll.TupleConcat(0 - hv_Angle);
                    if ((int)(new HTuple(hv_l.TupleGreater(0))) != 0)
                    {
                        hv_Columnuu = hv_Columnuu.TupleConcat((hv_ColumnIntuu.TupleSelect(hv_l)) - (hv_ColumnIntuu.TupleSelect(
                                                                                                        hv_l - 1)));
                        hv_Columndd = hv_Columndd.TupleConcat((hv_ColumnIntdd.TupleSelect(hv_l)) - (hv_ColumnIntdd.TupleSelect(
                                                                                                        hv_l - 1)));
                    }

                    //stop ()
                }


                HOperatorSet.TupleDeg(hv_AngleAll, out hv_DegAll);
                HOperatorSet.TupleMean(hv_Columnuu, out hv_Mean5);
                HOperatorSet.TupleMean(hv_Columndd, out hv_Mean8);
                //**过滤
                hv_Columnuum = new HTuple();
                hv_uum1      = 0;
                for (hv_uus = 0; (int)hv_uus <= (int)((new HTuple(hv_Columnuu.TupleLength())) - 1); hv_uus = (int)hv_uus + 1)
                {
                    if ((int)((new HTuple(hv_Columnuu.TupleLessEqual(hv_Mean5 * 1.1))).TupleAnd(new HTuple(hv_Columnuu.TupleGreaterEqual(
                                                                                                               hv_Mean5 * 0.9)))) != 0)
                    {
                        hv_Columnuum = hv_Columnuum.TupleConcat(hv_Columnuu.TupleSelect(hv_uus));
                        hv_uum1      = hv_uum1 + 1;
                    }
                }
                hv_Columnddm = new HTuple();
                hv_ddm       = 0;
                for (hv_dds = 0; (int)hv_dds <= (int)((new HTuple(hv_Columndd.TupleLength())) - 1); hv_dds = (int)hv_dds + 1)
                {
                    if ((int)((new HTuple(hv_Columndd.TupleLessEqual(hv_Mean8 * 1.1))).TupleAnd(new HTuple(hv_Columndd.TupleGreaterEqual(
                                                                                                               hv_Mean8 * 0.9)))) != 0)
                    {
                        hv_Columnddm = hv_Columnddm.TupleConcat(hv_Columndd.TupleSelect(hv_dds));
                        hv_ddm       = hv_ddm + 1;
                    }
                }
                hv_ColumnmArr = new HTuple();
                hv_ColumnmArr = hv_ColumnmArr.TupleConcat(hv_Columnuum);
                hv_ColumnmArr = hv_ColumnmArr.TupleConcat(hv_Columnddm);
                HOperatorSet.TupleMean(hv_ColumnmArr, out hv_Mean9);
                HOperatorSet.TupleMean(hv_DegAll, out hv_Mean10);
                HOperatorSet.TupleSort(hv_DegAll, out hv_Sorteda);
                hv_Ai        = hv_Sorteda[0];
                hv_Ax        = hv_Sorteda[(new HTuple(hv_Sorteda.TupleLength())) - 1];
                hv_pixeldist = 1;
                hv_L         = ((hv_Columnsdj.TupleSelect(hv_djsl - 1)) - (hv_Columnsdj.TupleSelect(0))) * hv_pixeldist;
                hv_D         = (hv_Mean4 - hv_Mean1) * hv_pixeldist;
                hv_Dmax      = ((hv_RowIntdd.TupleMax()) - (hv_RowIntuu.TupleMin())) * hv_pixeldist;
                hv_Dmin      = ((hv_RowIntdd.TupleMin()) - (hv_RowIntuu.TupleMax())) * hv_pixeldist;
                hv_d         = (hv_Mean3 - hv_Mean2) * hv_pixeldist;
                hv_dmax      = ((hv_RowIntdu.TupleMax()) - (hv_RowIntud.TupleMin())) * hv_pixeldist;
                hv_dmin      = ((hv_RowIntdu.TupleMin()) - (hv_RowIntud.TupleMax())) * hv_pixeldist;
                hv_LS        = hv_Mean9 * hv_pixeldist;
                //*区域旋转
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity1);
                HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity1, hv_Phi, hv_Height / 2, hv_Width / 2,
                                            out hv_HomMat2DRotate1);
                ho_RegionAffineTrans1.Dispose();
                HOperatorSet.AffineTransRegion(ho_Region3, out ho_RegionAffineTrans1, hv_HomMat2DRotate1,
                                               "nearest_neighbor");
                HOperatorSet.Union1(ho_RegionAffineTrans1, out RegionToDisp);

                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("齿数");
                hv_result = hv_result.TupleConcat(hv_djsl.D);
                hv_result = hv_result.TupleConcat("螺纹长度");
                hv_result = hv_result.TupleConcat(hv_L.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺纹大径平均值");
                hv_result = hv_result.TupleConcat(hv_D.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺纹大径最小值");
                hv_result = hv_result.TupleConcat(hv_Dmin.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺纹大径最大值");
                hv_result = hv_result.TupleConcat(hv_Dmax.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺纹小径平均值");
                hv_result = hv_result.TupleConcat(hv_d.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺纹小径最小值");
                hv_result = hv_result.TupleConcat(hv_dmin.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺纹小径最大值");
                hv_result = hv_result.TupleConcat(hv_dmax.D * pixeldist);
                hv_result = hv_result.TupleConcat("牙距");
                hv_result = hv_result.TupleConcat(hv_LS.D * pixeldist);
                hv_result = hv_result.TupleConcat("平均牙倾角");
                hv_result = hv_result.TupleConcat(hv_Mean10.D);
                hv_result = hv_result.TupleConcat("最小牙倾角");
                hv_result = hv_result.TupleConcat(hv_Ai.D);
                hv_result = hv_result.TupleConcat("最大牙倾角");
                hv_result = hv_result.TupleConcat(hv_Ax.D);
                result    = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageRotate.Dispose();
                ho_Region1.Dispose();
                ho_RegionAffineTrans.Dispose();
                ho_Region2.Dispose();
                ho_RegionUnionu.Dispose();
                ho_RegionUniond.Dispose();
                ho_Contours.Dispose();
                ho_SelectedContours.Dispose();
                ho_SmoothedContours1.Dispose();
                ho_ClippedContoursu.Dispose();
                ho_ClippedContoursd.Dispose();
                ho_Rectangle2.Dispose();
                ho_ClippedContoursuObjectSelected.Dispose();
                ho_ClippedContoursdObjectSelected.Dispose();
                ho_Circle.Dispose();
                ho_Circle1.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_SortedRegionssdj.Dispose();
                ho_Region3.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_SortedRegionsxdj.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionAffineTrans1.Dispose();
                algorithm.Region.Dispose();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("齿数");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹长度");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹大径平均值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹大径最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹大径最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹小径平均值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹小径最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹小径最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("牙距");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("平均牙倾角");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("最小牙倾角");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("最大牙倾角");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageRotate.Dispose();
                ho_Region1.Dispose();
                ho_RegionAffineTrans.Dispose();
                ho_Region2.Dispose();
                ho_RegionUnionu.Dispose();
                ho_RegionUniond.Dispose();
                ho_Contours.Dispose();
                ho_SelectedContours.Dispose();
                ho_SmoothedContours1.Dispose();
                ho_ClippedContoursu.Dispose();
                ho_ClippedContoursd.Dispose();
                ho_Rectangle2.Dispose();
                ho_ClippedContoursuObjectSelected.Dispose();
                ho_ClippedContoursdObjectSelected.Dispose();
                ho_Circle.Dispose();
                ho_Circle1.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_SortedRegionssdj.Dispose();
                ho_Region3.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_SortedRegionsxdj.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionAffineTrans1.Dispose();
                algorithm.Region.Dispose();
            }
            finally
            {
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageRotate.Dispose();
                ho_Region1.Dispose();
                ho_RegionAffineTrans.Dispose();
                ho_Region2.Dispose();
                ho_RegionUnionu.Dispose();
                ho_RegionUniond.Dispose();
                ho_Contours.Dispose();
                ho_SelectedContours.Dispose();
                ho_SmoothedContours1.Dispose();
                ho_ClippedContoursu.Dispose();
                ho_ClippedContoursd.Dispose();
                ho_Rectangle2.Dispose();
                ho_ClippedContoursuObjectSelected.Dispose();
                ho_ClippedContoursdObjectSelected.Dispose();
                ho_Circle.Dispose();
                ho_Circle1.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_SortedRegionssdj.Dispose();
                ho_Region3.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_SortedRegionsxdj.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionAffineTrans1.Dispose();
                algorithm.Region.Dispose();
            }
        }
Exemplo n.º 17
0
        private void action()
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_Rectangle, ho_ImageReduced;
            HObject ho_Region, ho_RegionLines, ho_RegionUnion1, ho_RegionUnion2;
            HObject ho_RegionUnion3, ho_Region1, ho_ConnectedRegions;
            HObject ho_RegionFillUp, ho_SelectedRegions, ho_SortedRegions;
            HObject ho_ObjectSelected = null, ho_Rectangle1 = null, ho_ImageReduced1 = null;
            HObject ho_Circle1 = null, ho_Circle2 = null, ho_Circle3 = null;
            HObject ho_Circle4 = null, ho_RegionUnionc1 = null, ho_RegionUnionc2 = null;
            HObject ho_RegionUnionc3 = null, ho_ConnectedRegionsc = null;
            HObject ho_SortedRegions1 = null, ho_ContoursL = null, ho_RegionLinesL = null;
            HObject ho_RegionLinesL1 = null, ho_RegionUnionL = null, ho_ConnectedRegionsL = null;
            HObject ho_SelectedRegionsL = null, ho_RegionFillUpL = null;
            HObject ho_ContoursSplitL = null, ho_SelectedContoursL = null;
            HObject ho_UnionContoursL = null, ho_RegionL = null, ho_RegionDifferenceL = null;
            HObject ho_ContoursR = null, ho_RegionLinesR = null, ho_RegionLinesR1 = null;
            HObject ho_RegionUnionR = null, ho_ConnectedRegionsR = null;
            HObject ho_SelectedRegionsR = null, ho_RegionFillUpR = null;
            HObject ho_ContoursSplitR = null, ho_SelectedContoursR = null;
            HObject ho_UnionContoursR = null, ho_RegionR = null, ho_RegionDifferenceR = null;
            HObject ho_ConnectedRegions1;

            // Local control variables

            HTuple hv_Width = null, hv_Height = null, hv_Row11 = null;
            HTuple hv_Column11 = null, hv_Row21 = null, hv_Column21 = null;
            HTuple hv_Row12 = null, hv_Column12 = null, hv_Row22 = null;
            HTuple hv_Column22 = null, hv_Number = null, hv_h = null;
            HTuple hv_w = null, hv_i = null, hv_Row = new HTuple();
            HTuple hv_Column = new HTuple(), hv_Phi = new HTuple();
            HTuple hv_Length1 = new HTuple(), hv_Length2 = new HTuple();
            HTuple hv_Cos = new HTuple(), hv_Sin = new HTuple(), hv_RT_X = new HTuple();
            HTuple hv_RT_Y = new HTuple(), hv_AX = new HTuple(), hv_AY = new HTuple();
            HTuple hv_RB_X = new HTuple(), hv_RB_Y = new HTuple();
            HTuple hv_BX = new HTuple(), hv_BY = new HTuple(), hv_LB_X = new HTuple();
            HTuple hv_LB_Y = new HTuple(), hv_CX = new HTuple(), hv_CY = new HTuple();
            HTuple hv_LT_X = new HTuple(), hv_LT_Y = new HTuple();
            HTuple hv_DX = new HTuple(), hv_DY = new HTuple(), hv_Number1 = new HTuple();
            HTuple hv_Area = new HTuple(), hv_Row1 = new HTuple();
            HTuple hv_Column1 = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_Distance1 = new HTuple(), hv_shapeParam = new HTuple();
            HTuple hv_MetrologyHandleL = new HTuple(), hv_Index = new HTuple();
            HTuple hv_RowL = new HTuple(), hv_ColumnL = new HTuple();
            HTuple hv_l = new HTuple(), hv_NumberL = new HTuple();
            HTuple hv_MetrologyHandleR = new HTuple(), hv_RowR = new HTuple();
            HTuple hv_ColumnR = new HTuple(), hv_r = new HTuple();
            HTuple hv_NumberR = new HTuple(), hv_AreaL = new HTuple();
            HTuple hv_AreaR = new HTuple(), hv_Area1 = null, hv_Row2 = null;
            HTuple hv_Column2 = null, hv_Area2 = null, hv_Row3 = null;
            HTuple hv_Column3 = null, hv_hm = null, hv_wm = null, hv_Row13 = null;
            HTuple hv_Column13 = null, hv_Row23 = null, hv_Column23 = null;
            HTuple hv_zh = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion1);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion2);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion3);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            HOperatorSet.GenEmptyObj(out ho_Circle2);
            HOperatorSet.GenEmptyObj(out ho_Circle3);
            HOperatorSet.GenEmptyObj(out ho_Circle4);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionc1);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionc2);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionc3);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsc);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions1);
            HOperatorSet.GenEmptyObj(out ho_ContoursL);
            HOperatorSet.GenEmptyObj(out ho_RegionLinesL);
            HOperatorSet.GenEmptyObj(out ho_RegionLinesL1);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionL);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsL);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsL);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUpL);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplitL);
            HOperatorSet.GenEmptyObj(out ho_SelectedContoursL);
            HOperatorSet.GenEmptyObj(out ho_UnionContoursL);
            HOperatorSet.GenEmptyObj(out ho_RegionL);
            HOperatorSet.GenEmptyObj(out ho_RegionDifferenceL);
            HOperatorSet.GenEmptyObj(out ho_ContoursR);
            HOperatorSet.GenEmptyObj(out ho_RegionLinesR);
            HOperatorSet.GenEmptyObj(out ho_RegionLinesR1);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionR);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsR);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsR);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUpR);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplitR);
            HOperatorSet.GenEmptyObj(out ho_SelectedContoursR);
            HOperatorSet.GenEmptyObj(out ho_UnionContoursR);
            HOperatorSet.GenEmptyObj(out ho_RegionR);
            HOperatorSet.GenEmptyObj(out ho_RegionDifferenceR);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            try
            {
                HOperatorSet.GenRectangle1(out ho_Rectangle, DRow1m, DCol1m, DRow2m, DCol2m);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced);
                ho_Region.Dispose();
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255);
                HOperatorSet.SmallestRectangle1(ho_Region, out hv_Row12, out hv_Column12, out hv_Row22,
                                                out hv_Column22);

                ho_RegionLines.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines, (hv_Row12 + hv_Row22) / 2, hv_Column12,
                                           ((hv_Row12 + hv_Row22) / 2) + 1, hv_Column12);

                ho_RegionUnion1.Dispose();
                HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion1);

                ho_RegionUnion2.Dispose();
                HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion2);

                ho_RegionUnion3.Dispose();
                HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion3);


                ho_Region1.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 128, 255);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions);

                ho_RegionFillUp.Dispose();
                HOperatorSet.FillUp(ho_ConnectedRegions, out ho_RegionFillUp);

                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_RegionFillUp, out ho_SelectedRegions, "area", "and",
                                         500, 99999000);

                ho_SortedRegions.Dispose();
                HOperatorSet.SortRegion(ho_SelectedRegions, out ho_SortedRegions, "upper_left",
                                        "true", "column");

                HOperatorSet.CountObj(ho_SortedRegions, out hv_Number);

                // stop(); only in hdevelop
                hv_h = new HTuple();
                hv_w = new HTuple();
                HTuple end_val36  = hv_Number - 1;
                HTuple step_val36 = 1;
                for (hv_i = 2; hv_i.Continue(end_val36, step_val36); hv_i = hv_i.TupleAdd(step_val36))
                {
                    ho_ObjectSelected.Dispose();
                    HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_i);
                    HOperatorSet.SmallestRectangle2(ho_ObjectSelected, out hv_Row, out hv_Column,
                                                    out hv_Phi, out hv_Length1, out hv_Length2);
                    ho_Rectangle1.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row, hv_Column, hv_Phi,
                                               hv_Length1 * 0.85, hv_Length2);
                    ho_ImageReduced1.Dispose();
                    HOperatorSet.ReduceDomain(ho_ImageReduced, ho_Rectangle1, out ho_ImageReduced1
                                              );
                    //*矩形4个角点
                    HOperatorSet.TupleCos(hv_Phi, out hv_Cos);
                    HOperatorSet.TupleSin(hv_Phi, out hv_Sin);
                    //*dev_set_color('green')
                    hv_RT_X = (((-hv_Length1) * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin);
                    hv_RT_Y = (((-hv_Length1) * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos);
                    ho_Circle1.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle1, hv_Row - hv_RT_Y, hv_Column + hv_RT_X,
                                           0.5);
                    //*最小矩形的顶点A
                    hv_AX = (((-hv_Length1) * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin);
                    hv_AY = (((-hv_Length1) * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos);
                    //*dev_set_color('red')
                    hv_RB_X = ((hv_Length1 * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin);
                    hv_RB_Y = ((hv_Length1 * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos);
                    ho_Circle2.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle2, hv_Row - hv_RB_Y, hv_Column + hv_RB_X,
                                           0.5);
                    //*最小矩形的顶点B
                    hv_BX = ((hv_Length1 * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin);
                    hv_BY = ((hv_Length1 * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos);

                    //*dev_set_color('yellow')
                    hv_LB_X = ((hv_Length1 * 0.9) * hv_Cos) + (hv_Length2 * hv_Sin);
                    hv_LB_Y = ((hv_Length1 * 0.9) * hv_Sin) - (hv_Length2 * hv_Cos);
                    ho_Circle3.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle3, hv_Row - hv_LB_Y, hv_Column + hv_LB_X,
                                           0.5);
                    //*最小矩形的顶点C
                    hv_CX = ((hv_Length1 * 0.85) * hv_Cos) + (hv_Length2 * hv_Sin);
                    hv_CY = ((hv_Length1 * 0.85) * hv_Sin) - (hv_Length2 * hv_Cos);

                    //*dev_set_color('pink')
                    hv_LT_X = (((-hv_Length1) * 0.85) * hv_Cos) + (hv_Length2 * hv_Sin);
                    hv_LT_Y = (((-hv_Length1) * 0.85) * hv_Sin) - (hv_Length2 * hv_Cos);
                    ho_Circle4.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle4, hv_Row - hv_LT_Y, hv_Column + hv_LT_X,
                                           0.5);
                    //*最小矩形的顶点D
                    hv_DX = (((-hv_Length1) * 0.85) * hv_Cos) + (hv_Length2 * hv_Sin);
                    hv_DY = (((-hv_Length1) * 0.85) * hv_Sin) - (hv_Length2 * hv_Cos);

                    //stop ()

                    ho_RegionUnionc1.Dispose();
                    HOperatorSet.Union2(ho_Circle1, ho_Circle2, out ho_RegionUnionc1);
                    ho_RegionUnionc2.Dispose();
                    HOperatorSet.Union2(ho_RegionUnionc1, ho_Circle3, out ho_RegionUnionc2);
                    ho_RegionUnionc3.Dispose();
                    HOperatorSet.Union2(ho_RegionUnionc2, ho_Circle4, out ho_RegionUnionc3);
                    ho_ConnectedRegionsc.Dispose();
                    HOperatorSet.Connection(ho_RegionUnionc3, out ho_ConnectedRegionsc);
                    HOperatorSet.CountObj(ho_RegionUnionc3, out hv_Number1);
                    ho_SortedRegions1.Dispose();
                    HOperatorSet.SortRegion(ho_ConnectedRegionsc, out ho_SortedRegions1, "upper_left",
                                            "true", "row");

                    HOperatorSet.AreaCenter(ho_SortedRegions1, out hv_Area, out hv_Row1, out hv_Column1);
                    HOperatorSet.DistancePp(hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(0),
                                            hv_Row1.TupleSelect(1), hv_Column1.TupleSelect(1), out hv_Distance);
                    HOperatorSet.DistancePl(hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(0),
                                            hv_Row1.TupleSelect(2), hv_Column1.TupleSelect(2), hv_Row1.TupleSelect(
                                                3), hv_Column1.TupleSelect(3), out hv_Distance1);
                    hv_h = hv_h.TupleConcat((hv_Distance1 * 1) / 0.9);
                    hv_w = hv_w.TupleConcat(hv_Distance);
                    //gen_region_line (RegionLines2, (Row1[0]+Row1[1])/2, (Column1[0]+Column1[1])/2, (Row1[2]+Row1[3])/2, (Column1[2]+Column1[3])/2)
                    hv_shapeParam = new HTuple();
                    hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Row1.TupleSelect(
                                                                    0)) + (hv_Row1.TupleSelect(1))) / 2);
                    hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Column1.TupleSelect(
                                                                    0)) + (hv_Column1.TupleSelect(1))) / 2);
                    hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Row1.TupleSelect(
                                                                    2)) + (hv_Row1.TupleSelect(3))) / 2);
                    hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Column1.TupleSelect(
                                                                    2)) + (hv_Column1.TupleSelect(3))) / 2);

                    //shapeParam := [Row12,(Column22+Column12)/2,Row22,(Column22+Column12)/2]
                    //找左侧点
                    HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandleL);
                    HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandleL, hv_Width, hv_Height);
                    HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandleL, "line", hv_shapeParam,
                                                           hv_Distance / 2, 1, 1, 60, new HTuple(), new HTuple(), out hv_Index);
                    //设置参数
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_transition",
                                                         "negative");
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_measures",
                                                         100);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_instances",
                                                         1);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_select",
                                                         "last");

                    //开始找边缘
                    HOperatorSet.ApplyMetrologyModel(ho_ImageReduced1, hv_MetrologyHandleL);
                    ho_ContoursL.Dispose();
                    HOperatorSet.GetMetrologyObjectMeasures(out ho_ContoursL, hv_MetrologyHandleL,
                                                            "all", "all", out hv_RowL, out hv_ColumnL);

                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                    }
                    HOperatorSet.DispCross(3600, hv_RowL, hv_ColumnL, 6, hv_Phi);

                    //左侧点,用一Row,只取最左边点
                    //gen_cross_contour_xld (CrossL, RowL, ColumnL, 6, 0)
                    ///释放测量句柄
                    HOperatorSet.ClearMetrologyModel(hv_MetrologyHandleL);
                    //gen_region_line (RegionLines, Row11, Column11, Row11+1, Column11)
                    //union1 (RegionLines, RegionUnion1)

                    for (hv_l = 0; (int)hv_l <= (int)((new HTuple(hv_RowL.TupleLength())) - 2); hv_l = (int)hv_l + 1)
                    {
                        ho_RegionLinesL.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLinesL, hv_RowL.TupleSelect(hv_l),
                                                   hv_ColumnL.TupleSelect(hv_l), hv_RowL.TupleSelect(hv_l + 1), hv_ColumnL.TupleSelect(
                                                       hv_l + 1));
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_RegionUnion1, ho_RegionLinesL, out ExpTmpOutVar_0
                                                );
                            ho_RegionUnion1.Dispose();
                            ho_RegionUnion1 = ExpTmpOutVar_0;
                        }
                    }
                    if (HDevWindowStack.IsOpen())
                    {
                        //dev_set_color ('blue')
                    }
                    //gen_contour_polygon_xld (Contour, RowL, ColumnL)
                    //stop ()
                    //disp_obj (Image, 3600)

                    ho_RegionLinesL1.Dispose();
                    HOperatorSet.GenRegionLine(out ho_RegionLinesL1, hv_RowL.TupleSelect(0),
                                               hv_ColumnL.TupleSelect(0), hv_RowL.TupleSelect((new HTuple(hv_RowL.TupleLength()
                                                                                                          )) - 1), hv_ColumnL.TupleSelect((new HTuple(hv_RowL.TupleLength())) - 1));
                    ho_RegionUnionL.Dispose();
                    HOperatorSet.Union2(ho_RegionUnion1, ho_RegionLinesL1, out ho_RegionUnionL
                                        );
                    ho_ConnectedRegionsL.Dispose();
                    HOperatorSet.Connection(ho_RegionUnionL, out ho_ConnectedRegionsL);
                    ho_SelectedRegionsL.Dispose();
                    HOperatorSet.SelectShapeStd(ho_ConnectedRegionsL, out ho_SelectedRegionsL,
                                                "max_area", 70);
                    ho_RegionFillUpL.Dispose();
                    HOperatorSet.FillUp(ho_SelectedRegionsL, out ho_RegionFillUpL);
                    ho_ContoursL.Dispose();
                    HOperatorSet.GenContourRegionXld(ho_RegionFillUpL, out ho_ContoursL, "border");
                    ho_ContoursSplitL.Dispose();
                    HOperatorSet.SegmentContoursXld(ho_ContoursL, out ho_ContoursSplitL, "lines_circles",
                                                    5, 8, 3.5);
                    ho_SelectedContoursL.Dispose();
                    HOperatorSet.SelectContoursXld(ho_ContoursSplitL, out ho_SelectedContoursL,
                                                   "contour_length", 70, 2000, -0.5, 0.5);
                    //***
                    ho_UnionContoursL.Dispose();
                    HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContoursL, out ho_UnionContoursL,
                                                          10, 1, "attr_keep");
                    ho_RegionL.Dispose();
                    HOperatorSet.GenRegionContourXld(ho_UnionContoursL, out ho_RegionL, "filled");

                    ho_RegionDifferenceL.Dispose();
                    HOperatorSet.Difference(ho_RegionFillUpL, ho_RegionL, out ho_RegionDifferenceL
                                            );
                    HOperatorSet.CountObj(ho_RegionDifferenceL, out hv_NumberL);

                    //*
                    //找右侧点
                    HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandleR);
                    HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandleR, hv_Width, hv_Height);
                    HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandleR, "line", hv_shapeParam,
                                                           hv_Distance / 2, 1, 1, 60, new HTuple(), new HTuple(), out hv_Index);
                    //设置参数
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "measure_transition",
                                                         "positive");
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "num_measures",
                                                         100);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "num_instances",
                                                         1);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "measure_select",
                                                         "first");

                    //开始找边缘
                    HOperatorSet.ApplyMetrologyModel(ho_ImageReduced1, hv_MetrologyHandleR);
                    ho_ContoursR.Dispose();
                    HOperatorSet.GetMetrologyObjectMeasures(out ho_ContoursR, hv_MetrologyHandleR,
                                                            "all", "all", out hv_RowR, out hv_ColumnR);

                    //disp_obj (Image, 3600)
                    if (HDevWindowStack.IsOpen())
                    {
                        //dev_set_color ('red')
                    }
                    //disp_cross (3600, RowR, ColumnR, 6, Phi)
                    //*右侧点,用一Row,只取最右边点
                    //gen_cross_contour_xld (CrossR, RowR, ColumnR, 6, 0)

                    ///释放测量句柄
                    HOperatorSet.ClearMetrologyModel(hv_MetrologyHandleR);
                    //close_measure (MeasureHandle1)
                    //stop ()

                    //gen_region_line (RegionLines, Row11, Column11, Row11+1, Column11)
                    //union1 (RegionLines, RegionUnion1)

                    for (hv_r = 0; (int)hv_r <= (int)((new HTuple(hv_RowR.TupleLength())) - 2); hv_r = (int)hv_r + 1)
                    {
                        ho_RegionLinesR.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLinesR, hv_RowR.TupleSelect(hv_r),
                                                   hv_ColumnR.TupleSelect(hv_r), hv_RowR.TupleSelect(hv_r + 1), hv_ColumnR.TupleSelect(
                                                       hv_r + 1));
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_RegionUnion2, ho_RegionLinesR, out ExpTmpOutVar_0
                                                );
                            ho_RegionUnion2.Dispose();
                            ho_RegionUnion2 = ExpTmpOutVar_0;
                        }
                    }

                    ho_RegionLinesR1.Dispose();
                    HOperatorSet.GenRegionLine(out ho_RegionLinesR1, hv_RowR.TupleSelect(0),
                                               hv_ColumnR.TupleSelect(0), hv_RowR.TupleSelect((new HTuple(hv_RowR.TupleLength()
                                                                                                          )) - 1), hv_ColumnR.TupleSelect((new HTuple(hv_RowR.TupleLength())) - 1));
                    ho_RegionUnionR.Dispose();
                    HOperatorSet.Union2(ho_RegionUnion2, ho_RegionLinesR1, out ho_RegionUnionR
                                        );
                    ho_ConnectedRegionsR.Dispose();
                    HOperatorSet.Connection(ho_RegionUnionR, out ho_ConnectedRegionsR);
                    ho_SelectedRegionsR.Dispose();
                    HOperatorSet.SelectShapeStd(ho_ConnectedRegionsR, out ho_SelectedRegionsR,
                                                "max_area", 70);
                    ho_RegionFillUpR.Dispose();
                    HOperatorSet.FillUp(ho_SelectedRegionsR, out ho_RegionFillUpR);
                    ho_ContoursR.Dispose();
                    HOperatorSet.GenContourRegionXld(ho_RegionFillUpR, out ho_ContoursR, "border");
                    ho_ContoursSplitR.Dispose();
                    HOperatorSet.SegmentContoursXld(ho_ContoursR, out ho_ContoursSplitR, "lines_circles",
                                                    5, 8, 3.5);
                    ho_SelectedContoursR.Dispose();
                    HOperatorSet.SelectContoursXld(ho_ContoursSplitR, out ho_SelectedContoursR,
                                                   "contour_length", 70, 2000, -0.5, 0.5);
                    ho_UnionContoursR.Dispose();
                    HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContoursR, out ho_UnionContoursR,
                                                          10, 1, "attr_keep");
                    ho_RegionR.Dispose();
                    HOperatorSet.GenRegionContourXld(ho_UnionContoursR, out ho_RegionR, "filled");

                    ho_RegionDifferenceR.Dispose();
                    HOperatorSet.Difference(ho_RegionFillUpR, ho_RegionR, out ho_RegionDifferenceR
                                            );
                    HOperatorSet.CountObj(ho_RegionDifferenceR, out hv_NumberR);

                    //stop ()

                    if ((int)(new HTuple(hv_NumberL.TupleGreater(0))) != 0)
                    {
                        HOperatorSet.AreaCenter(ho_RegionDifferenceL, out hv_AreaL, out hv_RowL,
                                                out hv_ColumnL);
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_RegionUnion3, ho_RegionDifferenceL, out ExpTmpOutVar_0
                                                );
                            ho_RegionUnion3.Dispose();
                            ho_RegionUnion3 = ExpTmpOutVar_0;
                        }
                    }
                    if ((int)(new HTuple(hv_NumberR.TupleGreater(0))) != 0)
                    {
                        HOperatorSet.AreaCenter(ho_RegionDifferenceR, out hv_AreaR, out hv_RowR,
                                                out hv_ColumnR);
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_RegionUnion3, ho_RegionDifferenceR, out ExpTmpOutVar_0
                                                );
                            ho_RegionUnion3.Dispose();
                            ho_RegionUnion3 = ExpTmpOutVar_0;
                        }
                    }
                    //union2 (RegionDifferenceL, RegionDifferenceR, RegionUnionLR)
                    //union2 (RegionUnion3, RegionUnionLR, RegionUnion3)
                }

                HOperatorSet.AreaCenter(ho_RegionUnion3, out hv_Area1, out hv_Row2, out hv_Column2);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionUnion3, out ho_ConnectedRegions1);
                HOperatorSet.AreaCenter(ho_ConnectedRegions1, out hv_Area2, out hv_Row3, out hv_Column3);

                hv_hm = (hv_h.TupleMin()) / (hv_h.TupleMean());
                hv_wm = (hv_w.TupleMax()) / (hv_w.TupleMean());
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_Number);
                HOperatorSet.SmallestRectangle1(ho_ObjectSelected, out hv_Row13, out hv_Column13,
                                                out hv_Row23, out hv_Column23);
                hv_zh = hv_Column23 - hv_Column13;
                HOperatorSet.Union1(ho_ConnectedRegions1, out RegionToDisp);

                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("最短比值");
                hv_result = hv_result.TupleConcat(hv_hm.D);
                hv_result = hv_result.TupleConcat("最宽比值");
                hv_result = hv_result.TupleConcat(hv_wm.D);
                hv_result = hv_result.TupleConcat("缺陷总面积");
                hv_result = hv_result.TupleConcat(hv_Area1.D);
                hv_result = hv_result.TupleConcat("最大缺陷面积");
                hv_result = hv_result.TupleConcat(hv_Area2.D);
                hv_result = hv_result.TupleConcat("宽度");
                hv_result = hv_result.TupleConcat(hv_zh.D);


                result = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionUnion1.Dispose();
                ho_RegionUnion2.Dispose();
                ho_RegionUnion3.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Circle1.Dispose();
                ho_Circle2.Dispose();
                ho_Circle3.Dispose();
                ho_Circle4.Dispose();
                ho_RegionUnionc1.Dispose();
                ho_RegionUnionc2.Dispose();
                ho_RegionUnionc3.Dispose();
                ho_ConnectedRegionsc.Dispose();
                ho_SortedRegions1.Dispose();
                ho_ContoursL.Dispose();
                ho_RegionLinesL.Dispose();
                ho_RegionLinesL1.Dispose();
                ho_RegionUnionL.Dispose();
                ho_ConnectedRegionsL.Dispose();
                ho_SelectedRegionsL.Dispose();
                ho_RegionFillUpL.Dispose();
                ho_ContoursSplitL.Dispose();
                ho_SelectedContoursL.Dispose();
                ho_UnionContoursL.Dispose();
                ho_RegionL.Dispose();
                ho_RegionDifferenceL.Dispose();
                ho_ContoursR.Dispose();
                ho_RegionLinesR.Dispose();
                ho_RegionLinesR1.Dispose();
                ho_RegionUnionR.Dispose();
                ho_ConnectedRegionsR.Dispose();
                ho_SelectedRegionsR.Dispose();
                ho_RegionFillUpR.Dispose();
                ho_ContoursSplitR.Dispose();
                ho_SelectedContoursR.Dispose();
                ho_UnionContoursR.Dispose();
                ho_RegionR.Dispose();
                ho_RegionDifferenceR.Dispose();
                ho_ConnectedRegions1.Dispose();
                algorithm.Region.Dispose();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("最短比值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("最宽比值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("缺陷总面积");
                hv_result = hv_result.TupleConcat(999999);
                hv_result = hv_result.TupleConcat("最大缺陷面积");
                hv_result = hv_result.TupleConcat(999999);
                hv_result = hv_result.TupleConcat("宽度");
                hv_result = hv_result.TupleConcat(999999);
                result    = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionUnion1.Dispose();
                ho_RegionUnion2.Dispose();
                ho_RegionUnion3.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Circle1.Dispose();
                ho_Circle2.Dispose();
                ho_Circle3.Dispose();
                ho_Circle4.Dispose();
                ho_RegionUnionc1.Dispose();
                ho_RegionUnionc2.Dispose();
                ho_RegionUnionc3.Dispose();
                ho_ConnectedRegionsc.Dispose();
                ho_SortedRegions1.Dispose();
                ho_ContoursL.Dispose();
                ho_RegionLinesL.Dispose();
                ho_RegionLinesL1.Dispose();
                ho_RegionUnionL.Dispose();
                ho_ConnectedRegionsL.Dispose();
                ho_SelectedRegionsL.Dispose();
                ho_RegionFillUpL.Dispose();
                ho_ContoursSplitL.Dispose();
                ho_SelectedContoursL.Dispose();
                ho_UnionContoursL.Dispose();
                ho_RegionL.Dispose();
                ho_RegionDifferenceL.Dispose();
                ho_ContoursR.Dispose();
                ho_RegionLinesR.Dispose();
                ho_RegionLinesR1.Dispose();
                ho_RegionUnionR.Dispose();
                ho_ConnectedRegionsR.Dispose();
                ho_SelectedRegionsR.Dispose();
                ho_RegionFillUpR.Dispose();
                ho_ContoursSplitR.Dispose();
                ho_SelectedContoursR.Dispose();
                ho_UnionContoursR.Dispose();
                ho_RegionR.Dispose();
                ho_RegionDifferenceR.Dispose();
                ho_ConnectedRegions1.Dispose();
                algorithm.Region.Dispose();
            }
            finally
            {
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionUnion1.Dispose();
                ho_RegionUnion2.Dispose();
                ho_RegionUnion3.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Circle1.Dispose();
                ho_Circle2.Dispose();
                ho_Circle3.Dispose();
                ho_Circle4.Dispose();
                ho_RegionUnionc1.Dispose();
                ho_RegionUnionc2.Dispose();
                ho_RegionUnionc3.Dispose();
                ho_ConnectedRegionsc.Dispose();
                ho_SortedRegions1.Dispose();
                ho_ContoursL.Dispose();
                ho_RegionLinesL.Dispose();
                ho_RegionLinesL1.Dispose();
                ho_RegionUnionL.Dispose();
                ho_ConnectedRegionsL.Dispose();
                ho_SelectedRegionsL.Dispose();
                ho_RegionFillUpL.Dispose();
                ho_ContoursSplitL.Dispose();
                ho_SelectedContoursL.Dispose();
                ho_UnionContoursL.Dispose();
                ho_RegionL.Dispose();
                ho_RegionDifferenceL.Dispose();
                ho_ContoursR.Dispose();
                ho_RegionLinesR.Dispose();
                ho_RegionLinesR1.Dispose();
                ho_RegionUnionR.Dispose();
                ho_ConnectedRegionsR.Dispose();
                ho_SelectedRegionsR.Dispose();
                ho_RegionFillUpR.Dispose();
                ho_ContoursSplitR.Dispose();
                ho_SelectedContoursR.Dispose();
                ho_UnionContoursR.Dispose();
                ho_RegionR.Dispose();
                ho_RegionDifferenceR.Dispose();
                ho_ConnectedRegions1.Dispose();
                algorithm.Region.Dispose();
            }
        }
Exemplo n.º 18
0
        public void TeethDist(HObject ho_Region, out HObject ho_Cross, HTuple hv_vOrh,
                              out HTuple hv_maxDist, out HTuple hv_minDist, out HTuple hv_meanDist)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];
            long      SP_O  = 0;

            // Local iconic variables

            HObject ho_RegionErosion, ho_ConnectedRegions;
            HObject ho_SortedRegions = null, ho_EmptyRegion = null, ho_ObjectSelected = null;
            HObject ho_RegionTrans = null, ho_SortedRegions1 = null, ho_SelectedRegions1 = null;
            HObject ho_ObjectSelected1 = null, ho_SelectedRegions2 = null;
            HObject ho_ObjectSelected2 = null;


            // Local control variables

            HTuple hv_Number = new HTuple(), hv_Index = new HTuple();
            HTuple hv_Area = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_Number1 = new HTuple(), hv_Number2 = new HTuple();
            HTuple hv_dist = new HTuple(), hv_Index1 = new HTuple(), hv_Area1 = new HTuple();
            HTuple hv_Row4 = new HTuple(), hv_Column3 = new HTuple(), hv_Area2 = new HTuple();
            HTuple hv_Row5 = new HTuple(), hv_Column4 = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HOperatorSet.GenEmptyObj(out ho_RegionErosion);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions);
            HOperatorSet.GenEmptyObj(out ho_EmptyRegion);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected2);

            try
            {
                ho_RegionErosion.Dispose();
                HOperatorSet.ErosionCircle(ho_Region, out ho_RegionErosion, 3.5);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_RegionErosion, out ho_ConnectedRegions);
                if ((int)(new HTuple(hv_vOrh.TupleEqual("h"))) != 0)
                {
                    ho_SortedRegions.Dispose();
                    HOperatorSet.SortRegion(ho_ConnectedRegions, out ho_SortedRegions, "character",
                                            "true", "column");
                    HOperatorSet.CountObj(ho_SortedRegions, out hv_Number);
                    ho_EmptyRegion.Dispose();
                    HOperatorSet.GenEmptyRegion(out ho_EmptyRegion);

                    for (hv_Index = 3; hv_Index.Continue(hv_Number - 2, 1); hv_Index = hv_Index.TupleAdd(1))
                    {
                        ho_ObjectSelected.Dispose();
                        HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_Index);
                        OTemp[SP_O] = ho_EmptyRegion.CopyObj(1, -1);
                        SP_O++;
                        ho_EmptyRegion.Dispose();
                        HOperatorSet.ConcatObj(ho_ObjectSelected, OTemp[SP_O - 1], out ho_EmptyRegion
                                               );
                        OTemp[SP_O - 1].Dispose();
                        SP_O = 0;
                    }
                    ho_RegionTrans.Dispose();
                    HOperatorSet.ShapeTrans(ho_EmptyRegion, out ho_RegionTrans, "outer_circle");
                    ho_SortedRegions1.Dispose();
                    HOperatorSet.SortRegion(ho_RegionTrans, out ho_SortedRegions1, "character",
                                            "true", "row");
                    HOperatorSet.AreaCenter(ho_SortedRegions1, out hv_Area, out hv_Row, out hv_Column);
                    ho_Cross.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row, hv_Column, 6, 0.785398);


                    HOperatorSet.CountObj(ho_SortedRegions1, out hv_Number1);
                    ho_SelectedRegions1.Dispose();
                    HOperatorSet.SelectShape(ho_SortedRegions1, out ho_SelectedRegions1, "row",
                                             "and", (hv_Row.TupleSelect(0)) - 10, (hv_Row.TupleSelect(0)) + 10);
                    HOperatorSet.CountObj(ho_SelectedRegions1, out hv_Number2);
                    hv_dist = new HTuple();
                    for (hv_Index1 = 1; hv_Index1.Continue(hv_Number2 - 1, 1); hv_Index1 = hv_Index1.TupleAdd(1))
                    {
                        ho_ObjectSelected1.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions1, out ho_ObjectSelected1, hv_Index1);
                        HOperatorSet.AreaCenter(ho_ObjectSelected1, out hv_Area1, out hv_Row4,
                                                out hv_Column3);

                        ho_ObjectSelected1.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions1, out ho_ObjectSelected1, hv_Index1 + 1);
                        HOperatorSet.AreaCenter(ho_ObjectSelected1, out hv_Area2, out hv_Row5,
                                                out hv_Column4);

                        hv_dist = hv_dist.TupleConcat(hv_Column4 - hv_Column3);
                    }


                    ho_SelectedRegions2.Dispose();
                    HOperatorSet.SelectShape(ho_SortedRegions1, out ho_SelectedRegions2, "row",
                                             "and", (hv_Row.TupleSelect(hv_Number1 - 1)) - 10, (hv_Row.TupleSelect(hv_Number1 - 1)) + 10);

                    HOperatorSet.CountObj(ho_SelectedRegions2, out hv_Number2);
                    for (hv_Index1 = 1; hv_Index1.Continue(hv_Number2 - 1, 1); hv_Index1 = hv_Index1.TupleAdd(1))
                    {
                        ho_ObjectSelected2.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions2, out ho_ObjectSelected2, hv_Index1);
                        HOperatorSet.AreaCenter(ho_ObjectSelected2, out hv_Area1, out hv_Row4,
                                                out hv_Column3);

                        ho_ObjectSelected2.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions2, out ho_ObjectSelected2, hv_Index1 + 1);
                        HOperatorSet.AreaCenter(ho_ObjectSelected2, out hv_Area2, out hv_Row5,
                                                out hv_Column4);

                        hv_dist = hv_dist.TupleConcat(hv_Column4 - hv_Column3);
                    }
                }
                else
                {
                    ho_SortedRegions.Dispose();
                    HOperatorSet.SortRegion(ho_ConnectedRegions, out ho_SortedRegions, "character",
                                            "true", "row");
                    HOperatorSet.CountObj(ho_SortedRegions, out hv_Number);
                    ho_EmptyRegion.Dispose();
                    HOperatorSet.GenEmptyRegion(out ho_EmptyRegion);

                    for (hv_Index = 3; hv_Index.Continue(hv_Number - 2, 1); hv_Index = hv_Index.TupleAdd(1))
                    {
                        ho_ObjectSelected.Dispose();
                        HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_Index);
                        OTemp[SP_O] = ho_EmptyRegion.CopyObj(1, -1);
                        SP_O++;
                        ho_EmptyRegion.Dispose();
                        HOperatorSet.ConcatObj(ho_ObjectSelected, OTemp[SP_O - 1], out ho_EmptyRegion
                                               );
                        OTemp[SP_O - 1].Dispose();
                        SP_O = 0;
                    }
                    ho_RegionTrans.Dispose();
                    HOperatorSet.ShapeTrans(ho_EmptyRegion, out ho_RegionTrans, "outer_circle");
                    ho_SortedRegions1.Dispose();
                    HOperatorSet.SortRegion(ho_RegionTrans, out ho_SortedRegions1, "character",
                                            "true", "column");
                    HOperatorSet.AreaCenter(ho_SortedRegions1, out hv_Area, out hv_Row, out hv_Column);
                    ho_Cross.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row, hv_Column, 6, 0.785398);


                    HOperatorSet.CountObj(ho_SortedRegions1, out hv_Number1);
                    ho_SelectedRegions1.Dispose();
                    HOperatorSet.SelectShape(ho_SortedRegions1, out ho_SelectedRegions1, "column",
                                             "and", (hv_Column.TupleSelect(0)) - 10, (hv_Column.TupleSelect(0)) + 10);
                    HOperatorSet.CountObj(ho_SelectedRegions1, out hv_Number2);
                    hv_dist = new HTuple();
                    for (hv_Index1 = 1; hv_Index1.Continue(hv_Number2 - 1, 1); hv_Index1 = hv_Index1.TupleAdd(1))
                    {
                        ho_ObjectSelected1.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions1, out ho_ObjectSelected1, hv_Index1);
                        HOperatorSet.AreaCenter(ho_ObjectSelected1, out hv_Area1, out hv_Row4,
                                                out hv_Column3);

                        ho_ObjectSelected1.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions1, out ho_ObjectSelected1, hv_Index1 + 1);
                        HOperatorSet.AreaCenter(ho_ObjectSelected1, out hv_Area2, out hv_Row5,
                                                out hv_Column4);

                        hv_dist = hv_dist.TupleConcat(hv_Row5 - hv_Row4);
                    }


                    ho_SelectedRegions2.Dispose();
                    HOperatorSet.SelectShape(ho_SortedRegions1, out ho_SelectedRegions2, "column",
                                             "and", (hv_Column.TupleSelect(hv_Number1 - 1)) - 10, (hv_Column.TupleSelect(
                                                                                                       hv_Number1 - 1)) + 10);

                    HOperatorSet.CountObj(ho_SelectedRegions2, out hv_Number2);
                    for (hv_Index1 = 1; hv_Index1.Continue(hv_Number2 - 1, 1); hv_Index1 = hv_Index1.TupleAdd(1))
                    {
                        ho_ObjectSelected2.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions2, out ho_ObjectSelected2, hv_Index1);
                        HOperatorSet.AreaCenter(ho_ObjectSelected2, out hv_Area1, out hv_Row4,
                                                out hv_Column3);

                        ho_ObjectSelected2.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions2, out ho_ObjectSelected2, hv_Index1 + 1);
                        HOperatorSet.AreaCenter(ho_ObjectSelected2, out hv_Area2, out hv_Row5,
                                                out hv_Column4);

                        hv_dist = hv_dist.TupleConcat(hv_Row5 - hv_Row4);
                    }
                }
                hv_maxDist  = hv_dist.TupleMax();
                hv_minDist  = hv_dist.TupleMin();
                hv_meanDist = hv_dist.TupleMean();
                ho_RegionErosion.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_EmptyRegion.Dispose();
                ho_ObjectSelected.Dispose();
                ho_RegionTrans.Dispose();
                ho_SortedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_ObjectSelected2.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_RegionErosion.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_EmptyRegion.Dispose();
                ho_ObjectSelected.Dispose();
                ho_RegionTrans.Dispose();
                ho_SortedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_ObjectSelected2.Dispose();

                throw HDevExpDefaultException;
            }
        }
Exemplo n.º 19
0
        // Main procedure
        private HTuple action()
        {
            // Local iconic variables
            HObject ho_Image, ho_ImageRotate, ho_GrayImage;
            HObject ho_ImageMedian, ho_Region, ho_ConnectedRegions;
            HObject ho_SelectedRegions, ho_SelectedRegions2, ho_SelectedRegions1;
            HObject ho_SelectedRegions3, ho_SortedRegions, ho_RegionUnion;
            HObject ho_Rectangle, ho_ImageReduced, ho_GrayImageA, ho_GragImageA;
            HObject ho_RegionsA_temp, ho_ConnectedRegionsA_temp, ho_SelectedRegionsA_temp;
            HObject ho_ImageAffineTrans, ho_RegionsA, ho_RegionDilationA;
            HObject ho_ConnectedRegionsA, ho_SelectedRegionsA, ho_RegionsA1;
            HObject ho_RegionOpeningA, ho_SelectedRegionsA1, ho_RegionUnionA;
            HObject ho_SelectedRegionsA2, ho_RegionsB, ho_ConnectedRegionsB;
            HObject ho_SelectedRegionsB, ho_RectangleB1, ho_RectangleB2;
            HObject ho_RegionDifferenceB, ho_ImageReducedB1, ho_RegionB1;
            HObject ho_ConnectedRegionsB2, ho_SelectedRegionsB2, ho_outContours;
            HObject ho_SelectedContours, ho_SortedContours, ho_Region1;
            HObject ho_ConnectedRegions1, ho_SelectedRegions4, ho_SelectedRegions5;
            HObject ho_SelectedRegions6, ho_Contours, ho_SortedContours3;
            HObject ho_obj = null, ho_objj = null, ho_ContoursSplit = null;
            HObject ho_SelectedXLD = null, ho_SortedContours1 = null, ho_M = null;
            HObject ho_Contour = null, ho_SelectedXLD1 = null, ho_SortedContours2 = null;
            HObject ho_ObjectSelectedB = null;

            // Local control variables

            HTuple hv_NumberB = null, hv_Row1 = null, hv_Column1 = null;
            HTuple hv_Phi = null, hv_Length1 = null, hv_Length2 = null;
            HTuple hv_Phi_temp = null, hv_Area_temp = null, hv_Row_temp = null;
            HTuple hv_Column_temp = null, hv_HomMat2D = null, hv_RowA1 = null;
            HTuple hv_ColumnA1 = null, hv_RowA2 = null, hv_ColumnA2 = null;
            HTuple hv_RowA3 = null, hv_ColumnA3 = null, hv_RowA4 = null;
            HTuple hv_ColumnA4 = null, hv_RowB16 = null, hv_ColumnB16 = null;
            HTuple hv_PhiB3 = null, hv_LengthB1 = null, hv_LengthB2 = null;
            HTuple hv_AreaB = null, hv_RowB2 = null, hv_ColumnB2 = null;
            HTuple hv_Number = null, hv_Number3 = null, hv_A1 = null;
            HTuple hv_A2 = null, hv_B1 = null, hv_B2 = null, hv_C1 = null;
            HTuple hv_C2 = null, hv_C3 = null, hv_C4 = null, hv_i = null;
            HTuple hv_Ro1 = new HTuple(), hv_Co1 = new HTuple(), hv_MinBR = new HTuple();
            HTuple hv_MaxBR = new HTuple(), hv_MinBC = new HTuple();
            HTuple hv_MaxBC = new HTuple(), hv_BR = new HTuple(), hv_BC = new HTuple();
            HTuple hv_j = new HTuple(), hv_Number1 = new HTuple();
            HTuple hv_a = new HTuple(), hv_Ro2 = new HTuple(), hv_Co2 = new HTuple();
            HTuple hv_LUC = new HTuple(), hv_RUC = new HTuple(), hv_Ro3 = new HTuple();
            HTuple hv_Co3 = new HTuple(), hv_LDC = new HTuple(), hv_RDC = new HTuple();
            HTuple hv_RowBegin = new HTuple(), hv_ColBegin = new HTuple();
            HTuple hv_RowEnd = new HTuple(), hv_ColEnd = new HTuple();
            HTuple hv_Nr = new HTuple(), hv_Nc = new HTuple(), hv_Dist = new HTuple();
            HTuple hv_Rowc = new HTuple(), hv_Colc = new HTuple();
            HTuple hv_MeanR = new HTuple(), hv_H1 = new HTuple(), hv_H3 = new HTuple();
            HTuple hv_Number2 = new HTuple(), hv_b = new HTuple();
            HTuple hv_Ro4 = new HTuple(), hv_Co4 = new HTuple(), hv_LUR = new HTuple();
            HTuple hv_LDR = new HTuple(), hv_Ro5 = new HTuple(), hv_Co5 = new HTuple();
            HTuple hv_RUR = new HTuple(), hv_RDR = new HTuple(), hv_MeanC = new HTuple();
            HTuple hv_H4 = new HTuple(), hv_H2 = new HTuple(), hv_Distance1 = new HTuple();
            HTuple hv_Distance2 = new HTuple(), hv_AreaA = null, hv_RowA = null;
            HTuple hv_ColumnA = null, hv_phi = null, hv_paozhengwei = null;
            HTuple hv_k = null, hv_Indices = null, hv_liebo = null;
            HTuple hv_NumB = null, hv_count = null, hv_I = new HTuple();
            HTuple hv_AreaB1 = new HTuple(), hv_RowB3 = new HTuple();
            HTuple hv_ColumnB3 = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_ImageRotate);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_ImageMedian);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions3);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_GrayImageA);
            HOperatorSet.GenEmptyObj(out ho_GragImageA);
            HOperatorSet.GenEmptyObj(out ho_RegionsA_temp);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsA_temp);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA_temp);
            HOperatorSet.GenEmptyObj(out ho_ImageAffineTrans);
            HOperatorSet.GenEmptyObj(out ho_RegionsA);
            HOperatorSet.GenEmptyObj(out ho_RegionDilationA);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsA);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA);
            HOperatorSet.GenEmptyObj(out ho_RegionsA1);
            HOperatorSet.GenEmptyObj(out ho_RegionOpeningA);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA1);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionA);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA2);
            HOperatorSet.GenEmptyObj(out ho_RegionsB);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsB);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsB);
            HOperatorSet.GenEmptyObj(out ho_RectangleB1);
            HOperatorSet.GenEmptyObj(out ho_RectangleB2);
            HOperatorSet.GenEmptyObj(out ho_RegionDifferenceB);
            HOperatorSet.GenEmptyObj(out ho_ImageReducedB1);
            HOperatorSet.GenEmptyObj(out ho_RegionB1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsB2);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsB2);
            HOperatorSet.GenEmptyObj(out ho_outContours);
            HOperatorSet.GenEmptyObj(out ho_SelectedContours);
            HOperatorSet.GenEmptyObj(out ho_SortedContours);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions4);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions5);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions6);
            HOperatorSet.GenEmptyObj(out ho_Contours);
            HOperatorSet.GenEmptyObj(out ho_SortedContours3);
            HOperatorSet.GenEmptyObj(out ho_obj);
            HOperatorSet.GenEmptyObj(out ho_objj);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_SelectedXLD);
            HOperatorSet.GenEmptyObj(out ho_SortedContours1);
            HOperatorSet.GenEmptyObj(out ho_M);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_SelectedXLD1);
            HOperatorSet.GenEmptyObj(out ho_SortedContours2);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelectedB);
            ho_Image.Dispose();
            if (!this.index)
            {
                HOperatorSet.ReadImage(out ho_Image, "test.bmp");
            }
            else
            {
                HOperatorSet.ReadImage(out ho_Image, "PersonImg\\xiaosy.png");
            }


            //*******************************************第一组预处理***********************************************
            ho_ImageRotate.Dispose();
            HOperatorSet.RotateImage(ho_Image, out ho_ImageRotate, 45, "constant");
            ho_GrayImage.Dispose();
            HOperatorSet.Rgb1ToGray(ho_ImageRotate, out ho_GrayImage);
            ho_ImageMedian.Dispose();
            HOperatorSet.MedianImage(ho_GrayImage, out ho_ImageMedian, "circle", 3, "mirrored");
            ho_Region.Dispose();
            HOperatorSet.Threshold(ho_GrayImage, out ho_Region, 200, 255);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                                     "and", 150, 99999);
            ho_SelectedRegions2.Dispose();
            HOperatorSet.SelectShape(ho_SelectedRegions, out ho_SelectedRegions2, "circularity",
                                     "and", 0.37, 0.7);
            ho_SelectedRegions1.Dispose();
            HOperatorSet.ClosingRectangle1(ho_SelectedRegions2, out ho_SelectedRegions1,
                                           5, 5);
            ho_SelectedRegions3.Dispose();
            HOperatorSet.SelectShape(ho_SelectedRegions1, out ho_SelectedRegions3, "holes_num",
                                     "and", 1, 99);
            ho_SortedRegions.Dispose();
            HOperatorSet.SortRegion(ho_SelectedRegions3, out ho_SortedRegions, "first_point",
                                    "true", "column");
            HOperatorSet.CountObj(ho_SelectedRegions3, out hv_NumberB);
            ho_RegionUnion.Dispose();
            HOperatorSet.Union1(ho_SelectedRegions3, out ho_RegionUnion);
            HOperatorSet.SmallestRectangle2(ho_RegionUnion, out hv_Row1, out hv_Column1,
                                            out hv_Phi, out hv_Length1, out hv_Length2);
            ho_Rectangle.Dispose();
            HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row1, hv_Column1, hv_Phi, hv_Length1,
                                       hv_Length2);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_GrayImage, ho_Rectangle, out ho_ImageReduced);



            //*******************************************第二组预处理***********************************************
            ho_GrayImageA.Dispose();
            HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImageA);
            ho_GragImageA.Dispose();
            HOperatorSet.GrayDilationRect(ho_GrayImageA, out ho_GragImageA, 4, 4);
            ho_RegionsA_temp.Dispose();
            HOperatorSet.Threshold(ho_GrayImageA, out ho_RegionsA_temp, 116, 218);
            ho_ConnectedRegionsA_temp.Dispose();
            HOperatorSet.Connection(ho_RegionsA_temp, out ho_ConnectedRegionsA_temp);
            ho_SelectedRegionsA_temp.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegionsA_temp, out ho_SelectedRegionsA_temp,
                                     "area", "and", 108357, 495042);
            HOperatorSet.OrientationRegion(ho_SelectedRegionsA_temp, out hv_Phi_temp);
            HOperatorSet.AreaCenter(ho_SelectedRegionsA_temp, out hv_Area_temp, out hv_Row_temp,
                                    out hv_Column_temp);
            HOperatorSet.VectorAngleToRigid(hv_Row_temp, hv_Column_temp, hv_Phi_temp, hv_Row_temp,
                                            hv_Column_temp, (new HTuple(0)).TupleRad(), out hv_HomMat2D);
            ho_ImageAffineTrans.Dispose();
            HOperatorSet.AffineTransImage(ho_GrayImageA, out ho_ImageAffineTrans, hv_HomMat2D,
                                          "constant", "false");
            ho_RegionsA.Dispose();
            HOperatorSet.Threshold(ho_ImageAffineTrans, out ho_RegionsA, 116, 218);
            ho_RegionDilationA.Dispose();
            HOperatorSet.DilationRectangle1(ho_RegionsA, out ho_RegionDilationA, 2, 2);
            ho_ConnectedRegionsA.Dispose();
            HOperatorSet.Connection(ho_RegionDilationA, out ho_ConnectedRegionsA);
            ho_SelectedRegionsA.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegionsA, out ho_SelectedRegionsA, "circularity",
                                     "and", 0.001, 0.03);
            ho_RegionsA1.Dispose();
            HOperatorSet.Threshold(ho_GrayImageA, out ho_RegionsA1, 227, 255);
            ho_RegionOpeningA.Dispose();
            HOperatorSet.OpeningCircle(ho_RegionsA1, out ho_RegionOpeningA, 5.5);
            ho_ConnectedRegionsA.Dispose();
            HOperatorSet.Connection(ho_RegionOpeningA, out ho_ConnectedRegionsA);
            ho_SelectedRegionsA1.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegionsA, out ho_SelectedRegionsA1, "row",
                                     "and", 513.2, 601.23);
            ho_RegionUnionA.Dispose();
            HOperatorSet.Union1(ho_SelectedRegionsA1, out ho_RegionUnionA);
            HOperatorSet.SmallestRectangle1(ho_RegionUnionA, out hv_RowA1, out hv_ColumnA1,
                                            out hv_RowA2, out hv_ColumnA2);
            ho_SelectedRegionsA2.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegionsA, out ho_SelectedRegionsA2, "width",
                                     "and", 92.694, 100);
            HOperatorSet.InnerRectangle1(ho_SelectedRegionsA2, out hv_RowA3, out hv_ColumnA3,
                                         out hv_RowA4, out hv_ColumnA4);
            ho_ConnectedRegionsA.Dispose();
            HOperatorSet.Connection(ho_RegionsA1, out ho_ConnectedRegionsA);



            //*******************************************第三组预处理*********************************************
            ho_RegionsB.Dispose();
            HOperatorSet.Threshold(ho_Image, out ho_RegionsB, 131, 153);
            ho_ConnectedRegionsB.Dispose();
            HOperatorSet.Connection(ho_RegionsB, out ho_ConnectedRegionsB);
            ho_SelectedRegionsB.Dispose();
            HOperatorSet.SelectShapeStd(ho_ConnectedRegionsB, out ho_SelectedRegionsB, "max_area",
                                        70);
            HOperatorSet.SmallestRectangle2(ho_SelectedRegionsB, out hv_RowB16, out hv_ColumnB16,
                                            out hv_PhiB3, out hv_LengthB1, out hv_LengthB2);
            ho_RectangleB1.Dispose();
            HOperatorSet.GenRectangle2(out ho_RectangleB1, hv_RowB16 - 5, hv_ColumnB16, hv_PhiB3,
                                       hv_LengthB1 - 5, hv_LengthB2 - 5);
            ho_RectangleB2.Dispose();
            HOperatorSet.GenRectangle2(out ho_RectangleB2, hv_RowB16 - 3, hv_ColumnB16, hv_PhiB3,
                                       hv_LengthB1 - 5, hv_LengthB2 - 18);
            ho_RegionDifferenceB.Dispose();
            HOperatorSet.Difference(ho_RectangleB1, ho_RectangleB2, out ho_RegionDifferenceB
                                    );
            ho_ImageReducedB1.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_RegionDifferenceB, out ho_ImageReducedB1
                                      );
            ho_RegionB1.Dispose();
            HOperatorSet.Threshold(ho_ImageReducedB1, out ho_RegionB1, 200, 255);
            ho_ConnectedRegionsB2.Dispose();
            HOperatorSet.Connection(ho_RegionB1, out ho_ConnectedRegionsB2);
            ho_SelectedRegionsB2.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegionsB2, out ho_SelectedRegionsB2, "area",
                                     "and", 26.01, 200);
            HOperatorSet.AreaCenter(ho_SelectedRegionsB2, out hv_AreaB, out hv_RowB2, out hv_ColumnB2);


            //*****************************************花瓣ABC**************************************************

            //边缘轮廓检测
            ho_outContours.Dispose();
            HOperatorSet.GenContourRegionXld(ho_SelectedRegions3, out ho_outContours, "border");
            ho_SelectedContours.Dispose();
            HOperatorSet.SelectContoursXld(ho_outContours, out ho_SelectedContours, "contour_length",
                                           60, 600, -0.5, 0.5);
            ho_SortedContours.Dispose();
            HOperatorSet.SortContoursXld(ho_SelectedContours, out ho_SortedContours, "upper_left",
                                         "true", "column");
            HOperatorSet.CountObj(ho_SortedContours, out hv_Number);

            //中心轮廓检测
            ho_Region1.Dispose();
            HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 0, 135);
            ho_ConnectedRegions1.Dispose();
            HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions1);
            ho_SelectedRegions4.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions4, "area",
                                     "and", 230, 600);
            ho_SelectedRegions5.Dispose();
            HOperatorSet.SelectShape(ho_SelectedRegions4, out ho_SelectedRegions5, "ratio",
                                     "and", 0.7, 1.352);
            ho_SelectedRegions6.Dispose();
            HOperatorSet.SelectShape(ho_SelectedRegions5, out ho_SelectedRegions6, "circularity",
                                     "and", 0.2, 1);
            ho_Contours.Dispose();
            HOperatorSet.GenContourRegionXld(ho_SelectedRegions6, out ho_Contours, "border");
            ho_SortedContours3.Dispose();
            HOperatorSet.SortContoursXld(ho_Contours, out ho_SortedContours3, "upper_left",
                                         "true", "row");
            HOperatorSet.CountObj(ho_SortedContours3, out hv_Number3);


            if ((int)(new HTuple((((hv_Number + hv_Number3) % 2)).TupleEqual(1))) != 0)
            {
                //这里需要在C#输出无法检测的信息
                HDevelopStop();
            }



            //里面的轮廓 A1左上到右下 A2左下到右上
            hv_A1 = new HTuple();
            hv_A2 = new HTuple();
            //外面的轮廓 B1垂直方向 B2水平方向
            hv_B1 = new HTuple();
            hv_B2 = new HTuple();
            //c1到c4顺时针顺序的四个花瓣长度
            hv_C1 = new HTuple();
            hv_C2 = new HTuple();
            hv_C3 = new HTuple();
            hv_C4 = new HTuple();


            //B的检测
            HTuple end_val103  = hv_Number;
            HTuple step_val103 = 1;

            for (hv_i = 1; hv_i.Continue(end_val103, step_val103); hv_i = hv_i.TupleAdd(step_val103))
            {
                ho_obj.Dispose();
                HOperatorSet.SelectObj(ho_SortedContours, out ho_obj, hv_i);
                HOperatorSet.GetContourXld(ho_obj, out hv_Ro1, out hv_Co1);
                HOperatorSet.TupleMin(hv_Ro1, out hv_MinBR);
                HOperatorSet.TupleMax(hv_Ro1, out hv_MaxBR);
                HOperatorSet.TupleMin(hv_Co1, out hv_MinBC);
                HOperatorSet.TupleMax(hv_Co1, out hv_MaxBC);
                hv_BR = hv_MaxBR - hv_MinBR;
                hv_BC = hv_MaxBC - hv_MinBC;
                hv_B1 = hv_B1.TupleConcat(hv_BR);
                hv_B2 = hv_B2.TupleConcat(hv_BC);



                //A的检测
                HTuple end_val118  = hv_Number3;
                HTuple step_val118 = 1;
                for (hv_j = 1; hv_j.Continue(end_val118, step_val118); hv_j = hv_j.TupleAdd(step_val118))
                {
                    if ((int)(new HTuple(hv_i.TupleEqual(hv_j))) != 0)
                    {
                        ho_objj.Dispose();
                        HOperatorSet.SelectObj(ho_SortedContours3, out ho_objj, hv_j);



                        //C的检测
                        ho_ContoursSplit.Dispose();
                        HOperatorSet.SegmentContoursXld(ho_objj, out ho_ContoursSplit, "lines",
                                                        5, 4, 2);
                        ho_SelectedXLD.Dispose();
                        HOperatorSet.SelectShapeXld(ho_ContoursSplit, out ho_SelectedXLD, "width",
                                                    "and", 13, 99);
                        ho_SortedContours1.Dispose();
                        HOperatorSet.SortContoursXld(ho_SelectedXLD, out ho_SortedContours1, "upper_left",
                                                     "true", "row");
                        HOperatorSet.CountObj(ho_SortedContours1, out hv_Number1);
                        HTuple end_val130  = hv_Number1;
                        HTuple step_val130 = 1;
                        for (hv_a = 1; hv_a.Continue(end_val130, step_val130); hv_a = hv_a.TupleAdd(step_val130))
                        {
                            ho_M.Dispose();
                            HOperatorSet.SelectObj(ho_SortedContours1, out ho_M, hv_a);
                            if ((int)(new HTuple(hv_a.TupleEqual(1))) != 0)
                            {
                                HOperatorSet.GetContourXld(ho_M, out hv_Ro2, out hv_Co2);
                                HOperatorSet.TupleMin(hv_Co2, out hv_LUC);
                                HOperatorSet.TupleMax(hv_Co2, out hv_RUC);
                            }
                            else
                            {
                                HOperatorSet.GetContourXld(ho_M, out hv_Ro3, out hv_Co3);
                                HOperatorSet.TupleMin(hv_Co3, out hv_LDC);
                                HOperatorSet.TupleMax(hv_Co3, out hv_RDC);
                            }
                            HOperatorSet.FitLineContourXld(ho_M, "huber", -1, 0, 5, 2, out hv_RowBegin,
                                                           out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc,
                                                           out hv_Dist);
                            ho_Contour.Dispose();
                            HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_RowBegin.TupleConcat(
                                                                  hv_RowEnd), hv_ColBegin.TupleConcat(hv_ColEnd));
                            HOperatorSet.GetContourXld(ho_Contour, out hv_Rowc, out hv_Colc);
                            HOperatorSet.TupleMean(hv_Rowc, out hv_MeanR);
                            if ((int)(new HTuple(hv_a.TupleEqual(1))) != 0)
                            {
                                hv_H1 = hv_MeanR - hv_MinBR;
                                hv_C1 = hv_C1.TupleConcat(hv_H1);
                            }
                            else
                            {
                                hv_H3 = hv_MaxBR - hv_MeanR;
                                hv_C3 = hv_C3.TupleConcat(hv_H3);
                            }
                        }

                        ho_SelectedXLD1.Dispose();
                        HOperatorSet.SelectShapeXld(ho_ContoursSplit, out ho_SelectedXLD1, "height",
                                                    "and", 13, 99);
                        ho_SortedContours2.Dispose();
                        HOperatorSet.SortContoursXld(ho_SelectedXLD1, out ho_SortedContours2, "upper_left",
                                                     "true", "column");
                        HOperatorSet.CountObj(ho_SortedContours2, out hv_Number2);
                        HTuple end_val157  = hv_Number2;
                        HTuple step_val157 = 1;
                        for (hv_b = 1; hv_b.Continue(end_val157, step_val157); hv_b = hv_b.TupleAdd(step_val157))
                        {
                            ho_M.Dispose();
                            HOperatorSet.SelectObj(ho_SortedContours2, out ho_M, hv_b);
                            if ((int)(new HTuple(hv_b.TupleEqual(1))) != 0)
                            {
                                HOperatorSet.GetContourXld(ho_M, out hv_Ro4, out hv_Co4);
                                HOperatorSet.TupleMin(hv_Ro4, out hv_LUR);
                                HOperatorSet.TupleMax(hv_Ro4, out hv_LDR);
                            }
                            else
                            {
                                HOperatorSet.GetContourXld(ho_M, out hv_Ro5, out hv_Co5);
                                HOperatorSet.TupleMin(hv_Ro5, out hv_RUR);
                                HOperatorSet.TupleMax(hv_Ro5, out hv_RDR);
                            }
                            HOperatorSet.FitLineContourXld(ho_M, "huber", -1, 0, 5, 2, out hv_RowBegin,
                                                           out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc,
                                                           out hv_Dist);
                            ho_Contour.Dispose();
                            HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_RowBegin.TupleConcat(
                                                                  hv_RowEnd), hv_ColBegin.TupleConcat(hv_ColEnd));
                            HOperatorSet.GetContourXld(ho_Contour, out hv_Rowc, out hv_Colc);
                            HOperatorSet.TupleMean(hv_Colc, out hv_MeanC);
                            if ((int)(new HTuple(hv_b.TupleEqual(1))) != 0)
                            {
                                hv_H4 = hv_MeanC - hv_MinBC;
                                hv_C4 = hv_C4.TupleConcat(hv_H4);
                            }
                            else
                            {
                                hv_H2 = hv_MaxBC - hv_MeanC;
                                hv_C2 = hv_C2.TupleConcat(hv_H2);
                            }
                        }

                        HOperatorSet.DistancePp(hv_LUC, hv_LUR, hv_RDC, hv_RDR, out hv_Distance1);
                        HOperatorSet.DistancePp(hv_RUC, hv_RUR, hv_LDC, hv_LDR, out hv_Distance2);
                        hv_A1 = hv_A1.TupleConcat(hv_Distance1);
                        hv_A2 = hv_A2.TupleConcat(hv_Distance2);
                    }
                }
            }

            //*********************************倾斜角度 跑针位 k*******************************************
            HOperatorSet.AreaCenter(ho_SelectedRegionsA, out hv_AreaA, out hv_RowA, out hv_ColumnA);
            HOperatorSet.OrientationRegion(ho_SelectedRegionsA, out hv_Phi);
            hv_Phi         = hv_Phi.TupleDeg();
            hv_phi         = ((90 - (hv_Phi.TupleAbs()))).TupleAbs();
            hv_paozhengwei = (hv_RowA3 - ((hv_RowA2 / 2) + (hv_RowA1 / 2))) - 4;
            hv_k           = (hv_RowA2 - hv_RowA1) + 4;



            //***************************************裂箔****************************************************
            HOperatorSet.TupleSortIndex(hv_AreaB, out hv_Indices);
            hv_liebo = new HTuple();
            hv_NumB  = new HTuple(hv_Indices.TupleLength());
            hv_count = 0;
            if ((int)(new HTuple(hv_NumB.TupleGreater(0))) != 0)
            {
                HTuple end_val208  = hv_NumB;
                HTuple step_val208 = 1;
                for (hv_I = 1; hv_I.Continue(end_val208, step_val208); hv_I = hv_I.TupleAdd(step_val208))
                {
                    ho_ObjectSelectedB.Dispose();
                    HOperatorSet.SelectObj(ho_SelectedRegionsB2, out ho_ObjectSelectedB, hv_I);
                    HOperatorSet.AreaCenter(ho_ObjectSelectedB, out hv_AreaB1, out hv_RowB3,
                                            out hv_ColumnB3);
                    if ((int)(new HTuple(hv_AreaB1.TupleGreater(30))) != 0)
                    {
                        hv_count = hv_count + 1;
                    }
                    else
                    {
                    }
                }
            }
            else
            {
            }
            if (hv_liebo == null)
            {
                hv_liebo = new HTuple();
            }
            hv_liebo[0] = hv_count;

            HTuple result = new HTuple();

            result.Append(hv_A1[0]);
            result.Append(hv_A2[0]);
            result.Append(hv_B1[0]);
            result.Append(hv_B2[0]);
            result.Append(hv_C1[0]);
            result.Append(hv_C2[0]);
            result.Append(hv_C3[0]);
            result.Append(hv_C4[0]);

            result.Append(hv_phi[0]);
            result.Append(hv_paozhengwei[0]);
            result.Append(hv_liebo[0]);



            //**************************************************************************************************



            ho_Image.Dispose();
            ho_ImageRotate.Dispose();
            ho_GrayImage.Dispose();
            ho_ImageMedian.Dispose();
            ho_Region.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();
            ho_SelectedRegions2.Dispose();
            ho_SelectedRegions1.Dispose();
            ho_SelectedRegions3.Dispose();
            ho_SortedRegions.Dispose();
            ho_RegionUnion.Dispose();
            ho_Rectangle.Dispose();
            ho_ImageReduced.Dispose();
            ho_GrayImageA.Dispose();
            ho_GragImageA.Dispose();
            ho_RegionsA_temp.Dispose();
            ho_ConnectedRegionsA_temp.Dispose();
            ho_SelectedRegionsA_temp.Dispose();
            ho_ImageAffineTrans.Dispose();
            ho_RegionsA.Dispose();
            ho_RegionDilationA.Dispose();
            ho_ConnectedRegionsA.Dispose();
            ho_SelectedRegionsA.Dispose();
            ho_RegionsA1.Dispose();
            ho_RegionOpeningA.Dispose();
            ho_SelectedRegionsA1.Dispose();
            ho_RegionUnionA.Dispose();
            ho_SelectedRegionsA2.Dispose();
            ho_RegionsB.Dispose();
            ho_ConnectedRegionsB.Dispose();
            ho_SelectedRegionsB.Dispose();
            ho_RectangleB1.Dispose();
            ho_RectangleB2.Dispose();
            ho_RegionDifferenceB.Dispose();
            ho_ImageReducedB1.Dispose();
            ho_RegionB1.Dispose();
            ho_ConnectedRegionsB2.Dispose();
            ho_SelectedRegionsB2.Dispose();
            ho_outContours.Dispose();
            ho_SelectedContours.Dispose();
            ho_SortedContours.Dispose();
            ho_Region1.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_SelectedRegions4.Dispose();
            ho_SelectedRegions5.Dispose();
            ho_SelectedRegions6.Dispose();
            ho_Contours.Dispose();
            ho_SortedContours3.Dispose();
            ho_obj.Dispose();
            ho_objj.Dispose();
            ho_ContoursSplit.Dispose();
            ho_SelectedXLD.Dispose();
            ho_SortedContours1.Dispose();
            ho_M.Dispose();
            ho_Contour.Dispose();
            ho_SelectedXLD1.Dispose();
            ho_SortedContours2.Dispose();
            ho_ObjectSelectedB.Dispose();
            return(result);
        }
Exemplo n.º 20
0
        public MeasureResult Action()
        {
            #region 輸出結果
            CircleResult mResult = null;
            #endregion

            // Local iconic variables

            HObject ho_R3_Circle = null;
            HObject ho_R3_ROI_Image = null, ho_R3_Region = null, ho_R3_ImageReduced = null;
            HObject ho_R3_Edges = null, ho_R3_ContoursSplit = null, ho_R3_SingleSegment = null;
            HObject ho_R3_ContEllipse = null;

            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_R3_R;
            HTuple hv_STD_R3_Row = new HTuple(), hv_STD_R3_Col = new HTuple();
            HTuple hv_STD_R3_V_Row = new HTuple(), hv_STD_R3_V_Col = new HTuple();
            HTuple hv_R3_X = new HTuple(), hv_R3_Y = new HTuple(), hv_R3_Pos_Row = new HTuple();
            HTuple hv_R3_Pos_Col = new HTuple(), hv_alpha = new HTuple();
            HTuple hv_R3_low = new HTuple(), hv_R3_high = new HTuple();
            HTuple hv_R3_NumSegments = new HTuple(), hv_NumCircles = new HTuple();
            HTuple hv_Num_Circle_Point = new HTuple(), hv_R3 = new HTuple();
            HTuple hv_i = new HTuple(), hv_Attrib = new HTuple(), hv_R3_Row = new HTuple();
            HTuple hv_R3_Column = new HTuple(), hv_R3_Radius = new HTuple();
            HTuple hv_R3_StartPhi = new HTuple(), hv_R3_EndPhi = new HTuple();
            HTuple hv_R3_PointOrder = new HTuple(), hv_R3_MinDist = new HTuple();
            HTuple hv_R3_MaxDist = new HTuple(), hv_R3_AvgDist = new HTuple();
            HTuple hv_R3_SigmaDist = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_R3_Circle);
            HOperatorSet.GenEmptyObj(out ho_R3_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R3_Region);
            HOperatorSet.GenEmptyObj(out ho_R3_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R3_Edges);
            HOperatorSet.GenEmptyObj(out ho_R3_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R3_SingleSegment);
            HOperatorSet.GenEmptyObj(out ho_R3_ContEllipse);

            //Measure: SDMS_R3
            //Author: John Hsieh
            //Date: 2012
            //ho_Image.Dispose();
            //HOperatorSet.ReadImage(out ho_Image, "Images/STD.bmp");
            //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
            //// dev_update_off(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;

            //****Model All
            //HOperatorSet.ReadShapeModel("D:/Projects/Halcon/SDMS/SDMS_Measure/Model/MatchingAll.shm",
            //	out hv_AllModelId);
            //ho_AllModelContours.Dispose();
            //HOperatorSet.GetShapeModelContours(out ho_AllModelContours, hv_AllModelId, 1);
            //HOperatorSet.FindShapeModel(ho_Image, hv_AllModelId, (new HTuple(0)).TupleRad()
            //	, (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", 6, 0.75, out hv_AllModelRow,
            //	out hv_AllModelColumn, out hv_AllModelAngle, out hv_AllModelScore);

            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****Display
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            //*****R3
            hv_R3_R = 29;

            //STD R3_ 位置
            hv_STD_R3_Row = 252;
            hv_STD_R3_Col = 1381;

            //STD 向量 STD_R3_
            hv_STD_R3_V_Row = hv_STD_R3_Row - hv_STD_Row;
            hv_STD_R3_V_Col = hv_STD_R3_Col - hv_STD_Col;

            //R3_X, R3_Y 分量
            hv_R3_X = (hv_STD_R3_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R3_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                ));
            hv_R3_Y = (hv_STD_R3_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R3_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                ));

            //目前圖形 R3_ 位置
            hv_R3_Pos_Row = (hv_STD_Row + hv_R3_Y) + hv_OffsetRow;
            hv_R3_Pos_Col = (hv_STD_Col + hv_R3_X) + hv_OffsetCol;

            //** 開始計算
            ho_R3_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_R3_Circle, hv_R3_Pos_Row, hv_R3_Pos_Col, hv_R3_R);
            ho_R3_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_R3_Circle, out ho_R3_ROI_Image);
            ho_R3_Region.Dispose();
            HOperatorSet.FastThreshold(ho_R3_ROI_Image, out ho_R3_Region, 100, 255, 15);
            ho_R3_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_R3_ROI_Image, ho_R3_Region, out ho_R3_ImageReduced
                );
            //stop ()
            //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
            hv_alpha = 0.9;
            hv_R3_low = 20;
            hv_R3_high = 60;
            ho_R3_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_R3_ImageReduced, out ho_R3_Edges, "sobel_fast",
                hv_alpha, hv_R3_low, hv_R3_high);
            //stop ()
            //*所有的數值越小,表示容錯範圍大,反之亦然
            ho_R3_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_R3_Edges, out ho_R3_ContoursSplit, "lines_circles",
                17, 4, 2);
            //Display the results
            //===========================================================
            HOperatorSet.CountObj(ho_R3_ContoursSplit, out hv_R3_NumSegments);
            hv_NumCircles = 0;
            hv_Num_Circle_Point = 0;
            hv_R3 = 999;
            for (hv_i = 1; hv_i.Continue(hv_R3_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
            {
                ho_R3_SingleSegment.Dispose();
                HOperatorSet.SelectObj(ho_R3_ContoursSplit, out ho_R3_SingleSegment, hv_i);
                HOperatorSet.GetContourGlobalAttribXld(ho_R3_SingleSegment, "cont_approx",
                    out hv_Attrib);
                if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                {
                    HOperatorSet.FitCircleContourXld(ho_R3_SingleSegment, "atukey", -1, 2,
                        hv_Num_Circle_Point, 5, 2, out hv_R3_Row, out hv_R3_Column, out hv_R3_Radius,
                        out hv_R3_StartPhi, out hv_R3_EndPhi, out hv_R3_PointOrder);

                    hv_NumCircles = hv_NumCircles + 1;
                    if ((int)(new HTuple(hv_R3.TupleGreater(hv_R3_Radius))) != 0)
                    {
                        hv_R3 = hv_R3_Radius.Clone();
                        mResult = new CircleResult()
                        {
                            Row = new HTuple(hv_R3_Row),
                            Col = new HTuple(hv_R3_Column),
                            Radius = new HTuple(hv_R3_Radius),
                            StartPhi = new HTuple(hv_R3_StartPhi),
                            EndPhi = new HTuple(hv_R3_EndPhi),
                            PointOrder = new HTuple(hv_R3_PointOrder),
                        };
                        //HOperatorSet.SetTposition(hv_WindowHandle, (hv_R3_Pos_Row - hv_msgOffsetY) + 60,
                        //	(hv_R3_Pos_Col + hv_msgOffsetX) - 60);
                        //HOperatorSet.WriteString(hv_WindowHandle, "R3");
                        //stop ()
                    }
                }
            }

            //*****R3 End
            ho_R3_Circle.Dispose();
            ho_R3_ROI_Image.Dispose();
            ho_R3_Region.Dispose();
            ho_R3_ImageReduced.Dispose();
            ho_R3_Edges.Dispose();
            ho_R3_ContoursSplit.Dispose();
            ho_R3_SingleSegment.Dispose();
            ho_R3_ContEllipse.Dispose();

            return mResult;
        }
Exemplo n.º 21
0
        //DateTime t1, t2, t3, t4,t5,t6,t7;
        private void action()
        {
            HObject ho_Rectangle, ho_ImageReduced, imagexz, retxz;
            HObject ho_Border, ho_Circle, ho_ImageReduced2, ho_Region, ho_RegionLines;
            HObject ho_Rectangle1 = null, ho_ImageReduced1 = null, ho_Border1 = null;
            HObject ho_SelectedContours = null;

            // Local control variables

            HTuple hv_Row1 = null, hv_Column1 = null, hv_Angle = null, hv_l1 = null, hv_l2 = null;
            HTuple hv_Score = null, hv_zj = null, hv_Area = null, hv_Row3 = null;
            HTuple hv_Column2 = null, hv_Phi1 = null, hv_zpj = null;
            HTuple hv_zcj = null, hv_zpr = null, hv_zpc = null, hv_zcr = null;
            HTuple hv_zcc = null, hv_Tp = null, hv_Tc = null, hv_Length = null;
            HTuple hv_dx = null, hv_dn = null, hv_dxr = null, hv_dxc = null;
            HTuple hv_dnr = null, hv_dnc = null, hv_i = null;
            //HTuple hv_Col = new HTuple(), hv_dis = new HTuple(), hv_dxi = new HTuple(), hv_Row2 = new HTuple();
            //HTuple hv_dni = new HTuple(), hv_Ix = new HTuple(), hv_In = new HTuple();
            //HTuple hv_Exception = new HTuple(), hv_j = new HTuple(), hv_jh = new HTuple();
            HTuple hv_disdxpx = null;
            HTuple hv_disdnpx = null;
            HTuple hv_djd = null, hv_djdr = null, hv_djdc = null, hv_djx = null;
            HTuple hv_djxr = null, hv_djxc = null, hv_xjd = null, hv_xjdr = null;
            HTuple hv_xjdc = null, hv_xjx = null, hv_xjxr = null, hv_xjxc = null;
            HTuple hv_ljd = null, hv_ljx = null, hv_dj = null, hv_xj = null;
            HTuple hv_djn = null, hv_djm = null, hv_xjn = null, hv_xjm = null;
            HTuple hv_djzd = null, hv_djzx = null, hv_xjzd = null;
            HTuple hv_xjzx = null, hv_ljtd = null, hv_ljtx = null;
            HTuple hv_Length3 = null, hv_Length4 = null, hv_ljdx = null;
            HTuple hv_ljdn = null, hv_ljxx = null, hv_ljxn = null;
            HTuple hv_ljdm = null, hv_ljxm = null, hv_djiaodu = null;
            HTuple hv_xjiaodu = null, hv_Length5 = null, hv_Length6 = null;
            HTuple hv_djiaodu1 = null, hv_xjiaodu1 = null, hv_jjdd = null;
            HTuple hv_jjdx = null, hv_jjxd = null, hv_jjxx = null;
            HTuple hv_jjdm = null, hv_jjxm = null;

            // Initialize local and output iconic variables

            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Border);
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced2);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Border1);
            HOperatorSet.GenEmptyObj(out ho_SelectedContours);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out imagexz);
            HOperatorSet.GenEmptyObj(out retxz);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            //t3 = DateTime.Now;
            try
            {
                if (hv_ModelID == null)
                {
                    HOperatorSet.ReadShapeModel(PathHelper.currentProductPath + @"\lwspmd.shm", out hv_ModelID);
                }
                retxz.Dispose();
                HOperatorSet.GenRectangle2(out retxz, hv1_Row, hv1_Column, hv1_Phi, hv1_Length1, hv1_Length2);
                imagexz.Dispose();
                HOperatorSet.ReduceDomain(Image, retxz, out imagexz);
                HOperatorSet.FindShapeModel(imagexz, hv_ModelID, -3.14, 6.29, 0.7, 0, 0.1,
                                            "least_squares", 0, 0.8, out hv_Row1, out hv_Column1, out hv_Angle, out hv_Score);
                hv_zj = (((((hv_Row1.TupleMax()) - (hv_Row1.TupleMin())) * ((hv_Row1.TupleMax()
                                                                             ) - (hv_Row1.TupleMin()))) + (((hv_Column1.TupleMax()) - (hv_Column1.TupleMin()
                                                                                                                                       )) * ((hv_Column1.TupleMax()) - (hv_Column1.TupleMin()))))).TupleSqrt();
                ho_Circle.Dispose();
                HOperatorSet.GenCircle(out ho_Circle, hv_Row1.TupleMean(), hv_Column1.TupleMean()
                                       , hv_zj / 2);
                ho_ImageReduced2.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Circle, out ho_ImageReduced2);
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced2, out ho_Region, 0, thv);
                HOperatorSet.AreaCenter(ho_Region, out hv_Area, out hv_Row3, out hv_Column2);
                HOperatorSet.OrientationRegion(ho_Region, out hv_Phi1);
                hv_zpj = hv_Phi1.Clone();
                hv_zcj = hv_Phi1 + ((new HTuple(90)).TupleRad());
                hv_zpr = hv_Row3 + ((hv_zcj.TupleSin()) * hv_zj);
                hv_zpc = hv_Column2 - ((hv_zcj.TupleCos()) * hv_zj);
                hv_zcr = hv_Row3 + ((hv_zpj.TupleSin()) * hv_zj);
                hv_zcc = hv_Row3 - ((hv_zpj.TupleCos()) * hv_zj);
                HOperatorSet.TupleTan(hv_zpj, out hv_Tp);
                HOperatorSet.TupleTan(hv_zcj, out hv_Tc);
                HOperatorSet.TupleLength(hv_Row1, out hv_Length);
                hv_dx  = new HTuple();
                hv_dn  = new HTuple();
                hv_dxr = new HTuple();
                hv_dxc = new HTuple();
                hv_dnr = new HTuple();
                hv_dnc = new HTuple();
                HTuple end_val29  = hv_Length - 1;
                HTuple step_val29 = 1;
                for (hv_i = 0; hv_i.Continue(end_val29, step_val29); hv_i = hv_i.TupleAdd(step_val29))
                {
                    //try


                    ho_Rectangle1.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row1.TupleSelect(hv_i),
                                               hv_Column1.TupleSelect(hv_i), hv_Phi + (hv_Angle.TupleSelect(hv_i)),
                                               hv_Length1, hv_Length2);
                    ho_ImageReduced1.Dispose();
                    HOperatorSet.ReduceDomain(Image, ho_Rectangle1, out ho_ImageReduced1
                                              );
                    ho_Border1.Dispose();
                    HOperatorSet.ThresholdSubPix(ho_ImageReduced1, out ho_Border1, thv);
                    ho_SelectedContours.Dispose();
                    HOperatorSet.SelectContoursXld(ho_Border1, out ho_SelectedContours, "contour_length",
                                                   30, 2000000, -0.5, 0.5);
                    HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContours, out ho_SelectedContours, 10, 1, "attr_keep");
                    HOperatorSet.GetContourXld(ho_SelectedContours, out hv_Row2, out hv_Col);
                    hv_dis = ((((((hv_Tp * hv_Col) + hv_Row2) - (hv_Tp * hv_zpc)) - hv_zpr) / (((1 + (hv_Tp * hv_Tp))).TupleSqrt()
                                                                                               ))).TupleAbs();
                    hv_dxi = hv_dis.TupleMax();
                    hv_dni = hv_dis.TupleMin();
                    HOperatorSet.TupleFind(hv_dis, hv_dxi, out hv_Ix);
                    HOperatorSet.TupleFind(hv_dis, hv_dni, out hv_In);
                    hv_dx  = hv_dx.TupleConcat(hv_dxi);
                    hv_dn  = hv_dn.TupleConcat(hv_dni);
                    hv_dxr = hv_dxr.TupleConcat(hv_Row2.TupleSelect(hv_Ix));
                    hv_dxc = hv_dxc.TupleConcat(hv_Col.TupleSelect(hv_Ix));
                    hv_dnr = hv_dnr.TupleConcat(hv_Row2.TupleSelect(hv_In));
                    hv_dnc = hv_dnc.TupleConcat(hv_Col.TupleSelect(hv_In));
                    //catch (Exception)
                    //endtry
                    //stop ()
                }
                hv_disdxpx = ((((((hv_Tc * hv_dxc) + hv_dxr) - (hv_Tc * hv_zcc)) - hv_zcr) / (((1 + (hv_Tc * hv_Tc))).TupleSqrt()
                                                                                              ))).TupleAbs();
                hv_disdnpx = ((((((hv_Tc * hv_dnc) + hv_dnr) - (hv_Tc * hv_zcc)) - hv_zcr) / (((1 + (hv_Tc * hv_Tc))).TupleSqrt()
                                                                                              ))).TupleAbs();
                HTuple end_val55  = hv_Length - 2;
                HTuple step_val55 = 1;
                //t1 = DateTime.Now;
                for (hv_i = 0; hv_i.Continue(end_val55, step_val55); hv_i = hv_i.TupleAdd(step_val55))
                {
                    //try


                    HTuple end_val59  = hv_Length - 1;
                    HTuple step_val59 = 1;
                    for (hv_j = hv_i + 1; hv_j.Continue(end_val59, step_val59); hv_j = hv_j.TupleAdd(step_val59))
                    {
                        if ((int)(new HTuple(((hv_disdxpx.TupleSelect(hv_i))).TupleGreater(hv_disdxpx.TupleSelect(
                                                                                               hv_j)))) != 0)
                        {
                            hv_jh = hv_disdxpx.TupleSelect(hv_i);
                            if (hv_disdxpx == null)
                            {
                                hv_disdxpx = new HTuple();
                            }
                            hv_disdxpx[hv_i] = hv_disdxpx.TupleSelect(hv_j);
                            if (hv_disdxpx == null)
                            {
                                hv_disdxpx = new HTuple();
                            }
                            hv_disdxpx[hv_j] = hv_jh;
                            hv_jh            = hv_dx.TupleSelect(hv_i);
                            if (hv_dx == null)
                            {
                                hv_dx = new HTuple();
                            }
                            hv_dx[hv_i] = hv_dx.TupleSelect(hv_j);
                            if (hv_dx == null)
                            {
                                hv_dx = new HTuple();
                            }
                            hv_dx[hv_j] = hv_jh;
                            hv_jh       = hv_dxr.TupleSelect(hv_i);
                            if (hv_dxr == null)
                            {
                                hv_dxr = new HTuple();
                            }
                            hv_dxr[hv_i] = hv_dxr.TupleSelect(hv_j);
                            if (hv_dxr == null)
                            {
                                hv_dxr = new HTuple();
                            }
                            hv_dxr[hv_j] = hv_jh;
                            hv_jh        = hv_dxc.TupleSelect(hv_i);
                            if (hv_dxc == null)
                            {
                                hv_dxc = new HTuple();
                            }
                            hv_dxc[hv_i] = hv_dxc.TupleSelect(hv_j);
                            if (hv_dxc == null)
                            {
                                hv_dxc = new HTuple();
                            }
                            hv_dxc[hv_j] = hv_jh;
                        }
                        if ((int)(new HTuple(((hv_disdnpx.TupleSelect(hv_i))).TupleGreater(hv_disdnpx.TupleSelect(
                                                                                               hv_j)))) != 0)
                        {
                            hv_jh = hv_disdnpx.TupleSelect(hv_i);
                            if (hv_disdnpx == null)
                            {
                                hv_disdnpx = new HTuple();
                            }
                            hv_disdnpx[hv_i] = hv_disdnpx.TupleSelect(hv_j);
                            if (hv_disdnpx == null)
                            {
                                hv_disdnpx = new HTuple();
                            }
                            hv_disdnpx[hv_j] = hv_jh;
                            hv_jh            = hv_dn.TupleSelect(hv_i);
                            if (hv_dn == null)
                            {
                                hv_dn = new HTuple();
                            }
                            hv_dn[hv_i] = hv_dn.TupleSelect(hv_j);
                            if (hv_dn == null)
                            {
                                hv_dn = new HTuple();
                            }
                            hv_dn[hv_j] = hv_jh;
                            hv_jh       = hv_dnr.TupleSelect(hv_i);
                            if (hv_dnr == null)
                            {
                                hv_dnr = new HTuple();
                            }
                            hv_dnr[hv_i] = hv_dnr.TupleSelect(hv_j);
                            if (hv_dnr == null)
                            {
                                hv_dnr = new HTuple();
                            }
                            hv_dnr[hv_j] = hv_jh;
                            hv_jh        = hv_dnc.TupleSelect(hv_i);
                            if (hv_dnc == null)
                            {
                                hv_dnc = new HTuple();
                            }
                            hv_dnc[hv_i] = hv_dnc.TupleSelect(hv_j);
                            if (hv_dnc == null)
                            {
                                hv_dnc = new HTuple();
                            }
                            hv_dnc[hv_j] = hv_jh;
                        }
                    }
                    //catch (Exception)
                    //endtry
                }
                //t2 = DateTime.Now;
                hv_djd  = new HTuple();
                hv_djdr = new HTuple();
                hv_djdc = new HTuple();
                hv_djx  = new HTuple();
                hv_djxr = new HTuple();
                hv_djxc = new HTuple();
                hv_xjd  = new HTuple();
                hv_xjdr = new HTuple();
                hv_xjdc = new HTuple();
                hv_xjx  = new HTuple();
                hv_xjxr = new HTuple();
                hv_xjxc = new HTuple();
                hv_ljd  = new HTuple();
                hv_ljx  = new HTuple();
                HTuple end_val107  = hv_Length - 1;
                HTuple step_val107 = 1;
                for (hv_i = 0; hv_i.Continue(end_val107, step_val107); hv_i = hv_i.TupleAdd(step_val107))
                {
                    if ((int)(new HTuple(((hv_dx.TupleSelect(hv_i))).TupleGreater(hv_zj))) != 0)
                    {
                        for (hv_j = -3; (int)hv_j <= 3; hv_j = (int)hv_j + 1)
                        {
                            if (((hv_i + hv_j) >= 0) && ((hv_i + hv_j) <= end_val107))
                            {
                                if ((int)((new HTuple(((hv_disdnpx.TupleSelect(hv_i + hv_j))).TupleGreater(
                                                          hv_disdxpx.TupleSelect(hv_i)))).TupleAnd(new HTuple(((hv_dn.TupleSelect(
                                                                                                                    hv_i + hv_j))).TupleLess(hv_zj)))) != 0)
                                {
                                    hv_ljd  = hv_ljd.TupleConcat(hv_disdxpx.TupleSelect(hv_i));
                                    hv_djd  = hv_djd.TupleConcat(hv_dx.TupleSelect(hv_i));
                                    hv_djdr = hv_djdr.TupleConcat(hv_dxr.TupleSelect(hv_i));
                                    hv_djdc = hv_djdc.TupleConcat(hv_dxc.TupleSelect(hv_i));
                                    hv_djx  = hv_djx.TupleConcat(hv_dn.TupleSelect(hv_i + hv_j));
                                    hv_djxr = hv_djxr.TupleConcat(hv_dnr.TupleSelect(hv_i + hv_j));
                                    hv_djxc = hv_djxc.TupleConcat(hv_dnc.TupleSelect(hv_i + hv_j));
                                    break;
                                }
                            }
                            // catch (Exception)
                            //catch (HalconException)
                            //{
                            //    //HDevExpDefaultException1.ToHTuple(out hv_Exception);
                            //}
                        }
                    }

                    if ((int)(new HTuple(((hv_dn.TupleSelect(hv_i))).TupleGreater(hv_zj))) != 0)
                    {
                        for (hv_j = -3; (int)hv_j <= 3; hv_j = (int)hv_j + 1)
                        {
                            //try
                            if (((hv_i + hv_j) >= 0) && ((hv_i + hv_j) <= end_val107))
                            {
                                if ((int)((new HTuple(((hv_disdxpx.TupleSelect(hv_i + hv_j))).TupleGreater(
                                                          hv_disdnpx.TupleSelect(hv_i)))).TupleAnd(new HTuple(((hv_dx.TupleSelect(
                                                                                                                    hv_i + hv_j))).TupleLess(hv_zj)))) != 0)
                                {
                                    hv_ljx  = hv_ljx.TupleConcat(hv_disdnpx.TupleSelect(hv_i));
                                    hv_xjd  = hv_xjd.TupleConcat(hv_dn.TupleSelect(hv_i));
                                    hv_xjdr = hv_xjdr.TupleConcat(hv_dnr.TupleSelect(hv_i));
                                    hv_xjdc = hv_xjdc.TupleConcat(hv_dnc.TupleSelect(hv_i));
                                    hv_xjx  = hv_xjx.TupleConcat(hv_dx.TupleSelect(hv_i + hv_j));
                                    hv_xjxr = hv_xjxr.TupleConcat(hv_dxr.TupleSelect(hv_i + hv_j));
                                    hv_xjxc = hv_xjxc.TupleConcat(hv_dxc.TupleSelect(hv_i + hv_j));
                                    break;
                                }
                            }
                            // catch (Exception)
                            //catch (HalconException)
                            //{
                            //    //HDevExpDefaultException1.ToHTuple(out hv_Exception);
                            //}
                        }
                    }
                }
                HOperatorSet.TupleLength(hv_ljd, out hv_l1);
                HTuple end_val153  = 1;
                HTuple step_val153 = -1;
                for (hv_i = hv_l1 - 1; hv_i.Continue(end_val153, step_val153); hv_i = hv_i.TupleAdd(step_val153))
                {
                    if ((int)(new HTuple(((hv_ljd.TupleSelect(hv_i))).TupleEqual(hv_ljd.TupleSelect(
                                                                                     hv_i - 1)))) != 0)
                    {
                        HOperatorSet.TupleRemove(hv_ljd, hv_i, out hv_ljd);
                        HOperatorSet.TupleRemove(hv_djd, hv_i, out hv_djd);
                        HOperatorSet.TupleRemove(hv_djdr, hv_i, out hv_djdr);
                        HOperatorSet.TupleRemove(hv_djdc, hv_i, out hv_djdc);
                        HOperatorSet.TupleRemove(hv_djx, hv_i, out hv_djx);
                        HOperatorSet.TupleRemove(hv_djxr, hv_i, out hv_djxr);
                        HOperatorSet.TupleRemove(hv_djxc, hv_i, out hv_djxc);
                    }
                }
                HOperatorSet.TupleLength(hv_ljx, out hv_l2);
                HTuple end_val165  = 1;
                HTuple step_val165 = -1;
                for (hv_i = hv_l2 - 1; hv_i.Continue(end_val165, step_val165); hv_i = hv_i.TupleAdd(step_val165))
                {
                    if ((int)(new HTuple(((hv_ljd.TupleSelect(hv_i))).TupleEqual(hv_ljd.TupleSelect(
                                                                                     hv_i - 1)))) != 0)
                    {
                        HOperatorSet.TupleRemove(hv_ljx, hv_i, out hv_ljx);
                        HOperatorSet.TupleRemove(hv_xjd, hv_i, out hv_xjd);
                        HOperatorSet.TupleRemove(hv_xjdr, hv_i, out hv_xjdr);
                        HOperatorSet.TupleRemove(hv_xjdc, hv_i, out hv_xjdc);
                        HOperatorSet.TupleRemove(hv_xjx, hv_i, out hv_xjx);
                        HOperatorSet.TupleRemove(hv_xjxr, hv_i, out hv_xjxr);
                        HOperatorSet.TupleRemove(hv_xjxc, hv_i, out hv_xjxc);
                    }
                }
                HOperatorSet.TupleLength(hv_ljx, out hv_l2);
                HOperatorSet.TupleLength(hv_ljd, out hv_l1);
                //t5 = DateTime.Now;
                hv_dj   = hv_djd - hv_djx;
                hv_xj   = hv_xjd - hv_xjx;
                hv_djx  = hv_dj.TupleMax();
                hv_djn  = hv_dj.TupleMin();
                hv_djm  = hv_dj.TupleMean();
                hv_xjx  = hv_xj.TupleMax();
                hv_xjn  = hv_xj.TupleMin();
                hv_xjm  = hv_xj.TupleMean();
                hv_djzd = (hv_djd.TupleMax()) - (hv_djx.TupleMin());
                hv_djzx = (hv_djd.TupleMin()) - (hv_djx.TupleMax());
                hv_xjzd = (hv_xjd.TupleMax()) - (hv_xjx.TupleMin());
                hv_xjzx = (hv_xjd.TupleMin()) - (hv_xjx.TupleMax());
                hv_ljtd = (hv_ljd.TupleConcat(0)) - ((new HTuple(0)).TupleConcat(hv_ljd));
                hv_ljtx = (hv_ljx.TupleConcat(0)) - ((new HTuple(0)).TupleConcat(hv_ljx));
                HOperatorSet.TupleLength(hv_ljtd, out hv_Length3);
                HOperatorSet.TupleLength(hv_ljtx, out hv_Length4);
                hv_ljtd = hv_ljtd.TupleSelectRange(1, hv_Length3 - 2);
                hv_ljtx = hv_ljtx.TupleSelectRange(1, hv_Length4 - 2);
                hv_ljdx = hv_ljtd.TupleMax();
                hv_ljdn = hv_ljtd.TupleMin();
                hv_ljxx = hv_ljtx.TupleMax();
                hv_ljxn = hv_ljtx.TupleMin();
                hv_ljdm = hv_ljtd.TupleMean();
                hv_ljxm = hv_ljtx.TupleMean();
                HOperatorSet.TupleAtan((hv_djxr - hv_djdr) / (hv_djdc - hv_djxc), out hv_djiaodu);
                HOperatorSet.TupleAtan((hv_xjxr - hv_xjdr) / (hv_xjdc - hv_xjxc), out hv_xjiaodu);
                HOperatorSet.TupleLength(hv_djiaodu, out hv_Length5);
                HOperatorSet.TupleLength(hv_xjiaodu, out hv_Length6);
                hv_djiaodu1 = ((hv_djiaodu - hv_zcj)).TupleAbs();
                hv_xjiaodu1 = ((hv_xjiaodu - hv_zcj)).TupleAbs();
                HTuple end_val177  = hv_Length5 - 1;
                HTuple step_val177 = 1;
                //t6 = DateTime.Now;
                for (hv_i = 0; hv_i.Continue(end_val177, step_val177); hv_i = hv_i.TupleAdd(step_val177))
                {
                    while ((int)(new HTuple(hv_djiaodu1.TupleGreater((new HTuple(90)).TupleRad()
                                                                     ))) != 0)
                    {
                        hv_djiaodu1 = hv_djiaodu1 - ((new HTuple(90)).TupleRad());
                    }
                    if ((int)(new HTuple(hv_djiaodu1.TupleGreater((new HTuple(45)).TupleRad()
                                                                  ))) != 0)
                    {
                        hv_djiaodu1 = (-hv_djiaodu1) + ((new HTuple(90)).TupleRad());
                    }
                }
                HTuple end_val185  = hv_Length6 - 1;
                HTuple step_val185 = 1;
                for (hv_i = 0; hv_i.Continue(end_val185, step_val185); hv_i = hv_i.TupleAdd(step_val185))
                {
                    while ((int)(new HTuple(hv_xjiaodu1.TupleGreater((new HTuple(90)).TupleRad()
                                                                     ))) != 0)
                    {
                        hv_xjiaodu1 = hv_xjiaodu1 - ((new HTuple(90)).TupleRad());
                    }
                    if ((int)(new HTuple(hv_xjiaodu1.TupleGreater((new HTuple(45)).TupleRad()
                                                                  ))) != 0)
                    {
                        hv_xjiaodu1 = (-hv_xjiaodu1) + ((new HTuple(90)).TupleRad());
                    }
                }
                hv_jjdd = hv_djiaodu1.TupleMax().TupleDeg();
                hv_jjdx = hv_djiaodu1.TupleMin().TupleDeg();
                hv_jjxd = hv_xjiaodu1.TupleMax().TupleDeg();
                hv_jjxx = hv_xjiaodu1.TupleMin().TupleDeg();
                hv_jjdm = hv_djiaodu1.TupleMean().TupleDeg();
                hv_jjxm = hv_xjiaodu1.TupleMean().TupleDeg();
                ho_RegionLines.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines, hv_djdr, hv_djdc, hv_djxr, hv_djxc);
                HOperatorSet.Union2(ho_RegionLines, ho_RegionLines, out RegionToDisp);
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("螺纹条数1");
                hv_result = hv_result.TupleConcat(hv_l1);
                hv_result = hv_result.TupleConcat("螺纹条数2");
                hv_result = hv_result.TupleConcat(hv_l2);
                hv_result = hv_result.TupleConcat("大径最大值");
                hv_result = hv_result.TupleConcat(hv_djx.D * pixeldist);
                hv_result = hv_result.TupleConcat("大径最小值");
                hv_result = hv_result.TupleConcat(hv_djn.D * pixeldist);
                hv_result = hv_result.TupleConcat("大径平均值");
                hv_result = hv_result.TupleConcat(hv_djm.D * pixeldist);
                hv_result = hv_result.TupleConcat("小径最大值");
                hv_result = hv_result.TupleConcat(hv_xjx.D * pixeldist);
                hv_result = hv_result.TupleConcat("小径最小值");
                hv_result = hv_result.TupleConcat(hv_xjn.D * pixeldist);
                hv_result = hv_result.TupleConcat("小径平均值");
                hv_result = hv_result.TupleConcat(hv_xjm.D * pixeldist);
                hv_result = hv_result.TupleConcat("整体大径最大值");
                hv_result = hv_result.TupleConcat(hv_djzd.D * pixeldist);
                hv_result = hv_result.TupleConcat("整体大径最小值");
                hv_result = hv_result.TupleConcat(hv_djzx.D * pixeldist);
                hv_result = hv_result.TupleConcat("整体小径最大值");
                hv_result = hv_result.TupleConcat(hv_xjzd.D * pixeldist);
                hv_result = hv_result.TupleConcat("整体小径最小值");
                hv_result = hv_result.TupleConcat(hv_xjzx.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺距最大值");
                hv_result = hv_result.TupleConcat(hv_ljdx.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺距最小值");
                hv_result = hv_result.TupleConcat(hv_ljdn.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺距平均值");
                hv_result = hv_result.TupleConcat(hv_ljdm.D * pixeldist);
                hv_result = hv_result.TupleConcat("角度最大值");
                hv_result = hv_result.TupleConcat(hv_jjdd.D);
                hv_result = hv_result.TupleConcat("角度最大值");
                hv_result = hv_result.TupleConcat(hv_jjdx.D);
                hv_result = hv_result.TupleConcat("角度平均值");
                hv_result = hv_result.TupleConcat(hv_jjdm.D);
                result    = hv_result.Clone();

                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Border.Dispose();
                ho_Circle.Dispose();
                ho_ImageReduced2.Dispose();
                ho_Region.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Border1.Dispose();
                ho_SelectedContours.Dispose();
                imagexz.Dispose();
                retxz.Dispose();
                ho_RegionLines.Dispose();
                algorithm.Region.Dispose();
                //t4 = DateTime.Now;
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("螺纹条数1");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹条数2");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("大径最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("大径最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("大径平均值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("小径最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("小径最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("小径平均值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("整体大径最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("整体大径最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("整体小径最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("整体小径最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺距最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺距最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺距平均值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("角度最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("角度最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("角度平均值");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Border.Dispose();
                ho_Circle.Dispose();
                ho_ImageReduced2.Dispose();
                ho_Region.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Border1.Dispose();
                ho_SelectedContours.Dispose();
                imagexz.Dispose();
                retxz.Dispose();
                ho_RegionLines.Dispose();
                algorithm.Region.Dispose();
            }
        }
Exemplo n.º 22
0
        public static void scale_image_range(HObject ho_Image, out HObject ho_ImageScaled, HTuple hv_Min, HTuple hv_Max)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_ImageSelected = null, ho_SelectedChannel = null;
            HObject ho_LowerRegion = null, ho_UpperRegion = null, ho_ImageSelectedScaled = null;

            // Local copy input parameter variables
            HObject ho_Image_COPY_INP_TMP;

            ho_Image_COPY_INP_TMP = ho_Image.CopyObj(1, -1);


            HTuple hv_LowerLimit = new HTuple(), hv_UpperLimit = new HTuple();
            HTuple hv_Mult = null, hv_Add = null, hv_NumImages = null;
            HTuple hv_ImageIndex = null, hv_Channels = new HTuple();
            HTuple hv_ChannelIndex = new HTuple(), hv_MinGray = new HTuple();
            HTuple hv_MaxGray = new HTuple(), hv_Range = new HTuple();
            HTuple hv_Max_COPY_INP_TMP = hv_Max.Clone();
            HTuple hv_Min_COPY_INP_TMP = hv_Min.Clone();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            HOperatorSet.GenEmptyObj(out ho_ImageSelected);
            HOperatorSet.GenEmptyObj(out ho_SelectedChannel);
            HOperatorSet.GenEmptyObj(out ho_LowerRegion);
            HOperatorSet.GenEmptyObj(out ho_UpperRegion);
            HOperatorSet.GenEmptyObj(out ho_ImageSelectedScaled);
            try
            {
                if ((int)(new HTuple((new HTuple(hv_Min_COPY_INP_TMP.TupleLength())).TupleEqual(
                                         2))) != 0)
                {
                    hv_LowerLimit       = hv_Min_COPY_INP_TMP.TupleSelect(1);
                    hv_Min_COPY_INP_TMP = hv_Min_COPY_INP_TMP.TupleSelect(0);
                }
                else
                {
                    hv_LowerLimit = 0.0;
                }
                if ((int)(new HTuple((new HTuple(hv_Max_COPY_INP_TMP.TupleLength())).TupleEqual(
                                         2))) != 0)
                {
                    hv_UpperLimit       = hv_Max_COPY_INP_TMP.TupleSelect(1);
                    hv_Max_COPY_INP_TMP = hv_Max_COPY_INP_TMP.TupleSelect(0);
                }
                else
                {
                    hv_UpperLimit = 255.0;
                }
                //
                //Calculate scaling parameters.
                hv_Mult = (((hv_UpperLimit - hv_LowerLimit)).TupleReal()) / (hv_Max_COPY_INP_TMP - hv_Min_COPY_INP_TMP);
                hv_Add  = ((-hv_Mult) * hv_Min_COPY_INP_TMP) + hv_LowerLimit;
                //
                //Scale image.
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.ScaleImage(ho_Image_COPY_INP_TMP, out ExpTmpOutVar_0, hv_Mult,
                                            hv_Add);
                    ho_Image_COPY_INP_TMP.Dispose();
                    ho_Image_COPY_INP_TMP = ExpTmpOutVar_0;
                }
                //
                //Clip gray values if necessary.
                //This must be done for each image and channel separately.
                ho_ImageScaled.Dispose();
                HOperatorSet.GenEmptyObj(out ho_ImageScaled);
                HOperatorSet.CountObj(ho_Image_COPY_INP_TMP, out hv_NumImages);
                HTuple end_val49  = hv_NumImages;
                HTuple step_val49 = 1;
                for (hv_ImageIndex = 1; hv_ImageIndex.Continue(end_val49, step_val49); hv_ImageIndex = hv_ImageIndex.TupleAdd(step_val49))
                {
                    ho_ImageSelected.Dispose();
                    HOperatorSet.SelectObj(ho_Image_COPY_INP_TMP, out ho_ImageSelected, hv_ImageIndex);
                    HOperatorSet.CountChannels(ho_ImageSelected, out hv_Channels);
                    HTuple end_val52  = hv_Channels;
                    HTuple step_val52 = 1;
                    for (hv_ChannelIndex = 1; hv_ChannelIndex.Continue(end_val52, step_val52); hv_ChannelIndex = hv_ChannelIndex.TupleAdd(step_val52))
                    {
                        ho_SelectedChannel.Dispose();
                        HOperatorSet.AccessChannel(ho_ImageSelected, out ho_SelectedChannel, hv_ChannelIndex);
                        HOperatorSet.MinMaxGray(ho_SelectedChannel, ho_SelectedChannel, 0, out hv_MinGray,
                                                out hv_MaxGray, out hv_Range);
                        ho_LowerRegion.Dispose();
                        HOperatorSet.Threshold(ho_SelectedChannel, out ho_LowerRegion, ((hv_MinGray.TupleConcat(
                                                                                             hv_LowerLimit))).TupleMin(), hv_LowerLimit);
                        ho_UpperRegion.Dispose();
                        HOperatorSet.Threshold(ho_SelectedChannel, out ho_UpperRegion, hv_UpperLimit,
                                               ((hv_UpperLimit.TupleConcat(hv_MaxGray))).TupleMax());
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.PaintRegion(ho_LowerRegion, ho_SelectedChannel, out ExpTmpOutVar_0,
                                                     hv_LowerLimit, "fill");
                            ho_SelectedChannel.Dispose();
                            ho_SelectedChannel = ExpTmpOutVar_0;
                        }
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.PaintRegion(ho_UpperRegion, ho_SelectedChannel, out ExpTmpOutVar_0,
                                                     hv_UpperLimit, "fill");
                            ho_SelectedChannel.Dispose();
                            ho_SelectedChannel = ExpTmpOutVar_0;
                        }
                        if ((int)(new HTuple(hv_ChannelIndex.TupleEqual(1))) != 0)
                        {
                            ho_ImageSelectedScaled.Dispose();
                            HOperatorSet.CopyObj(ho_SelectedChannel, out ho_ImageSelectedScaled,
                                                 1, 1);
                        }
                        else
                        {
                            {
                                HObject ExpTmpOutVar_0;
                                HOperatorSet.AppendChannel(ho_ImageSelectedScaled, ho_SelectedChannel,
                                                           out ExpTmpOutVar_0);
                                ho_ImageSelectedScaled.Dispose();
                                ho_ImageSelectedScaled = ExpTmpOutVar_0;
                            }
                        }
                    }
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.ConcatObj(ho_ImageScaled, ho_ImageSelectedScaled, out ExpTmpOutVar_0
                                               );
                        ho_ImageScaled.Dispose();
                        ho_ImageScaled = ExpTmpOutVar_0;
                    }
                }
                ho_Image_COPY_INP_TMP.Dispose();
                ho_ImageSelected.Dispose();
                ho_SelectedChannel.Dispose();
                ho_LowerRegion.Dispose();
                ho_UpperRegion.Dispose();
                ho_ImageSelectedScaled.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Image_COPY_INP_TMP.Dispose();
                ho_ImageSelected.Dispose();
                ho_SelectedChannel.Dispose();
                ho_LowerRegion.Dispose();
                ho_UpperRegion.Dispose();
                ho_ImageSelectedScaled.Dispose();

                throw HDevExpDefaultException;
            }
        }
Exemplo n.º 23
0
        private void btn_ReadCharacter_Click(object sender, EventArgs e)
        {
            if (m_ocrhandle == null)
            {
                MessageBox.Show("请先读取字符库");
                return;
            }
            if (hDisplay1.GetSearchRegions().Count == 0)
            {
                MessageBox.Show("请先添加搜索区域");
                return;
            }
            HTuple  hv_Number;
            HObject _roi              = hDisplay1.GetSearchRegions().ElementAt(0);
            HObject _region           = new HObject();
            HObject _imagereduced     = new HObject();
            HObject ho_ObjectSelected = new HObject();

            HOperatorSet.ReduceDomain(m_image, _roi, out _imagereduced);
            HOperatorSet.Threshold(_imagereduced, out _region, m_OcrParam.GrayMin, m_OcrParam.GrayMax);
            HOperatorSet.Connection(_region, out _region);
            HOperatorSet.SelectShape(_region, out _region, "area", "and", m_OcrParam.AreaMin, m_OcrParam.AreaMax);

            HOperatorSet.CountObj(_region, out hv_Number);
            HTuple                 hv_Index     = new HTuple();
            List <RegionX>         _listregionx = new List <RegionX>();
            List <ViewROI.StringX> _liststringx = new List <ViewROI.StringX>();

            for (hv_Index = 1; hv_Index.Continue(hv_Number, 1); hv_Index = hv_Index.TupleAdd(1))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(_region, out ho_ObjectSelected, hv_Index);
                RegionX _hregion = new RegionX(ho_ObjectSelected.CopyObj(1, -1), "green");
                _listregionx.Add(_hregion);
            }



            HTuple _characters = new HTuple();
            HTuple _confidence = new HTuple();

            // DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, _listregionx, null);
            HOperatorSet.DoOcrMultiClassMlp(_region, m_image, m_ocrhandle, out _characters, out _confidence);


            HTuple _row    = new HTuple();
            HTuple _column = new HTuple();
            HTuple _area   = new HTuple();

            for (hv_Index = 1; hv_Index.Continue(hv_Number, 1); hv_Index = hv_Index.TupleAdd(1))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(_region, out ho_ObjectSelected, hv_Index);
                HOperatorSet.AreaCenter(ho_ObjectSelected, out _area, out _row, out _column);
                ViewROI.StringX sx = new ViewROI.StringX(19, true, false);
                sx.SetString(_characters[hv_Index - 1], (int)_row.D + 50, (int)_column.D, Color.Green);
                _liststringx.Add(sx);
            }
            DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, _listregionx, _liststringx);

            List <OcrResult> ocrresultlist = new List <OcrResult>();

            for (int i = 0; i < _characters.TupleLength(); i++)
            {
                OcrResult _ocrresult = new OcrResult();
                _ocrresult.character = _characters[i];
                _ocrresult.scale     = _confidence[i].D.ToString("F4");
                ocrresultlist.Add(_ocrresult);
            }
            dGV_CharacterResult.DataSource = ocrresultlist;
        }
Exemplo n.º 24
0
        public static void dev_display_shape_matching_results(HTuple hv_WindowHandle, HTuple hv_ModelID, HTuple hv_Color, HTuple hv_Row, HTuple hv_Column, HTuple hv_Angle, HTuple hv_ScaleR, HTuple hv_ScaleC, HTuple hv_Model)
        {
            if (hv_WindowHandle == null)
            {
                return;
            }
            // Local iconic variables
            HObject ho_ModelContours = null, ho_ContoursAffinTrans = null;
            // Local control variables
            HTuple hv_NumMatches, hv_Index = new HTuple();
            HTuple hv_Match = new HTuple(), hv_HomMat2DIdentity = new HTuple();
            HTuple hv_HomMat2DScale = new HTuple(), hv_HomMat2DRotate = new HTuple();
            HTuple hv_HomMat2DTranslate = new HTuple();

            HTuple hv_Model_COPY_INP_TMP  = hv_Model.Clone();
            HTuple hv_ScaleC_COPY_INP_TMP = hv_ScaleC.Clone();
            HTuple hv_ScaleR_COPY_INP_TMP = hv_ScaleR.Clone();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ModelContours);
            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);

            try
            {
                //This procedure displays the results of Shape-Based Matching.
                //
                hv_NumMatches = new HTuple(hv_Row.TupleLength());
                if ((int)(new HTuple(hv_NumMatches.TupleGreater(0))) != 0)
                {
                    if ((int)(new HTuple((new HTuple(hv_ScaleR_COPY_INP_TMP.TupleLength())).TupleEqual(
                                             1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleR_COPY_INP_TMP, out hv_ScaleR_COPY_INP_TMP);
                    }
                    if ((int)(new HTuple((new HTuple(hv_ScaleC_COPY_INP_TMP.TupleLength())).TupleEqual(
                                             1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleC_COPY_INP_TMP, out hv_ScaleC_COPY_INP_TMP);
                    }
                    if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength())).TupleEqual(
                                             0))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, 0, out hv_Model_COPY_INP_TMP);
                    }
                    else if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength()
                                                          )).TupleEqual(1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_Model_COPY_INP_TMP, out hv_Model_COPY_INP_TMP);
                    }
                    for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ModelID.TupleLength()
                                                                          )) - 1); hv_Index = (int)hv_Index + 1)
                    {
                        ho_ModelContours.Dispose();
                        HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID.TupleSelect(
                                                               hv_Index), 1);
                        if (hv_WindowHandle > 0)
                        {
                            HOperatorSet.SetColor(hv_WindowHandle, hv_Color.TupleSelect(
                                                      hv_Index % (new HTuple(hv_Color.TupleLength()))));
                        }
                        for (hv_Match = 0; hv_Match.Continue(hv_NumMatches - 1, 1); hv_Match = hv_Match.TupleAdd(1))
                        {
                            if ((int)(new HTuple(hv_Index.TupleEqual(hv_Model_COPY_INP_TMP.TupleSelect(
                                                                         hv_Match)))) != 0)
                            {
                                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                                HOperatorSet.HomMat2dScale(hv_HomMat2DIdentity, hv_ScaleR_COPY_INP_TMP.TupleSelect(
                                                               hv_Match), hv_ScaleC_COPY_INP_TMP.TupleSelect(hv_Match), 0, 0,
                                                           out hv_HomMat2DScale);
                                HOperatorSet.HomMat2dRotate(hv_HomMat2DScale, hv_Angle.TupleSelect(
                                                                hv_Match), 0, 0, out hv_HomMat2DRotate);
                                HOperatorSet.HomMat2dTranslate(hv_HomMat2DRotate, hv_Row.TupleSelect(
                                                                   hv_Match), hv_Column.TupleSelect(hv_Match), out hv_HomMat2DTranslate);
                                ho_ContoursAffinTrans.Dispose();
                                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ContoursAffinTrans,
                                                                   hv_HomMat2DTranslate);
                                HOperatorSet.DispObj(ho_ContoursAffinTrans, hv_WindowHandle);
                            }
                        }
                    }
                }
                ho_ModelContours.Dispose();
                ho_ContoursAffinTrans.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_ModelContours.Dispose();
                ho_ContoursAffinTrans.Dispose();

                throw HDevExpDefaultException;
            }
        }
Exemplo n.º 25
0
        public static void CreateFeatureModel(HObject ho_Image, HTuple hv_Rows, HTuple hv_Columns,
                                              HTuple hv_NumLevels, HTuple hv_AngleStart, HTuple hv_AngleExtent, HTuple hv_AngleStep,
                                              HTuple hv_ScaleMin, HTuple hv_ScaleMax, HTuple hv_MinContrast, HTuple hv_MaxContrast,
                                              HTuple hv_MinLength, out HTuple hv_ModelId, out HTuple hv_RowSource, out HTuple hv_ColumnSource,
                                              out HTuple hv_AngleSource, out HTuple hv_XldRows, out HTuple hv_XldCols, out HTuple hv_XldPointCounts)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables
            HObject ho_Region, ho_TemplateImage, ho_ModelContours;
            HObject ho_ObjectSelected = null;

            // Local control variables
            HTuple hv_AngleExtentOut = null, hv_AngleStartOut = null;
            HTuple hv_AngleStepOut = null, hv_Scale = null, hv_Score = null;
            HTuple hv_HomMat2D = null, hv_Num = null, hv_Index = null;
            HTuple hv_Cols              = new HTuple();
            HTuple hv_Rows_COPY_INP_TMP = hv_Rows.Clone();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_TemplateImage);
            HOperatorSet.GenEmptyObj(out ho_ModelContours);
            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();

            ho_Region.Dispose();
            HOperatorSet.GenRegionPolygonFilled(out ho_Region, hv_Rows_COPY_INP_TMP, hv_Columns);
            ho_TemplateImage.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_Region, out ho_TemplateImage);
            HOperatorSet.SetSystem("border_shape_models", "false");

            HOperatorSet.CreateScaledShapeModel(ho_TemplateImage, hv_NumLevels, hv_AngleStartOut,
                                                hv_AngleExtentOut, hv_AngleStepOut, hv_ScaleMin, hv_ScaleMax, 0.0121, (new HTuple("none")).TupleConcat(
                                                    "no_pregeneration"), "use_polarity", ((hv_MinContrast.TupleConcat(hv_MaxContrast))).TupleConcat(
                                                    hv_MinLength), 3, out hv_ModelId);
            HOperatorSet.FindScaledShapeModel(ho_TemplateImage, hv_ModelId, hv_AngleStartOut,
                                              hv_AngleExtentOut, hv_ScaleMin, hv_ScaleMax, 0.5, 1, 0.5, "least_squares",
                                              hv_NumLevels, 0.9, out hv_RowSource, out hv_ColumnSource, out hv_AngleSource,
                                              out hv_Scale, out hv_Score);

            ho_ModelContours.Dispose();
            HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelId, 1);
            HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RowSource, hv_ColumnSource, hv_AngleSource,
                                            out hv_HomMat2D);
            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ExpTmpOutVar_0, hv_HomMat2D);
                ho_ModelContours.Dispose();
                ho_ModelContours = ExpTmpOutVar_0;
            }
            HOperatorSet.CountObj(ho_ModelContours, out hv_Num);

            hv_XldRows        = new HTuple();
            hv_XldCols        = new HTuple();
            hv_XldPointCounts = new HTuple();

            HTuple end_val23  = hv_Num;
            HTuple step_val23 = 1;

            for (hv_Index = 1; hv_Index.Continue(end_val23, step_val23); hv_Index = hv_Index.TupleAdd(step_val23))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_ModelContours, out ho_ObjectSelected, hv_Index);
                HOperatorSet.GetContourXld(ho_ObjectSelected, out hv_Rows_COPY_INP_TMP, out hv_Cols);
                hv_XldRows        = hv_XldRows.TupleConcat(hv_Rows_COPY_INP_TMP);
                hv_XldCols        = hv_XldCols.TupleConcat(hv_Cols);
                hv_XldPointCounts = hv_XldPointCounts.TupleConcat(new HTuple(hv_Rows_COPY_INP_TMP.TupleLength()));
            }


            ho_Region.Dispose();
            ho_TemplateImage.Dispose();
            ho_ModelContours.Dispose();
            ho_ObjectSelected.Dispose();

            return;
        }
Exemplo n.º 26
0
        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;
        }
Exemplo n.º 27
0
        public MeasureResult Action()
        {
            #region 輸出結果
            CircleResult mResult = null;
            #endregion

            // Local iconic variables

            HObject ho_R17_Circle = null;
            HObject ho_R17_ROI_Image = null, ho_R17_Region = null, ho_R17_ImageReduced = null;
            HObject ho_R17_Edges = null, ho_R17_ContoursSplit = null, ho_R17_SingleSegment = null;
            HObject ho_R17_ContEllipse = null;

            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_R17_Row, hv_STD_R17_Col;
            HTuple hv_STD_R17_V_Row, hv_STD_R17_V_Col, hv_R17_X, hv_R17_Y;
            HTuple hv_R17_Pos_Row, hv_R17_Pos_Col, hv_R17_R;
            HTuple hv_alpha = new HTuple(), hv_R17_low = new HTuple();
            HTuple hv_R17_high = new HTuple(), hv_R17_NumSegments = new HTuple();
            HTuple hv_NumCircles = new HTuple(), hv_Num_Circle_Point = new HTuple();
            HTuple hv_R17 = new HTuple(), hv_i = new HTuple(), hv_Attrib = new HTuple();
            HTuple hv_R17_Row = new HTuple(), hv_R17_Column = new HTuple();
            HTuple hv_R17_Radius = new HTuple(), hv_R17_StartPhi = new HTuple();
            HTuple hv_R17_EndPhi = new HTuple(), hv_R17_PointOrder = new HTuple();
            HTuple hv_R17_MinDist = new HTuple(), hv_R17_MaxDist = new HTuple();
            HTuple hv_R17_AvgDist = new HTuple(), hv_R17_SigmaDist = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_R17_Circle);
            HOperatorSet.GenEmptyObj(out ho_R17_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R17_Region);
            HOperatorSet.GenEmptyObj(out ho_R17_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R17_Edges);
            HOperatorSet.GenEmptyObj(out ho_R17_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R17_SingleSegment);
            HOperatorSet.GenEmptyObj(out ho_R17_ContEllipse);

            //Measure: SDMS_R17
            //Author: John Hsieh
            //Date: 2012
            // dev_update_off(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;

            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****Display
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            //*****R17
            //STD R17_ 位置
            hv_STD_R17_Row = 802;
            hv_STD_R17_Col = 660;

            //STD 向量 STD_R17_
            hv_STD_R17_V_Row = hv_STD_R17_Row - hv_STD_Row;
            hv_STD_R17_V_Col = hv_STD_R17_Col - hv_STD_Col;

            //R17_X, R17_Y 分量
            hv_R17_X = (hv_STD_R17_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R17_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                ));
            hv_R17_Y = (hv_STD_R17_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R17_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                ));

            //目前圖形 R17_ 位置
            hv_R17_Pos_Row = (hv_STD_Row + hv_R17_Y) + hv_OffsetRow;
            hv_R17_Pos_Col = (hv_STD_Col + hv_R17_X) + hv_OffsetCol;

            hv_R17_R = 17;

            ho_R17_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_R17_Circle, hv_R17_Pos_Row, hv_R17_Pos_Col, hv_R17_R);
            if (HDevWindowStack.IsOpen())
            {
                //dev_display (R17_Circle)
            }
            //stop ()

            ho_R17_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_R17_Circle, out ho_R17_ROI_Image);
            ho_R17_Region.Dispose();
            HOperatorSet.FastThreshold(ho_R17_ROI_Image, out ho_R17_Region, 100, 255, 15);
            ho_R17_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_R17_ROI_Image, ho_R17_Region, out ho_R17_ImageReduced
                );
            //stop ()
            //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
            hv_alpha = 0.9;
            hv_R17_low = 10;
            hv_R17_high = 60;
            ho_R17_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_R17_ImageReduced, out ho_R17_Edges, "sobel_fast",
                hv_alpha, hv_R17_low, hv_R17_high);
            //stop ()
            //*所有的數值越小,表示容錯範圍大,反之亦然
            ho_R17_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_R17_Edges, out ho_R17_ContoursSplit, "lines_circles",
                17, 1, 1);
            //Display the results
            //===========================================================
            HOperatorSet.CountObj(ho_R17_ContoursSplit, out hv_R17_NumSegments);
            hv_NumCircles = 0;
            hv_Num_Circle_Point = 0;
            hv_R17 = 999;
            for (hv_i = 1; hv_i.Continue(hv_R17_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
            {
                ho_R17_SingleSegment.Dispose();
                HOperatorSet.SelectObj(ho_R17_ContoursSplit, out ho_R17_SingleSegment, hv_i);
                HOperatorSet.GetContourGlobalAttribXld(ho_R17_SingleSegment, "cont_approx",
                    out hv_Attrib);
                if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                {
                    HOperatorSet.FitCircleContourXld(ho_R17_SingleSegment, "atukey", -1, 2,
                        hv_Num_Circle_Point, 5, 2, out hv_R17_Row, out hv_R17_Column, out hv_R17_Radius,
                        out hv_R17_StartPhi, out hv_R17_EndPhi, out hv_R17_PointOrder);
                    ho_R17_ContEllipse.Dispose();
                    HOperatorSet.GenEllipseContourXld(out ho_R17_ContEllipse, hv_R17_Row, hv_R17_Column,
                        0, hv_R17_Radius, hv_R17_Radius, 0, (new HTuple(360)).TupleRad(), "positive",
                        1.0);
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_R17_ContEllipse, HDevWindowStack.GetActive());
                    }
                    HOperatorSet.DistEllipseContourXld(ho_R17_SingleSegment, "algebraic", -1,
                        0, hv_R17_Row, hv_R17_Column, 0, hv_R17_Radius, hv_R17_Radius, out hv_R17_MinDist,
                        out hv_R17_MaxDist, out hv_R17_AvgDist, out hv_R17_SigmaDist);
                    hv_NumCircles = hv_NumCircles + 1;
                    if ((int)(new HTuple(hv_R17.TupleGreater(hv_R17_Radius))) != 0)
                    {
                        hv_R17 = hv_R17_Radius.Clone();
                        mResult = new CircleResult()
                        {
                            Row = new HTuple(hv_R17_Row),
                            Col = new HTuple(hv_R17_Column),
                            Radius = new HTuple(hv_R17_Radius),
                            StartPhi = new HTuple(hv_R17_StartPhi),
                            EndPhi = new HTuple(hv_R17_EndPhi),
                            PointOrder = new HTuple(hv_R17_PointOrder),
                        };
                    }
                    //stop ()
                }
            }

            ho_R17_Circle.Dispose();
            ho_R17_ROI_Image.Dispose();
            ho_R17_Region.Dispose();
            ho_R17_ImageReduced.Dispose();
            ho_R17_Edges.Dispose();
            ho_R17_ContoursSplit.Dispose();
            ho_R17_SingleSegment.Dispose();
            ho_R17_ContEllipse.Dispose();

            return mResult;
        }
Exemplo n.º 28
0
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image, ho_DerivGauss = null, ho_RegionCrossings = null;
        HObject ho_Region = null, ho_region_outer = null, ho_contour_outer = null;
        HObject ho_ContCircle = null;

        // Local control variables

        HTuple hv_AcqHandle = null, hv_side = null;
        HTuple hv_dia = null, hv_Width = new HTuple(), hv_Height = new HTuple();
        HTuple hv_HalfH = new HTuple(), hv_HalfW = new HTuple();
        HTuple hv_row_len = new HTuple(), hv_row_outer = new HTuple();
        HTuple hv_col_outer = new HTuple(), hv_Rows = new HTuple();
        HTuple hv_Cols = new HTuple(), hv_i = new HTuple(), hv_Indices = new HTuple();
        HTuple hv_Length = new HTuple(), hv_col_min = new HTuple();
        HTuple hv_indice_min = new HTuple(), hv_col_max = new HTuple();
        HTuple hv_indice_max = new HTuple(), hv_Row = new HTuple();
        HTuple hv_Col = new HTuple(), hv_Radius = new HTuple();
        HTuple hv_StartPhi = new HTuple(), hv_EndPhi = new HTuple();
        HTuple hv_PointOrder = new HTuple(), hv_TupleMax = new HTuple();
        HTuple hv_IndexMax = new HTuple(), hv_colToMax0 = new HTuple();
        HTuple hv_output = new HTuple(), hv_outputmm = new HTuple();
        HTuple hv_TupleMin = new HTuple(), hv_IndexMin = new HTuple();
        HTuple hv_colToMin0 = new HTuple(), hv_Exception = null;
        HTuple hv_MessageError = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_DerivGauss);
        HOperatorSet.GenEmptyObj(out ho_RegionCrossings);
        HOperatorSet.GenEmptyObj(out ho_Region);
        HOperatorSet.GenEmptyObj(out ho_region_outer);
        HOperatorSet.GenEmptyObj(out ho_contour_outer);
        HOperatorSet.GenEmptyObj(out ho_ContCircle);
        try
        {
            HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1,
                                          "default", -1, "false", "default", "GC3851M_CAM_4", 0, -1, out hv_AcqHandle);
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 3500.0);


            hv_side = 2;
            hv_dia  = 1;
            HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
            ho_Image.Dispose();
            HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
            try
            {
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                //* Define constants and tuples:
                hv_HalfH     = hv_Height / 2;
                hv_HalfW     = hv_Width / 2;
                hv_row_len   = hv_Height.Clone();
                hv_row_outer = new HTuple();
                hv_col_outer = new HTuple();

                //* Edge detection
                ho_DerivGauss.Dispose();
                HOperatorSet.DerivateGauss(ho_Image, out ho_DerivGauss, 1, "x");
                //* diameter 2 doesn't have a clean background => different values
                if ((int)((new HTuple(hv_dia.TupleEqual(2))).TupleOr(new HTuple(hv_dia.TupleEqual(
                                                                                    5)))) != 0)
                {
                    ho_RegionCrossings.Dispose();
                    HOperatorSet.DualThreshold(ho_DerivGauss, out ho_RegionCrossings, 20, 2,
                                               2);
                    ho_Region.Dispose();
                    HOperatorSet.Union1(ho_RegionCrossings, out ho_Region);
                }
                else
                {
                    ho_RegionCrossings.Dispose();
                    HOperatorSet.DualThreshold(ho_DerivGauss, out ho_RegionCrossings, 20, 12,
                                               2);
                    ho_Region.Dispose();
                    HOperatorSet.Union1(ho_RegionCrossings, out ho_Region);
                }

                //* Retrieve points from detected edges
                HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Cols);

                //* Side 1 => upper side closer to probe
                if ((int)(new HTuple(hv_side.TupleEqual(1))) != 0)
                {
                    //* Extract the points which define the outer or inner edge
                    if ((int)((new HTuple((new HTuple(hv_dia.TupleEqual(3))).TupleOr(new HTuple(hv_dia.TupleEqual(
                                                                                                    4))))).TupleOr(new HTuple(hv_dia.TupleEqual(5)))) != 0)
                    {
                        HTuple end_val36  = hv_row_len;
                        HTuple step_val36 = 1;
                        for (hv_i = 1; hv_i.Continue(end_val36, step_val36); hv_i = hv_i.TupleAdd(step_val36))
                        {
                            HOperatorSet.TupleFind(hv_Rows, hv_i - 1, out hv_Indices);
                            //* if none point exists in this row replace it with an incremental interpolation
                            if ((int)(new HTuple(hv_Indices.TupleEqual(-1))) != 0)
                            {
                                if (hv_row_outer == null)
                                {
                                    hv_row_outer = new HTuple();
                                }
                                hv_row_outer[hv_i - 1] = (hv_row_outer.TupleSelect(hv_i - 2)) + 1;
                                if (hv_col_outer == null)
                                {
                                    hv_col_outer = new HTuple();
                                }
                                hv_col_outer[hv_i - 1] = hv_col_outer.TupleSelect(hv_i - 2);
                                continue;
                            }
                            HOperatorSet.TupleLength(hv_Indices, out hv_Length);
                            HOperatorSet.TupleMin(hv_Cols.TupleSelect(hv_Indices), out hv_col_min);
                            HOperatorSet.TupleFind(hv_Cols.TupleSelect(hv_Indices), hv_col_min,
                                                   out hv_indice_min);
                            if (hv_row_outer == null)
                            {
                                hv_row_outer = new HTuple();
                            }
                            hv_row_outer[hv_i - 1] = hv_Rows.TupleSelect(hv_Indices.TupleSelect(hv_indice_min));
                            if (hv_col_outer == null)
                            {
                                hv_col_outer = new HTuple();
                            }
                            hv_col_outer[hv_i - 1] = hv_Cols.TupleSelect(hv_Indices.TupleSelect(hv_indice_min));
                        }
                    }
                    else if ((int)((new HTuple(hv_dia.TupleEqual(1))).TupleOr(new HTuple(hv_dia.TupleEqual(
                                                                                             2)))) != 0)
                    {
                        HTuple end_val51  = hv_row_len;
                        HTuple step_val51 = 1;
                        for (hv_i = 1; hv_i.Continue(end_val51, step_val51); hv_i = hv_i.TupleAdd(step_val51))
                        {
                            HOperatorSet.TupleFind(hv_Rows, hv_i - 1, out hv_Indices);
                            //* if none point exists in this row replace it with an incremental interpolation
                            if ((int)(new HTuple(hv_Indices.TupleEqual(-1))) != 0)
                            {
                                if (hv_row_outer == null)
                                {
                                    hv_row_outer = new HTuple();
                                }
                                hv_row_outer[hv_i - 1] = (hv_row_outer.TupleSelect(hv_i - 2)) + 1;
                                if (hv_col_outer == null)
                                {
                                    hv_col_outer = new HTuple();
                                }
                                hv_col_outer[hv_i - 1] = hv_col_outer.TupleSelect(hv_i - 2);
                                continue;
                            }
                            HOperatorSet.TupleLength(hv_Indices, out hv_Length);
                            HOperatorSet.TupleMax(hv_Cols.TupleSelect(hv_Indices), out hv_col_max);
                            HOperatorSet.TupleFind(hv_Cols.TupleSelect(hv_Indices), hv_col_max,
                                                   out hv_indice_max);
                            if (hv_row_outer == null)
                            {
                                hv_row_outer = new HTuple();
                            }
                            hv_row_outer[hv_i - 1] = hv_Rows.TupleSelect(hv_Indices.TupleSelect(hv_indice_max));
                            if (hv_col_outer == null)
                            {
                                hv_col_outer = new HTuple();
                            }
                            hv_col_outer[hv_i - 1] = hv_Cols.TupleSelect(hv_Indices.TupleSelect(hv_indice_max));
                        }
                    }

                    //* retrieve the outer or inner edge points + cirlce fitting
                    ho_region_outer.Dispose();
                    HOperatorSet.GenRegionPoints(out ho_region_outer, hv_row_outer, hv_col_outer);
                    ho_contour_outer.Dispose();
                    HOperatorSet.GenContourPolygonXld(out ho_contour_outer, hv_row_outer, hv_col_outer);
                    HOperatorSet.FitCircleContourXld(ho_contour_outer, "geotukey", -1, 0, 0,
                                                     3, 2, out hv_Row, out hv_Col, out hv_Radius, out hv_StartPhi, out hv_EndPhi,
                                                     out hv_PointOrder);
                    ho_ContCircle.Dispose();
                    HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row, hv_Col, hv_Radius,
                                                     0, 6.28318, "positive", 1);
                    HOperatorSet.GetContourXld(ho_ContCircle, out hv_Row, out hv_Col);

                    //* find the maximum of the estimated circle
                    HOperatorSet.TupleMax(hv_Col, out hv_TupleMax);
                    HOperatorSet.TupleFindFirst(hv_Col, hv_TupleMax, out hv_IndexMax);

                    //* calculate pixel and mm outputs
                    hv_colToMax0 = hv_Col.TupleSelect(hv_IndexMax);
                    hv_output    = (-hv_HalfW) + hv_colToMax0;
                    hv_outputmm  = hv_output * 0.001675;
                }

                //* Side 2 => side closer to vertical moving axis
                if ((int)(new HTuple(hv_side.TupleEqual(2))) != 0)
                {
                    //* Extract the points which define the outer or inner edge
                    if ((int)((new HTuple((new HTuple(hv_dia.TupleEqual(3))).TupleOr(new HTuple(hv_dia.TupleEqual(
                                                                                                    4))))).TupleOr(new HTuple(hv_dia.TupleEqual(5)))) != 0)
                    {
                        HTuple end_val88  = hv_row_len;
                        HTuple step_val88 = 1;
                        for (hv_i = 1; hv_i.Continue(end_val88, step_val88); hv_i = hv_i.TupleAdd(step_val88))
                        {
                            HOperatorSet.TupleFind(hv_Rows, hv_i - 1, out hv_Indices);
                            //* if none point exists in this row replace it with an incremental interpolation
                            if ((int)(new HTuple(hv_Indices.TupleEqual(-1))) != 0)
                            {
                                if (hv_row_outer == null)
                                {
                                    hv_row_outer = new HTuple();
                                }
                                hv_row_outer[hv_i - 1] = (hv_row_outer.TupleSelect(hv_i - 2)) + 1;
                                if (hv_col_outer == null)
                                {
                                    hv_col_outer = new HTuple();
                                }
                                hv_col_outer[hv_i - 1] = hv_col_outer.TupleSelect(hv_i - 2);
                                continue;
                            }
                            HOperatorSet.TupleLength(hv_Indices, out hv_Length);
                            HOperatorSet.TupleMax(hv_Cols.TupleSelect(hv_Indices), out hv_col_max);
                            HOperatorSet.TupleFind(hv_Cols.TupleSelect(hv_Indices), hv_col_max,
                                                   out hv_indice_max);
                            if (hv_row_outer == null)
                            {
                                hv_row_outer = new HTuple();
                            }
                            hv_row_outer[hv_i - 1] = hv_Rows.TupleSelect(hv_Indices.TupleSelect(hv_indice_max));
                            if (hv_col_outer == null)
                            {
                                hv_col_outer = new HTuple();
                            }
                            hv_col_outer[hv_i - 1] = hv_Cols.TupleSelect(hv_Indices.TupleSelect(hv_indice_max));
                        }
                    }
                    else if ((int)((new HTuple(hv_dia.TupleEqual(1))).TupleOr(new HTuple(hv_dia.TupleEqual(
                                                                                             2)))) != 0)
                    {
                        HTuple end_val103  = hv_row_len;
                        HTuple step_val103 = 1;
                        for (hv_i = 1; hv_i.Continue(end_val103, step_val103); hv_i = hv_i.TupleAdd(step_val103))
                        {
                            HOperatorSet.TupleFind(hv_Rows, hv_i - 1, out hv_Indices);
                            //* if none point exists in this row replace it with an incremental interpolation
                            if ((int)(new HTuple(hv_Indices.TupleEqual(-1))) != 0)
                            {
                                if (hv_row_outer == null)
                                {
                                    hv_row_outer = new HTuple();
                                }
                                hv_row_outer[hv_i - 1] = (hv_row_outer.TupleSelect(hv_i - 2)) + 1;
                                if (hv_col_outer == null)
                                {
                                    hv_col_outer = new HTuple();
                                }
                                hv_col_outer[hv_i - 1] = hv_col_outer.TupleSelect(hv_i - 2);
                                continue;
                            }
                            HOperatorSet.TupleLength(hv_Indices, out hv_Length);
                            HOperatorSet.TupleMin(hv_Cols.TupleSelect(hv_Indices), out hv_col_min);
                            HOperatorSet.TupleFind(hv_Cols.TupleSelect(hv_Indices), hv_col_min,
                                                   out hv_indice_min);
                            if (hv_row_outer == null)
                            {
                                hv_row_outer = new HTuple();
                            }
                            hv_row_outer[hv_i - 1] = hv_Rows.TupleSelect(hv_Indices.TupleSelect(hv_indice_min));
                            if (hv_col_outer == null)
                            {
                                hv_col_outer = new HTuple();
                            }
                            hv_col_outer[hv_i - 1] = hv_Cols.TupleSelect(hv_Indices.TupleSelect(hv_indice_min));
                        }
                    }

                    //* retrieve the outer or inner edge points + cirlce fitting
                    ho_region_outer.Dispose();
                    HOperatorSet.GenRegionPoints(out ho_region_outer, hv_row_outer, hv_col_outer);
                    ho_contour_outer.Dispose();
                    HOperatorSet.GenContourPolygonXld(out ho_contour_outer, hv_row_outer, hv_col_outer);
                    HOperatorSet.FitCircleContourXld(ho_contour_outer, "geotukey", -1, 0, 0,
                                                     3, 2, out hv_Row, out hv_Col, out hv_Radius, out hv_StartPhi, out hv_EndPhi,
                                                     out hv_PointOrder);
                    ho_ContCircle.Dispose();
                    HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row, hv_Col, hv_Radius,
                                                     0, 6.28318, "positive", 1);
                    HOperatorSet.GetContourXld(ho_ContCircle, out hv_Row, out hv_Col);

                    //* find the minimum of the estimated circle
                    HOperatorSet.TupleMin(hv_Col, out hv_TupleMin);
                    HOperatorSet.TupleFindFirst(hv_Col, hv_TupleMin, out hv_IndexMin);

                    //* calculate pixel and mm outputs
                    hv_colToMin0 = hv_Col.TupleSelect(hv_IndexMin);
                    hv_output    = hv_HalfW - hv_colToMin0;
                    hv_outputmm  = hv_output * 0.001675;
                }
            }
            // catch (Exception)
            catch (HalconException HDevExpDefaultException1)
            {
                HDevExpDefaultException1.ToHTuple(out hv_Exception);
                hv_MessageError = " ERROR: Not able to analize photo, move horizontal axis";
            }

            HOperatorSet.CloseFramegrabber(hv_AcqHandle);
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_Image.Dispose();
            ho_DerivGauss.Dispose();
            ho_RegionCrossings.Dispose();
            ho_Region.Dispose();
            ho_region_outer.Dispose();
            ho_contour_outer.Dispose();
            ho_ContCircle.Dispose();

            throw HDevExpDefaultException;
        }
        ho_Image.Dispose();
        ho_DerivGauss.Dispose();
        ho_RegionCrossings.Dispose();
        ho_Region.Dispose();
        ho_region_outer.Dispose();
        ho_contour_outer.Dispose();
        ho_ContCircle.Dispose();
    }
Exemplo n.º 29
0
        public void PZT_Detection_ext(HObject ho_RegionPZT_T, HObject ho_R, out HObject ho_Arrow,
                                      HTuple hv_mesureNumber, HTuple hv_angle, HTuple hv_mes_width, HTuple hv_sigma,
                                      HTuple hv_threshold, HTuple hv_dist_STD, out HTuple hv_dist_PZT, out HTuple hv_pZTOkNg,
                                      out HTuple hv_Rows1, out HTuple hv_Cols1, out HTuple hv_Rows2, out HTuple hv_Cols2)
        {
            HTuple hv_Dist = null, hv_Row = null, hv_Column = null;
            HTuple hv_Phi = null, hv_Length1 = null, hv_Length2 = null;
            HTuple hv_Inter = null, hv_RowStart = null, hv_ColStart = null;
            HTuple hv_Width = null, hv_Height = null, hv_i = null;
            HTuple hv_Row_Mes = new HTuple(), hv_Col_Mes = new HTuple();
            HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge1 = new HTuple();
            HTuple hv_ColumnEdge1 = new HTuple(), hv_Amplitude1 = new HTuple();
            HTuple hv_Distance1 = new HTuple(), hv_RowEdge2 = new HTuple();
            HTuple hv_ColumnEdge2 = new HTuple(), hv_Amplitude2 = new HTuple();
            HTuple hv_Distance2 = new HTuple(), hv_L1 = new HTuple();
            HTuple hv_L2 = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_Dist_Index = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Arrow);
            hv_pZTOkNg = new HTuple();
            hv_Rows1   = new HTuple();
            hv_Cols1   = new HTuple();
            hv_Rows2   = new HTuple();
            hv_Cols2   = new HTuple();
            hv_Dist    = new HTuple();
            ho_Arrow.Dispose();
            HOperatorSet.GenEmptyObj(out ho_Arrow);
            HOperatorSet.SmallestRectangle2(ho_RegionPZT_T, out hv_Row, out hv_Column, out hv_Phi,
                                            out hv_Length1, out hv_Length2);
            hv_Inter    = (((2 * hv_Length2) / hv_mesureNumber)).TupleFloor();
            hv_RowStart = hv_Row + ((hv_Length2 - (hv_Inter / 2)) * (((hv_angle + ((new HTuple(90)).TupleRad()
                                                                                   ))).TupleSin()));
            hv_ColStart = hv_Column - ((hv_Length2 - (hv_Inter / 2)) * (((hv_angle + ((new HTuple(90)).TupleRad()
                                                                                      ))).TupleCos()));
            HOperatorSet.GetImageSize(ho_R, out hv_Width, out hv_Height);
            HTuple end_val11  = hv_mesureNumber - 1;
            HTuple step_val11 = 1;

            for (hv_i = 0; hv_i.Continue(end_val11, step_val11); hv_i = hv_i.TupleAdd(step_val11))
            {
                hv_Row_Mes = hv_RowStart - ((hv_Inter * hv_i) * (((hv_angle + ((new HTuple(90)).TupleRad()
                                                                               ))).TupleSin()));
                hv_Col_Mes = hv_ColStart + ((hv_Inter * hv_i) * (((hv_angle + ((new HTuple(90)).TupleRad()
                                                                               ))).TupleCos()));
                HOperatorSet.GenMeasureRectangle2(hv_Row_Mes, hv_Col_Mes, hv_angle, hv_Length1,
                                                  hv_mes_width, hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
                HOperatorSet.MeasurePos(ho_R, hv_MeasureHandle, hv_sigma, hv_threshold, "positive",
                                        "first", out hv_RowEdge1, out hv_ColumnEdge1, out hv_Amplitude1, out hv_Distance1);
                HOperatorSet.MeasurePos(ho_R, hv_MeasureHandle, hv_sigma, hv_threshold, "negative",
                                        "last", out hv_RowEdge2, out hv_ColumnEdge2, out hv_Amplitude2, out hv_Distance2);
                HOperatorSet.CloseMeasure(hv_MeasureHandle);
                HOperatorSet.TupleLength(hv_RowEdge1, out hv_L1);
                HOperatorSet.TupleLength(hv_RowEdge2, out hv_L2);
                if ((int)((new HTuple(hv_L1.TupleNotEqual(1))).TupleOr(new HTuple(hv_L2.TupleNotEqual(
                                                                                      1)))) != 0)
                {
                    continue;
                }
                hv_Rows1 = hv_Rows1.TupleConcat(hv_RowEdge1);
                hv_Cols1 = hv_Cols1.TupleConcat(hv_ColumnEdge1);
                hv_Rows2 = hv_Rows2.TupleConcat(hv_RowEdge2);
                hv_Cols2 = hv_Cols2.TupleConcat(hv_ColumnEdge2);
                HOperatorSet.DistancePp(hv_RowEdge1, hv_ColumnEdge1, hv_RowEdge2, hv_ColumnEdge2,
                                        out hv_Distance);
                hv_Dist = hv_Dist.TupleConcat(hv_Distance);
            }
            ho_Arrow.Dispose();
            gen_arrow_contour_xld(out ho_Arrow, hv_Rows1, hv_Cols1, hv_Rows2, hv_Cols2, 20,
                                  20);
            HOperatorSet.TupleSortIndex(hv_Dist, out hv_Dist_Index);
            hv_dist_PZT = hv_Dist.TupleSelect(hv_Dist_Index.TupleSelectRange(1, (new HTuple(hv_Dist_Index.TupleLength()
                                                                                            )) - 2));
            hv_dist_PZT = hv_dist_PZT.TupleMean();
            if ((int)(new HTuple(hv_dist_PZT.TupleGreater(hv_dist_STD))) != 0)
            {
                hv_pZTOkNg = 1;
            }
            else
            {
                hv_pZTOkNg = 0;
            }

            return;
        }
Exemplo n.º 30
0
        /// <summary>
        /// 初始化後才能叫用 Action
        /// </summary>
        public MeasureResult Action()
        {
            #region 輸出結果, CircleResult or Distance Result
            CircleResult mResult = null;
            #endregion

            // Local iconic variables

            HObject ho_R1_Circle = null;
            HObject ho_R1_ROI_Image = null, ho_R1_Region = null, ho_R1_ImageReduced = null;
            HObject ho_R1_Edges = null, ho_R1_ContoursSplit = null, ho_R1_SingleSegment = null;
            //HObject ho_R1_ContEllipse = null;

            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            //HTuple hv_AllModelId;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_R1_Row, hv_STD_R1_Col;
            HTuple hv_STD_R1_V_Row, hv_STD_R1_V_Col, hv_R1_X, hv_R1_Y;
            HTuple hv_R1_Pos_Row, hv_R1_Pos_Col, hv_R1_R;
            HTuple hv_alpha = new HTuple(), hv_R1_low = new HTuple(), hv_R1_high = new HTuple();
            HTuple hv_R1_NumSegments = new HTuple(), hv_NumCircles = new HTuple();
            HTuple hv_Num_Circle_Point = new HTuple(), hv_R1 = new HTuple();
            HTuple hv_R1_limit = new HTuple(), hv_i = new HTuple(), hv_Attrib = new HTuple();
            HTuple hv_R1_Row = new HTuple(), hv_R1_Column = new HTuple();
            HTuple hv_R1_Radius = new HTuple(), hv_R1_StartPhi = new HTuple();
            HTuple hv_R1_EndPhi = new HTuple(), hv_R1_PointOrder = new HTuple();
            HTuple hv_R1_MinDist = new HTuple(), hv_R1_MaxDist = new HTuple();
            HTuple hv_R1_AvgDist = new HTuple(), hv_R1_SigmaDist = new HTuple();
            HTuple hv_ResultText = new HTuple(), hv_MeasureReasult;

            // Initialize local and output iconic variables
            //HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_R1_Circle);
            HOperatorSet.GenEmptyObj(out ho_R1_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R1_Region);
            HOperatorSet.GenEmptyObj(out ho_R1_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R1_Edges);
            HOperatorSet.GenEmptyObj(out ho_R1_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R1_SingleSegment);
            //HOperatorSet.GenEmptyObj(out ho_R1_ContEllipse);

            //Measure: SDMS_R1
            //Author: John Hsieh
            //Date: 2012
            //ho_Image.Dispose();
            //HOperatorSet.ReadImage(out ho_Image, "D:/Projects/Halcon/SDMS/SDMS_Measure/Images/E-1/E1-1.bmp");
            //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
            //dev_update_off ()
            // dev_update_window(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;

            //****Model All
            //HOperatorSet.ReadShapeModel("D:/Projects/Halcon/SDMS/SDMS_Measure/Model/MatchingAll.shm",
            //	out hv_AllModelId);
            //ho_AllModelContours.Dispose();
            //HOperatorSet.GetShapeModelContours(out ho_AllModelContours, hv_AllModelId, 1);
            //HOperatorSet.FindShapeModel(ho_Image, hv_AllModelId, (new HTuple(0)).TupleRad()
            //	, (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", 6, 0.75, out hv_AllModelRow,
            //	out hv_AllModelColumn, out hv_AllModelAngle, out modelScore);

            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****R1
            //STD R1_ 位置
            hv_STD_R1_Row = 589;
            hv_STD_R1_Col = 705;

            //STD 向量 STD_R1_
            hv_STD_R1_V_Row = hv_STD_R1_Row - hv_STD_Row;
            hv_STD_R1_V_Col = hv_STD_R1_Col - hv_STD_Col;

            //R1_X, R1_Y 分量
            hv_R1_X = (hv_STD_R1_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R1_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                ));
            hv_R1_Y = (hv_STD_R1_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R1_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                ));

            //目前圖形 R1_ 位置
            hv_R1_Pos_Row = (hv_STD_Row + hv_R1_Y) + hv_OffsetRow;
            hv_R1_Pos_Col = (hv_STD_Col + hv_R1_X) + hv_OffsetCol;

            hv_R1_R = 20;
            //*ROI
            ho_R1_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_R1_Circle, hv_R1_Pos_Row, hv_R1_Pos_Col, hv_R1_R);
            ho_R1_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_R1_Circle, out ho_R1_ROI_Image);
            ho_R1_Region.Dispose();
            HOperatorSet.FastThreshold(ho_R1_ROI_Image, out ho_R1_Region, 100, 255, 20);
            ho_R1_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_R1_ROI_Image, ho_R1_Region, out ho_R1_ImageReduced
                );

            //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
            hv_alpha = 0.9;
            hv_R1_low = 20;
            hv_R1_high = 40;
            ho_R1_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_R1_ImageReduced, out ho_R1_Edges, "sobel_fast",
                hv_alpha, hv_R1_low, hv_R1_high);
            //stop ()
            //*所有的數值越小,表示容錯範圍大,反之亦然
            ho_R1_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_R1_Edges, out ho_R1_ContoursSplit, "lines_circles",
                17, 1, 1);
            //Display the results
            //===========================================================
            HOperatorSet.CountObj(ho_R1_ContoursSplit, out hv_R1_NumSegments);
            hv_NumCircles = 0;
            hv_Num_Circle_Point = 5;
            hv_R1 = 999;
            hv_R1_limit = 10;
            for (hv_i = 1; hv_i.Continue(hv_R1_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
            {
                ho_R1_SingleSegment.Dispose();
                HOperatorSet.SelectObj(ho_R1_ContoursSplit, out ho_R1_SingleSegment, hv_i);
                HOperatorSet.GetContourGlobalAttribXld(ho_R1_SingleSegment, "cont_approx", out hv_Attrib);

                if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                {
                    HOperatorSet.FitCircleContourXld(ho_R1_SingleSegment, "atukey", -1, 2,
                        hv_Num_Circle_Point, 5, 2, out hv_R1_Row, out hv_R1_Column, out hv_R1_Radius,
                        out hv_R1_StartPhi, out hv_R1_EndPhi, out hv_R1_PointOrder);

                    //ho_R1_ContEllipse.Dispose();
                    //HOperatorSet.GenEllipseContourXld(out ho_R1_ContEllipse, hv_R1_Row, hv_R1_Column,
                    //	0, hv_R1_Radius, hv_R1_Radius, 0, (new HTuple(360)).TupleRad(), "positive",
                    //	1.0);

                    //HOperatorSet.DistEllipseContourXld(ho_R1_SingleSegment, "algebraic", -1,
                    //	0, hv_R1_Row, hv_R1_Column, 0, hv_R1_Radius, hv_R1_Radius, out hv_R1_MinDist,
                    //	out hv_R1_MaxDist, out hv_R1_AvgDist, out hv_R1_SigmaDist);

                    hv_NumCircles = hv_NumCircles + 1;
                    if ((int)(new HTuple(hv_R1.TupleGreater(hv_R1_Radius))) != 0)
                    {
                        hv_R1 = hv_R1_Radius.Clone();
                        //hv_ResultText = (((("C" + hv_NumCircles) + ": Radius = ") + (hv_R1_Radius.TupleString(
                        //	".3"))) + " / MaxDeviation: ") + (hv_R1_MaxDist.TupleString(".3"));
                        //HOperatorSet.SetTposition(hv_WindowHandle, hv_R1_Pos_Row - hv_msgOffsetY,
                        //	hv_R1_Pos_Col - hv_msgOffsetX);
                        //HOperatorSet.WriteString(hv_WindowHandle, "R1");

                        #region 組合結果
                        mResult = new CircleResult(new HTuple(hv_R1_Row)
                                                , new HTuple(hv_R1_Column)
                                                , new HTuple(hv_R1_Radius)
                                                , new HTuple(hv_R1_StartPhi)
                                                , new HTuple(hv_R1_EndPhi)
                                                , new HTuple(hv_R1_PointOrder)) { };
                        #endregion
                    }
                }
            }
            hv_MeasureReasult = hv_R1.Clone();
            //****R1 End
            //ho_Image.Dispose();
            ho_R1_Circle.Dispose();
            ho_R1_ROI_Image.Dispose();
            ho_R1_Region.Dispose();
            ho_R1_ImageReduced.Dispose();
            ho_R1_Edges.Dispose();
            ho_R1_ContoursSplit.Dispose();
            ho_R1_SingleSegment.Dispose();
            //ho_R1_ContEllipse.Dispose();

            return mResult;
        }
Exemplo n.º 31
0
        public void FitCicrle(HObject ho_Image, HTuple hv_WindowHandle, HTuple hv_CircleRow,
                              HTuple hv_CircleColumn, HTuple hv_CircleRadius, HTuple hv_RectRow, HTuple hv_RectColumn,
                              HTuple hv_RectPhi, HTuple hv_RectLength1, HTuple hv_RectLength2, HTuple hv_Transition,
                              HTuple hv_Sigma, HTuple hv_Threshold, out HTuple hv_CenterRow, out HTuple hv_CenterColumn,
                              out HTuple hv_CenterRaduis, out HTuple hv_StartPhi, out HTuple hv_EndPhi, out HTuple hv_Circularity)
        {
            // Local iconic variables

            HObject ho_Rectangle, ho_Contour;

            // Local control variables

            HTuple hv_Width = null, hv_Height = null, hv_sr = null;
            HTuple hv_sc = null, hv_lenth1 = null, hv_lenth2 = null;
            HTuple hv_val = null, hv_PointNum = null, hv_mr = null;
            HTuple hv_mc = null, hv_ag = null, hv_i = null, hv_lr = null;
            HTuple hv_lc = null, hv_j = null, hv_MeasureHandle = new HTuple();
            HTuple hv_RowEdge = new HTuple(), hv_ColumnEdge = new HTuple();
            HTuple hv_Amplitude = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_RowEdgeCount = new HTuple(), hv_RowEdgeVal = new HTuple();
            HTuple hv_Indices = new HTuple(), hv_RowEdgeValMin = new HTuple();
            HTuple hv_rc = null, hv_zr = null, hv_zc = null, hv_PointOrder = null;
            HTuple hv_CircleColumn_COPY_INP_TMP = hv_CircleColumn.Clone();
            HTuple hv_CircleRow_COPY_INP_TMP    = hv_CircleRow.Clone();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            hv_CenterRow    = new HTuple();
            hv_CenterColumn = new HTuple();
            hv_CenterRaduis = new HTuple();
            hv_StartPhi     = new HTuple();
            hv_EndPhi       = new HTuple();
            hv_Circularity  = new HTuple();
            try
            {
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                }
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                //
                //tuple_deg (RectPhi, Deg)
                //
                //gen_circle_contour_xld (ContCircle, CircleRow, CircleColumn, CircleRadius, 0, 6.28318/2, 'positive', 1)
                //
                hv_sr = hv_CircleRow_COPY_INP_TMP - hv_CircleRadius;
                hv_sc = hv_CircleColumn_COPY_INP_TMP.Clone();
                //
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                }
                //
                hv_lenth1 = hv_CircleRadius / 2.5;
                hv_lenth2 = hv_CircleRadius / 8;
                //lenth1 := RectLength1
                //lenth2 := RectLength2
                hv_val      = 5;
                hv_PointNum = (6.28318 * hv_CircleRadius) / ((hv_lenth2 + hv_val) * 2);
                //
                hv_mr = hv_sr.Clone();
                hv_mc = hv_sc.Clone();
                //
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_mr, hv_mc, (new HTuple(0 + 90)).TupleRad()
                                           , hv_lenth1, hv_lenth2);
                //
                hv_ag = 10;
                //
                HOperatorSet.DispImage(ho_Image, 3600);
                //tuple_rad (ag, rd)
                //
                HTuple end_val30  = 360 / hv_ag;
                HTuple step_val30 = 1;
                for (hv_i = 0; hv_i.Continue(end_val30, step_val30); hv_i = hv_i.TupleAdd(step_val30))
                {
                    hv_mr = hv_sr + (hv_CircleRadius * (1 - ((((hv_ag.TupleRad()) * hv_i)).TupleCos()
                                                             )));
                    hv_mc = hv_sc - (hv_CircleRadius * ((((hv_ag.TupleRad()) * hv_i)).TupleSin()));
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle, hv_mr, hv_mc, (((hv_ag * hv_i) + 90)).TupleRad()
                                               , hv_lenth1, hv_lenth2);
                }
                //
                //stop ()
                //
                hv_lr = new HTuple();
                hv_lc = new HTuple();
                //
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
                }
                hv_j = 0;
                if (HDevWindowStack.IsOpen())
                {
                    //dev_set_draw ('fill')
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1);
                }
                //
                HTuple end_val46  = 360 / hv_ag;
                HTuple step_val46 = 1;
                for (hv_i = 0; hv_i.Continue(end_val46, step_val46); hv_i = hv_i.TupleAdd(step_val46))
                {
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                    }
                    //
                    hv_mr = hv_sr + (hv_CircleRadius * (1 - ((((hv_ag.TupleRad()) * hv_i)).TupleCos()
                                                             )));
                    hv_mc = hv_sc - (hv_CircleRadius * ((((hv_ag.TupleRad()) * hv_i)).TupleSin()));
                    //
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle, hv_mr, hv_mc, (((hv_ag * hv_i) + 90)).TupleRad()
                                               , hv_lenth1, hv_lenth2);
                    //
                    HOperatorSet.GenMeasureRectangle2(hv_mr, hv_mc, (((hv_ag * hv_i) + 90)).TupleRad()
                                                      , hv_lenth1, hv_lenth2, hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
                    //Transition = 'positive' :dark-to-light;  Transition = 'negative': light-to-dark
                    HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, hv_Sigma, hv_Threshold,
                                            hv_Transition, "all", out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude,
                                            out hv_Distance);
                    //
                    if ((int)(new HTuple((new HTuple(hv_RowEdge.TupleLength())).TupleEqual(0))) != 0)
                    {
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                        }
                        HOperatorSet.DispCross(hv_WindowHandle, hv_mr, hv_mc, 20, (new HTuple(45)).TupleRad()
                                               );
                        //stop ()
                    }
                    else if ((int)(new HTuple((new HTuple(hv_RowEdge.TupleLength())).TupleEqual(
                                                  1))) != 0)
                    {
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                        }
                        HOperatorSet.DispCross(hv_WindowHandle, hv_RowEdge, hv_ColumnEdge, 20,
                                               (new HTuple(45)).TupleRad());
                        if (hv_lr == null)
                        {
                            hv_lr = new HTuple();
                        }
                        hv_lr[hv_j] = hv_RowEdge.TupleSelect(0);
                        if (hv_lc == null)
                        {
                            hv_lc = new HTuple();
                        }
                        hv_lc[hv_j] = hv_ColumnEdge.TupleSelect(0);
                        hv_j        = hv_j + 1;
                    }
                    else if ((int)(new HTuple((new HTuple(hv_RowEdge.TupleLength())).TupleNotEqual(
                                                  1))) != 0)
                    {
                        //By position near DrawLine
                        hv_RowEdgeCount = new HTuple(hv_RowEdge.TupleLength());
                        hv_RowEdgeVal   = hv_RowEdge - hv_mr;
                        HOperatorSet.TupleAbs(hv_RowEdgeVal, out hv_RowEdgeVal);
                        //tuple_sort_index (RowEdgeVal, Indices)
                        HOperatorSet.TupleMin(hv_RowEdgeVal, out hv_RowEdgeValMin);
                        HOperatorSet.TupleFind(hv_RowEdgeVal, hv_RowEdgeValMin, out hv_Indices);
                        if ((int)(new HTuple((new HTuple(hv_Indices.TupleLength())).TupleEqual(
                                                 1))) != 0)
                        {
                            if (hv_lr == null)
                            {
                                hv_lr = new HTuple();
                            }
                            hv_lr[hv_j] = hv_RowEdge.TupleSelect(hv_Indices);
                            if (hv_lc == null)
                            {
                                hv_lc = new HTuple();
                            }
                            hv_lc[hv_j] = hv_ColumnEdge.TupleSelect(hv_Indices);
                        }
                        else
                        {
                            if (hv_lr == null)
                            {
                                hv_lr = new HTuple();
                            }
                            hv_lr[hv_j] = hv_RowEdge.TupleSelect(hv_Indices.TupleSelect(0));
                            if (hv_lc == null)
                            {
                                hv_lc = new HTuple();
                            }
                            hv_lc[hv_j] = hv_ColumnEdge.TupleSelect(hv_Indices.TupleSelect(0));
                        }
                        hv_j = hv_j + 1;
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue");
                        }
                        HOperatorSet.DispCross(hv_WindowHandle, hv_RowEdge.TupleSelect(hv_Indices),
                                               hv_ColumnEdge.TupleSelect(hv_Indices), 20, (new HTuple(45)).TupleRad()
                                               );
                        //
                        //By Threshold
                    }
                    HOperatorSet.CloseMeasure(hv_MeasureHandle);
                }
                //
                //stop ()
                //
                hv_rc = new HTuple();
                hv_zr = new HTuple();
                hv_zc = new HTuple();
                //
                hv_CircleRow_COPY_INP_TMP    = hv_lr.Clone();
                hv_CircleColumn_COPY_INP_TMP = hv_lc.Clone();
                //
                //
                if ((int)(new HTuple((new HTuple(hv_CircleRow_COPY_INP_TMP.TupleLength())).TupleLess(
                                         4))) != 0)
                {
                    disp_message(hv_WindowHandle, "FitLine Fail", "image", 10, 10, "black", "true");
                    ho_Rectangle.Dispose();
                    ho_Contour.Dispose();

                    return;
                }
                ho_Contour.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_CircleRow_COPY_INP_TMP,
                                                  hv_CircleColumn_COPY_INP_TMP);
                //
                HOperatorSet.FitCircleContourXld(ho_Contour, "algebraic", -1, 0, 0, 3, 2, out hv_CenterRow,
                                                 out hv_CenterColumn, out hv_CenterRaduis, out hv_StartPhi, out hv_EndPhi,
                                                 out hv_PointOrder);
                HOperatorSet.CircularityXld(ho_Contour, out hv_Circularity);
                //
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue");
                }
                HOperatorSet.DispCircle(hv_WindowHandle, hv_CenterRow, hv_CenterColumn, hv_CenterRaduis);
                //
                ho_Rectangle.Dispose();
                ho_Contour.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Rectangle.Dispose();
                ho_Contour.Dispose();

                throw HDevExpDefaultException;
            }
        }
Exemplo n.º 32
0
        // Main procedure
        private void porosityHorizonatal()
        {
            // Local iconic variables
            HObject ho_Image = null, ho_Rectangle = null, ho_ImageReduced = null;
            HObject ho_ImageMean = null, ho_Region = null, ho_ConnectedRegions = null;
            HObject ho_RegionClosing = null, ho_SmallConnection = null;
            HObject ho_ContCircle = null;
            // Local control variables
            HTuple hv_AcqHandle = null, hv_found = null;
            HTuple hv_cnt = null, hv_bol = null, hv_porosity_area_px = null;
            HTuple hv_porosity_area_mm = null;
            HTuple hv_UsedThreshold = new HTuple(), hv_Circularity = new HTuple();
            HTuple hv_Area = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_Length = new HTuple(), hv_circ_min = new HTuple();
            HTuple hv_area_min = new HTuple(), hv_index = new HTuple();
            HTuple hv_i = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_SmallConnection);
            HOperatorSet.GenEmptyObj(out ho_ContCircle);
            // Wait for CAM4 thread to be closed
            _waitHandleCam3.WaitOne();
            // Close te thread DOOR
            _waitHandleCam3.Reset();
            // Open camera frame
            HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "GC2591MP_CAM_3", 0, -1, out hv_AcqHandle);
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 35000.0); // 30 000
            // Information for PLC that frame is opened
            DetectionHorStart();
            hv_found = 0;
            hv_cnt   = 0;
            hv_bol   = 0;

            Porositydetectedhor = false;
            while (Porositydetectedhor == false)
            {
                ho_Image.Dispose();
                HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
                ho_Rectangle.Dispose();
                //HOperatorSet.GenRectangle1(out ho_Rectangle, 520, 200, 1050, 700);
                //HOperatorSet.GenRectangle1(out ho_Rectangle, 1020, 200, 1460, 700);
                HOperatorSet.GenRectangle1(out ho_Rectangle, 900, 200, 1460, 700);
                //HOperatorSet.GenRectangle1(out ho_Rectangle, 1120, 200, 1630, 700);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced);
                ho_ImageMean.Dispose();
                HOperatorSet.MeanImage(ho_ImageReduced, out ho_ImageMean, 21, 21);
                ho_Region.Dispose();
                HOperatorSet.BinaryThreshold(ho_ImageMean, out ho_Region, "max_separability", "dark", out hv_UsedThreshold);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
                ho_RegionClosing.Dispose();
                HOperatorSet.ClosingCircle(ho_ConnectedRegions, out ho_RegionClosing, 17);
                ho_SmallConnection.Dispose();
                HOperatorSet.Connection(ho_RegionClosing, out ho_SmallConnection);
                HOperatorSet.Circularity(ho_SmallConnection, out hv_Circularity);
                HOperatorSet.AreaCenter(ho_SmallConnection, out hv_Area, out hv_Row, out hv_Column);
                HOperatorSet.TupleLength(hv_Row, out hv_Length);
                // Criteria for porosity
                hv_circ_min = 0.5;
                hv_area_min = 300;  // 300 - HDev //1000
                hv_index    = 0;
                HTuple end_val36  = hv_Length;
                HTuple step_val36 = 1;

                for (hv_i = 1; hv_i.Continue(end_val36, step_val36); hv_i = hv_i.TupleAdd(step_val36))
                {
                    if ((int)((new HTuple(((hv_Circularity.TupleSelect(hv_i - 1))).TupleGreater(
                                              hv_circ_min))).TupleAnd(new HTuple(((hv_Area.TupleSelect(hv_i - 1))).TupleGreater(
                                                                                     hv_area_min)))) != 0)
                    {
                        hv_index = hv_i - 1;
                        hv_found = hv_found + 1;
                        hv_bol   = 1;
                        break;
                    }
                    else
                    {
                        hv_bol = 0;
                    }
                }
                //HOperatorSet.ClearWindow(hv_porosityWinHandle);
                //HOperatorSet.DispObj(ho_Image, hv_porosityWinHandle);
                if ((int)((new HTuple(hv_found.TupleGreater(0))).TupleAnd(new HTuple(hv_bol.TupleEqual(
                                                                                         1)))) != 0)
                {
                    PorosityIsDetected();
                    ho_ContCircle.Dispose();
                    //HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row.TupleSelect(hv_index),
                    //    hv_Column.TupleSelect(hv_index), 50, 0, 6.28318, "positive", 1);
                    //HOperatorSet.DispObj(ho_ContCircle, hv_porosityWinHandle);
                    hv_found            = 0;
                    hv_cnt              = 0;
                    Porositydetectedhor = true;
                }
                hv_cnt = hv_cnt + 1;
            }

            //HOperatorSet.ClearWindow(hv_porosityWinHandle);
            HOperatorSet.CloseFramegrabber(hv_AcqHandle);
            // Open the thread DOOR
            _waitHandleCam3.Set();
            // Dispose all iconic variables
            ho_Image.Dispose();
            ho_Rectangle.Dispose();
            ho_ImageReduced.Dispose();
            ho_ImageMean.Dispose();
            ho_Region.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_RegionClosing.Dispose();
            ho_SmallConnection.Dispose();
            ho_ContCircle.Dispose();
        }
Exemplo n.º 33
0
        public MeasureResult Action()
        {
            #region 輸出結果
            CircleResult mResult = null;
            #endregion

            // Local iconic variables

            HObject ho_R19_Circle = null;
            HObject ho_R19_ROI_Image = null, ho_R19_Region = null, ho_R19_ImageReduced = null;
            HObject ho_R19_Edges = null, ho_R19_ContoursSplit = null, ho_R19_SingleSegment = null;
            HObject ho_R19_ContEllipse = null;


            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_R19_Row, hv_STD_R19_Col;
            HTuple hv_STD_R19_V_Row, hv_STD_R19_V_Col, hv_R19_X, hv_R19_Y;
            HTuple hv_R19_Pos_Row, hv_R19_Pos_Col, hv_R19_R;
            HTuple hv_alpha = new HTuple(), hv_R19_low = new HTuple();
            HTuple hv_R19_high = new HTuple(), hv_R19_NumSegments = new HTuple();
            HTuple hv_NumCircles = new HTuple(), hv_Num_Circle_Point = new HTuple();
            HTuple hv_R19 = new HTuple(), hv_i = new HTuple(), hv_Attrib = new HTuple();
            HTuple hv_R19_Row = new HTuple(), hv_R19_Column = new HTuple();
            HTuple hv_R19_Radius = new HTuple(), hv_R19_StartPhi = new HTuple();
            HTuple hv_R19_EndPhi = new HTuple(), hv_R19_PointOrder = new HTuple();
            HTuple hv_R19_MinDist = new HTuple(), hv_R19_MaxDist = new HTuple();
            HTuple hv_R19_AvgDist = new HTuple(), hv_R19_SigmaDist = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_R19_Circle);
            HOperatorSet.GenEmptyObj(out ho_R19_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R19_Region);
            HOperatorSet.GenEmptyObj(out ho_R19_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R19_Edges);
            HOperatorSet.GenEmptyObj(out ho_R19_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R19_SingleSegment);
            HOperatorSet.GenEmptyObj(out ho_R19_ContEllipse);

            //Measure: SDMS_R19
            //Author: John Hsieh
            //Date: 2012

            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;


            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****Display
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }

            //****R19
            //STD R19_ 位置
            hv_STD_R19_Row = 794;
            hv_STD_R19_Col = 620;

            //STD 向量 STD_R19_
            hv_STD_R19_V_Row = hv_STD_R19_Row - hv_STD_Row;
            hv_STD_R19_V_Col = hv_STD_R19_Col - hv_STD_Col;


            //R19_X, R19_Y 分量
            hv_R19_X = (hv_STD_R19_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R19_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                                                                                                    ));
            hv_R19_Y = (hv_STD_R19_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R19_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                                                                                                    ));


            //目前圖形 R19_ 位置
            hv_R19_Pos_Row = (hv_STD_Row + hv_R19_Y) + hv_OffsetRow;
            hv_R19_Pos_Col = (hv_STD_Col + hv_R19_X) + hv_OffsetCol;

            hv_R19_R = 15;

            ho_R19_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_R19_Circle, hv_R19_Pos_Row, hv_R19_Pos_Col, hv_R19_R);

            if (HDevWindowStack.IsOpen())
            {
                //dev_display (R19_Circle)
            }
            //stop ()
            ho_R19_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_R19_Circle, out ho_R19_ROI_Image);
            ho_R19_Region.Dispose();
            HOperatorSet.FastThreshold(ho_R19_ROI_Image, out ho_R19_Region, 100, 255, 15);
            ho_R19_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_R19_ROI_Image, ho_R19_Region, out ho_R19_ImageReduced
                                      );
            //stop ()
            //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
            hv_alpha    = 0.9;
            hv_R19_low  = 2;
            hv_R19_high = 60;
            ho_R19_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_R19_ImageReduced, out ho_R19_Edges, "sobel_fast",
                                     hv_alpha, hv_R19_low, hv_R19_high);
            //stop ()
            //*所有的數值越小,表示容錯範圍大,反之亦然
            ho_R19_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_R19_Edges, out ho_R19_ContoursSplit, "lines_circles",
                                            17, 1, 1);
            //Display the results
            //===========================================================
            HOperatorSet.CountObj(ho_R19_ContoursSplit, out hv_R19_NumSegments);
            hv_NumCircles       = 0;
            hv_Num_Circle_Point = 0;
            hv_R19 = 999;
            for (hv_i = 1; hv_i.Continue(hv_R19_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
            {
                ho_R19_SingleSegment.Dispose();
                HOperatorSet.SelectObj(ho_R19_ContoursSplit, out ho_R19_SingleSegment, hv_i);
                HOperatorSet.GetContourGlobalAttribXld(ho_R19_SingleSegment, "cont_approx",
                                                       out hv_Attrib);
                if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                {
                    HOperatorSet.FitCircleContourXld(ho_R19_SingleSegment, "atukey", -1, 2,
                                                     hv_Num_Circle_Point, 5, 2, out hv_R19_Row, out hv_R19_Column, out hv_R19_Radius,
                                                     out hv_R19_StartPhi, out hv_R19_EndPhi, out hv_R19_PointOrder);
                    ho_R19_ContEllipse.Dispose();
                    HOperatorSet.GenEllipseContourXld(out ho_R19_ContEllipse, hv_R19_Row, hv_R19_Column,
                                                      0, hv_R19_Radius, hv_R19_Radius, 0, (new HTuple(360)).TupleRad(), "positive",
                                                      1.0);
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_R19_ContEllipse, HDevWindowStack.GetActive());
                    }
                    HOperatorSet.DistEllipseContourXld(ho_R19_SingleSegment, "algebraic", -1,
                                                       0, hv_R19_Row, hv_R19_Column, 0, hv_R19_Radius, hv_R19_Radius, out hv_R19_MinDist,
                                                       out hv_R19_MaxDist, out hv_R19_AvgDist, out hv_R19_SigmaDist);
                    hv_NumCircles = hv_NumCircles + 1;
                    if ((int)(new HTuple(hv_R19.TupleGreater(hv_R19_Radius))) != 0)
                    {
                        hv_R19  = hv_R19_Radius.Clone();
                        mResult = new CircleResult()
                        {
                            Row        = new HTuple(hv_R19_Row),
                            Col        = new HTuple(hv_R19_Column),
                            Radius     = new HTuple(hv_R19_Radius),
                            StartPhi   = new HTuple(hv_R19_StartPhi),
                            EndPhi     = new HTuple(hv_R19_EndPhi),
                            PointOrder = new HTuple(hv_R19_PointOrder),
                        };
                    }
                }
            }
            ho_R19_Circle.Dispose();
            ho_R19_ROI_Image.Dispose();
            ho_R19_Region.Dispose();
            ho_R19_ImageReduced.Dispose();
            ho_R19_Edges.Dispose();
            ho_R19_ContoursSplit.Dispose();
            ho_R19_SingleSegment.Dispose();
            ho_R19_ContEllipse.Dispose();


            return(mResult);
        }
Exemplo n.º 34
0
        public MeasureResult Action()
        {
            #region 輸出結果
            CircleResult mResult = null;
            #endregion
            // Local iconic variables

            HObject ho_R6_Circle = null;
            HObject ho_R6_ROI_Image = null, ho_R6_Region = null, ho_R6_RegionBorder = null;
            HObject ho_R6_RegionDilation = null, ho_R6_ImageReduced = null;
            HObject ho_R6_Edges = null, ho_R6_ContoursSplit = null, ho_R6_SingleSegment = null;
            HObject ho_R6_ContEllipse = null;

            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_R6;
            HTuple hv_STD_R6_Row = new HTuple(), hv_STD_R6_Col = new HTuple();
            HTuple hv_STD_R6_V_Row = new HTuple(), hv_STD_R6_V_Col = new HTuple();
            HTuple hv_R6_X = new HTuple(), hv_R6_Y = new HTuple(), hv_R6_Pos_Row = new HTuple();
            HTuple hv_R6_Pos_Col = new HTuple(), hv_R6_R = new HTuple();
            HTuple hv_R6_R_Max = new HTuple(), hv_R6_R_Inc = new HTuple();
            HTuple hv_R6_NumSegments = new HTuple(), hv_NumCircles = new HTuple();
            HTuple hv_Num_Circle_Point = new HTuple(), hv_i = new HTuple();
            HTuple hv_Attrib = new HTuple(), hv_R6_Row = new HTuple();
            HTuple hv_R6_Column = new HTuple(), hv_R6_Radius = new HTuple();
            HTuple hv_R6_StartPhi = new HTuple(), hv_R6_EndPhi = new HTuple();
            HTuple hv_R6_PointOrder = new HTuple(), hv_R6_MinDist = new HTuple();
            HTuple hv_R6_MaxDist = new HTuple(), hv_R6_AvgDist = new HTuple();
            HTuple hv_R6_SigmaDist = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_R6_Circle);
            HOperatorSet.GenEmptyObj(out ho_R6_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R6_Region);
            HOperatorSet.GenEmptyObj(out ho_R6_RegionBorder);
            HOperatorSet.GenEmptyObj(out ho_R6_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_R6_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R6_Edges);
            HOperatorSet.GenEmptyObj(out ho_R6_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R6_SingleSegment);
            HOperatorSet.GenEmptyObj(out ho_R6_ContEllipse);

            //Measure: SDMS_R6
            //Author: John Hsieh
            //Date: 2012
            // dev_update_off(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;

            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****Display
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            //*****R6
            hv_R6 = 999;
            //STD R6_ 位置
            hv_STD_R6_Row = 1292;
            hv_STD_R6_Col = 639;

            //STD 向量 STD_R6_
            hv_STD_R6_V_Row = hv_STD_R6_Row - hv_STD_Row;
            hv_STD_R6_V_Col = hv_STD_R6_Col - hv_STD_Col;

            //R6_X, R6_Y 分量
            hv_R6_X = (hv_STD_R6_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R6_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                ));
            hv_R6_Y = (hv_STD_R6_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R6_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                ));

            //目前圖形 R4 位置
            hv_R6_Pos_Row = (hv_STD_Row + hv_R6_Y) + hv_OffsetRow;
            hv_R6_Pos_Col = (hv_STD_Col + hv_R6_X) + hv_OffsetCol;

            //R6_Region 由半徑 29 開始搜尋, 最大搜尋至 41, Inc =2
            hv_R6_R = 29;
            hv_R6_R_Max = 41;
            hv_R6_R_Inc = 2;
            while ((int)((new HTuple(hv_R6.TupleEqual(999))).TupleAnd(new HTuple(hv_R6_R.TupleLess(
                hv_R6_R_Max)))) != 0)
            {
                //*******************************
                ho_R6_Circle.Dispose();
                HOperatorSet.GenCircle(out ho_R6_Circle, hv_R6_Pos_Row, hv_R6_Pos_Col, hv_R6_R);
                ho_R6_ROI_Image.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_R6_Circle, out ho_R6_ROI_Image);
                ho_R6_Region.Dispose();
                HOperatorSet.Threshold(ho_R6_ROI_Image, out ho_R6_Region, 100, 255);
                ho_R6_RegionBorder.Dispose();
                HOperatorSet.Boundary(ho_R6_Region, out ho_R6_RegionBorder, "inner");
                ho_R6_RegionDilation.Dispose();
                HOperatorSet.DilationCircle(ho_R6_RegionBorder, out ho_R6_RegionDilation,
                    1.5);
                ho_R6_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_R6_ROI_Image, ho_R6_RegionDilation, out ho_R6_ImageReduced
                    );
                ho_R6_Edges.Dispose();
                HOperatorSet.EdgesSubPix(ho_R6_ImageReduced, out ho_R6_Edges, "lanser2",
                    0.3, 40, 90);

                //fast_threshold (R6_ROI_Image, R6_Region, 80, 255, 15)
                //reduce_domain (R6_ROI_Image, R6_Region, R6_ImageReduced)
                //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
                //alpha := 10
                //R6_low := 10
                //R6_high := 60
                //edges_sub_pix (R6_ImageReduced, R6_Edges, 'sobel_fast', alpha, R6_low, R6_high)
                //stop ()
                //*所有的數值越小,表示容錯範圍大,反之亦然
                ho_R6_ContoursSplit.Dispose();
                HOperatorSet.SegmentContoursXld(ho_R6_Edges, out ho_R6_ContoursSplit, "lines_circles",
                    6, 4, 4);

                //Display the results
                //===========================================================
                HOperatorSet.CountObj(ho_R6_ContoursSplit, out hv_R6_NumSegments);
                hv_NumCircles = 0;
                hv_Num_Circle_Point = 0;
                for (hv_i = 1; hv_i.Continue(hv_R6_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
                {
                    ho_R6_SingleSegment.Dispose();
                    HOperatorSet.SelectObj(ho_R6_ContoursSplit, out ho_R6_SingleSegment, hv_i);
                    HOperatorSet.GetContourGlobalAttribXld(ho_R6_SingleSegment, "cont_approx",
                        out hv_Attrib);
                    if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                    {
                        HOperatorSet.FitCircleContourXld(ho_R6_SingleSegment, "atukey", -1, 2,
                            hv_Num_Circle_Point, 5, 2, out hv_R6_Row, out hv_R6_Column, out hv_R6_Radius,
                            out hv_R6_StartPhi, out hv_R6_EndPhi, out hv_R6_PointOrder);
                        ho_R6_ContEllipse.Dispose();
                        HOperatorSet.GenEllipseContourXld(out ho_R6_ContEllipse, hv_R6_Row, hv_R6_Column,
                            0, hv_R6_Radius, hv_R6_Radius, 0, (new HTuple(360)).TupleRad(), "positive",
                            1.0);
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.DispObj(ho_R6_ContEllipse, HDevWindowStack.GetActive()
                                );
                        }
                        HOperatorSet.DistEllipseContourXld(ho_R6_SingleSegment, "algebraic",
                            -1, 0, hv_R6_Row, hv_R6_Column, 0, hv_R6_Radius, hv_R6_Radius, out hv_R6_MinDist,
                            out hv_R6_MaxDist, out hv_R6_AvgDist, out hv_R6_SigmaDist);
                        hv_NumCircles = hv_NumCircles + 1;
                        if ((int)(new HTuple(hv_R6.TupleGreater(hv_R6_Radius))) != 0)
                        {
                            hv_R6 = hv_R6_Radius.Clone();
                            mResult = new CircleResult()
                            {
                                Row = new HTuple(hv_R6_Row),
                                Col = new HTuple(hv_R6_Column),
                                Radius = new HTuple(hv_R6_Radius),
                                StartPhi = new HTuple(hv_R6_StartPhi),
                                EndPhi = new HTuple(hv_R6_EndPhi),
                                PointOrder = new HTuple(hv_R6_PointOrder),
                            };
                        }
                    }
                }
                hv_R6_R = hv_R6_R + hv_R6_R_Inc;
            }

            ho_R6_Circle.Dispose();
            ho_R6_ROI_Image.Dispose();
            ho_R6_Region.Dispose();
            ho_R6_RegionBorder.Dispose();
            ho_R6_RegionDilation.Dispose();
            ho_R6_ImageReduced.Dispose();
            ho_R6_Edges.Dispose();
            ho_R6_ContoursSplit.Dispose();
            ho_R6_SingleSegment.Dispose();
            ho_R6_ContEllipse.Dispose();

            return mResult;
        }