/// <summary>
    /// Evaluates the sharpness of the calibration plate in the calibration
    /// image.
    /// </summary>
    public void eval_caltab_focus(HObject ho_Image,
                                  HObject ho_Marks,
                                  HTuple hv_Contrast,
                                  out HTuple hv_FocusScore)
    {
        // Local iconic variables

        HObject ho_Region, ho_RegionUnion, ho_ImageReduced;
        HObject ho_DerivGauss;

        // Local control variables

        HTuple hv_Number, hv_MeanGradient, hv_Deviation;
        HTuple hv_MinScore, hv_RawResult;

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Region);
        HOperatorSet.GenEmptyObj(out ho_RegionUnion);
        HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        HOperatorSet.GenEmptyObj(out ho_DerivGauss);

        hv_FocusScore = 0.0;
        if ((int)(new HTuple(hv_Contrast.TupleEqual(0))) != 0)
        {
            ho_Region.Dispose();
            ho_RegionUnion.Dispose();
            ho_ImageReduced.Dispose();
            ho_DerivGauss.Dispose();

            return;
        }
        HOperatorSet.CountObj(ho_Marks, out hv_Number);
        if ((int)(new HTuple(hv_Number.TupleLess(3))) != 0)
        {
            ho_Region.Dispose();
            ho_RegionUnion.Dispose();
            ho_ImageReduced.Dispose();
            ho_DerivGauss.Dispose();

            return;
        }
        ho_Region.Dispose();
        HOperatorSet.GenRegionContourXld(ho_Marks, out ho_Region, "margin");
        ho_RegionUnion.Dispose();
        HOperatorSet.Union1(ho_Region, out ho_RegionUnion);
        ho_ImageReduced.Dispose();
        HOperatorSet.ReduceDomain(ho_Image, ho_RegionUnion, out ho_ImageReduced);
        ho_DerivGauss.Dispose();
        HOperatorSet.DerivateGauss(ho_ImageReduced, out ho_DerivGauss, 0.7, "gradient");
        HOperatorSet.Intensity(ho_Region, ho_DerivGauss, out hv_MeanGradient, out hv_Deviation);
        hv_MinScore = 0.25;
        //Normalize the Gradient with the contrast
        hv_RawResult  = hv_MeanGradient / hv_Contrast;
        hv_FocusScore = (((hv_RawResult * 4.5)).TupleSort())[(new HTuple((new HTuple(hv_RawResult.TupleLength()
                                                                                     )) / 20.0)).TupleRound()];
        hv_FocusScore = ((((((((hv_FocusScore - hv_MinScore)).TupleConcat(0.0))).TupleMax()
                            )).TupleConcat(1.0))).TupleMin();
        ho_Region.Dispose();
        ho_RegionUnion.Dispose();
        ho_ImageReduced.Dispose();
        ho_DerivGauss.Dispose();

        return;
    }
Example #2
0
        // Procedures
        // Chapter: Graphics / Text
        // Short Description: This procedure writes a text message.
        public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
                                 HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
        {
            // Local control variables

            HTuple hv_Red = null, hv_Green = null, hv_Blue = null;
            HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null;
            HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null;
            HTuple hv_WidthWin = null, hv_HeightWin = null, hv_MaxAscent = null;
            HTuple hv_MaxDescent = null, hv_MaxWidth = null, hv_MaxHeight = null;
            HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRow = new HTuple();
            HTuple hv_FactorColumn = new HTuple(), hv_Width = new HTuple();
            HTuple hv_Index = new HTuple(), hv_Ascent = new HTuple();
            HTuple hv_Descent = new HTuple(), hv_W = new HTuple();
            HTuple hv_H = new HTuple(), hv_FrameHeight = new HTuple();
            HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
            HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
            HTuple hv_Exception = new HTuple(), hv_CurrentColor = new HTuple();

            HTuple hv_Color_COPY_INP_TMP  = hv_Color.Clone();
            HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
            HTuple hv_Row_COPY_INP_TMP    = hv_Row.Clone();
            HTuple hv_String_COPY_INP_TMP = hv_String.Clone();

            // Initialize local and output iconic variables

            //This procedure displays text in a graphics window.
            //
            //Input parameters:
            //WindowHandle: The WindowHandle of the graphics window, where
            //   the message should be displayed
            //String: A tuple of strings containing the text message to be displayed
            //CoordSystem: If set to 'window', the text position is given
            //   with respect to the window coordinate system.
            //   If set to 'image', image coordinates are used.
            //   (This may be useful in zoomed images.)
            //Row: The row coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Column: The column coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Color: defines the color of the text as string.
            //   If set to [], '' or 'auto' the currently set color is used.
            //   If a tuple of strings is passed, the colors are used cyclically
            //   for each new textline.
            //Box: If set to 'true', the text is written within a white box.
            //
            //prepare window
            HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue);
            HOperatorSet.GetPart(hv_WindowHandle, out hv_Row1Part, out hv_Column1Part, out hv_Row2Part,
                                 out hv_Column2Part);
            HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowWin, out hv_ColumnWin,
                                          out hv_WidthWin, out hv_HeightWin);
            HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
            //
            //default settings
            if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Row_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Column_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
            {
                hv_Color_COPY_INP_TMP = "";
            }
            //
            hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n");
            //
            //Estimate extentions of text depending on font size.
            HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent,
                                        out hv_MaxWidth, out hv_MaxHeight);
            if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
            {
                hv_R1 = hv_Row_COPY_INP_TMP.Clone();
                hv_C1 = hv_Column_COPY_INP_TMP.Clone();
            }
            else
            {
                //transform image to window coordinates
                hv_FactorRow    = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);
                hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);
                hv_R1           = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow;
                hv_C1           = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn;
            }
            //
            //display text box depending on text size
            if ((int)(new HTuple(hv_Box.TupleEqual("true"))) != 0)
            {
                //calculate box extents
                hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " ";
                hv_Width = new HTuple();
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                                      )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    HOperatorSet.GetStringExtents(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
                                                      hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
                    hv_Width = hv_Width.TupleConcat(hv_W);
                }
                hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                            ));
                hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
                hv_R2         = hv_R1 + hv_FrameHeight;
                hv_C2         = hv_C1 + hv_FrameWidth;
                //display rectangles
                HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode);
                HOperatorSet.SetDraw(hv_WindowHandle, "fill");
                HOperatorSet.SetColor(hv_WindowHandle, "light gray");
                HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 3, hv_C1 + 3, hv_R2 + 3, hv_C2 + 3);
                HOperatorSet.SetColor(hv_WindowHandle, "white");
                HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
                HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode);
            }
            else if ((int)(new HTuple(hv_Box.TupleNotEqual("false"))) != 0)
            {
                hv_Exception = "Wrong value of control parameter Box";
                throw new HalconException(hv_Exception);
            }
            //Write text.
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                                  )) - 1); hv_Index = (int)hv_Index + 1)
            {
                hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                                                                                           )));
                if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
                                                                                                  "auto")))) != 0)
                {
                    HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor);
                }
                else
                {
                    HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
                }
                hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
                HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_C1);
                HOperatorSet.WriteString(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
                                             hv_Index));
            }
            //reset changed window settings
            HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
            HOperatorSet.SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
                                 hv_Column2Part);

            return;
        }
        public static void list_image_files(HTuple hv_ImageDirectory, HTuple hv_Extensions, HTuple hv_Options,
                                            out HTuple hv_ImageFiles)
        {
            // Local iconic variables

            // Local control variables

            HTuple hv_ImageDirectoryIndex = new HTuple();
            HTuple hv_ImageFilesTmp = new HTuple(), hv_CurrentImageDirectory = new HTuple();
            HTuple hv_HalconImages = new HTuple(), hv_OS = new HTuple();
            HTuple hv_Directories = new HTuple(), hv_Index = new HTuple();
            HTuple hv_Length = new HTuple(), hv_NetworkDrive = new HTuple();
            HTuple hv_Substring = new HTuple(), hv_FileExists = new HTuple();
            HTuple hv_AllFiles = new HTuple(), hv_i = new HTuple();
            HTuple hv_Selection               = new HTuple();
            HTuple hv_Extensions_COPY_INP_TMP = new HTuple(hv_Extensions);

            // Initialize local and output iconic variables
            hv_ImageFiles = new HTuple();
            //This procedure returns all files in a given directory
            //with one of the suffixes specified in Extensions.
            //
            //Input parameters:
            //ImageDirectory: Directory or a tuple of directories with images.
            //   If a directory is not found locally, the respective directory
            //   is searched under %HALCONIMAGES%/ImageDirectory.
            //   See the Installation Guide for further information
            //   in case %HALCONIMAGES% is not set.
            //Extensions: A string tuple containing the extensions to be found
            //   e.g. ['png','tif',jpg'] or others
            //If Extensions is set to 'default' or the empty string '',
            //   all image suffixes supported by HALCON are used.
            //Options: as in the operator list_files, except that the 'files'
            //   option is always used. Note that the 'directories' option
            //   has no effect but increases runtime, because only files are
            //   returned.
            //
            //Output parameter:
            //ImageFiles: A tuple of all found image file names
            //
            if ((int)((new HTuple((new HTuple(hv_Extensions_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
                                      new HTuple(hv_Extensions_COPY_INP_TMP.TupleEqual(""))))).TupleOr(new HTuple(hv_Extensions_COPY_INP_TMP.TupleEqual(
                                                                                                                      "default")))) != 0)
            {
                hv_Extensions_COPY_INP_TMP.Dispose();
                hv_Extensions_COPY_INP_TMP     = new HTuple();
                hv_Extensions_COPY_INP_TMP[0]  = "ima";
                hv_Extensions_COPY_INP_TMP[1]  = "tif";
                hv_Extensions_COPY_INP_TMP[2]  = "tiff";
                hv_Extensions_COPY_INP_TMP[3]  = "gif";
                hv_Extensions_COPY_INP_TMP[4]  = "bmp";
                hv_Extensions_COPY_INP_TMP[5]  = "jpg";
                hv_Extensions_COPY_INP_TMP[6]  = "jpeg";
                hv_Extensions_COPY_INP_TMP[7]  = "jp2";
                hv_Extensions_COPY_INP_TMP[8]  = "jxr";
                hv_Extensions_COPY_INP_TMP[9]  = "png";
                hv_Extensions_COPY_INP_TMP[10] = "pcx";
                hv_Extensions_COPY_INP_TMP[11] = "ras";
                hv_Extensions_COPY_INP_TMP[12] = "xwd";
                hv_Extensions_COPY_INP_TMP[13] = "pbm";
                hv_Extensions_COPY_INP_TMP[14] = "pnm";
                hv_Extensions_COPY_INP_TMP[15] = "pgm";
                hv_Extensions_COPY_INP_TMP[16] = "ppm";
                //
            }
            hv_ImageFiles.Dispose();
            hv_ImageFiles = new HTuple();
            //Loop through all given image directories.
            for (hv_ImageDirectoryIndex = 0; (int)hv_ImageDirectoryIndex <= (int)((new HTuple(hv_ImageDirectory.TupleLength()
                                                                                              )) - 1); hv_ImageDirectoryIndex = (int)hv_ImageDirectoryIndex + 1)
            {
                hv_ImageFilesTmp.Dispose();
                hv_ImageFilesTmp = new HTuple();
                hv_CurrentImageDirectory.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_CurrentImageDirectory = hv_ImageDirectory.TupleSelect(
                        hv_ImageDirectoryIndex);
                }
                if ((int)(new HTuple(hv_CurrentImageDirectory.TupleEqual(""))) != 0)
                {
                    hv_CurrentImageDirectory.Dispose();
                    hv_CurrentImageDirectory = ".";
                }
                hv_HalconImages.Dispose();
                HOperatorSet.GetSystem("image_dir", out hv_HalconImages);
                hv_OS.Dispose();
                HOperatorSet.GetSystem("operating_system", out hv_OS);
                if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        {
                            HTuple
                                ExpTmpLocalVar_HalconImages = hv_HalconImages.TupleSplit(
                                ";");
                            hv_HalconImages.Dispose();
                            hv_HalconImages = ExpTmpLocalVar_HalconImages;
                        }
                    }
                }
                else
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        {
                            HTuple
                                ExpTmpLocalVar_HalconImages = hv_HalconImages.TupleSplit(
                                ":");
                            hv_HalconImages.Dispose();
                            hv_HalconImages = ExpTmpLocalVar_HalconImages;
                        }
                    }
                }
                hv_Directories.Dispose();
                hv_Directories = new HTuple(hv_CurrentImageDirectory);
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_HalconImages.TupleLength()
                                                                      )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        {
                            HTuple
                                ExpTmpLocalVar_Directories = hv_Directories.TupleConcat(
                                ((hv_HalconImages.TupleSelect(hv_Index)) + "/") + hv_CurrentImageDirectory);
                            hv_Directories.Dispose();
                            hv_Directories = ExpTmpLocalVar_Directories;
                        }
                    }
                }
                hv_Length.Dispose();
                HOperatorSet.TupleStrlen(hv_Directories, out hv_Length);
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_NetworkDrive.Dispose();
                    HOperatorSet.TupleGenConst(new HTuple(hv_Length.TupleLength()), 0, out hv_NetworkDrive);
                }
                if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
                {
                    for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_Length.TupleLength()
                                                                          )) - 1); hv_Index = (int)hv_Index + 1)
                    {
                        if ((int)(new HTuple(((((hv_Directories.TupleSelect(hv_Index))).TupleStrlen()
                                               )).TupleGreater(1))) != 0)
                        {
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_Substring.Dispose();
                                HOperatorSet.TupleStrFirstN(hv_Directories.TupleSelect(hv_Index), 1,
                                                            out hv_Substring);
                            }
                            if ((int)((new HTuple(hv_Substring.TupleEqual("//"))).TupleOr(new HTuple(hv_Substring.TupleEqual(
                                                                                                         "\\\\")))) != 0)
                            {
                                if (hv_NetworkDrive == null)
                                {
                                    hv_NetworkDrive = new HTuple();
                                }
                                hv_NetworkDrive[hv_Index] = 1;
                            }
                        }
                    }
                }
                hv_ImageFilesTmp.Dispose();
                hv_ImageFilesTmp = new HTuple();
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_Directories.TupleLength()
                                                                      )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_FileExists.Dispose();
                        HOperatorSet.FileExists(hv_Directories.TupleSelect(hv_Index), out hv_FileExists);
                    }
                    if ((int)(hv_FileExists) != 0)
                    {
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_AllFiles.Dispose();
                            HOperatorSet.ListFiles(hv_Directories.TupleSelect(hv_Index), (new HTuple("files")).TupleConcat(
                                                       hv_Options), out hv_AllFiles);
                        }
                        hv_ImageFilesTmp.Dispose();
                        hv_ImageFilesTmp = new HTuple();
                        for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_Extensions_COPY_INP_TMP.TupleLength()
                                                                      )) - 1); hv_i = (int)hv_i + 1)
                        {
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_Selection.Dispose();
                                HOperatorSet.TupleRegexpSelect(hv_AllFiles, (((".*" + (hv_Extensions_COPY_INP_TMP.TupleSelect(
                                                                                           hv_i))) + "$")).TupleConcat("ignore_case"), out hv_Selection);
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_ImageFilesTmp = hv_ImageFilesTmp.TupleConcat(
                                        hv_Selection);
                                    hv_ImageFilesTmp.Dispose();
                                    hv_ImageFilesTmp = ExpTmpLocalVar_ImageFilesTmp;
                                }
                            }
                        }
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleRegexpReplace(hv_ImageFilesTmp, (new HTuple("\\\\")).TupleConcat(
                                                                "replace_all"), "/", out ExpTmpOutVar_0);
                            hv_ImageFilesTmp.Dispose();
                            hv_ImageFilesTmp = ExpTmpOutVar_0;
                        }
                        if ((int)(hv_NetworkDrive.TupleSelect(hv_Index)) != 0)
                        {
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleRegexpReplace(hv_ImageFilesTmp, (new HTuple("//")).TupleConcat(
                                                                    "replace_all"), "/", out ExpTmpOutVar_0);
                                hv_ImageFilesTmp.Dispose();
                                hv_ImageFilesTmp = ExpTmpOutVar_0;
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_ImageFilesTmp = "/" + hv_ImageFilesTmp;
                                    hv_ImageFilesTmp.Dispose();
                                    hv_ImageFilesTmp = ExpTmpLocalVar_ImageFilesTmp;
                                }
                            }
                        }
                        else
                        {
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleRegexpReplace(hv_ImageFilesTmp, (new HTuple("//")).TupleConcat(
                                                                    "replace_all"), "/", out ExpTmpOutVar_0);
                                hv_ImageFilesTmp.Dispose();
                                hv_ImageFilesTmp = ExpTmpOutVar_0;
                            }
                        }
                        break;
                    }
                }
                //Concatenate the output image paths.
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    {
                        HTuple
                            ExpTmpLocalVar_ImageFiles = hv_ImageFiles.TupleConcat(
                            hv_ImageFilesTmp);
                        hv_ImageFiles.Dispose();
                        hv_ImageFiles = ExpTmpLocalVar_ImageFiles;
                    }
                }
            }

            hv_Extensions_COPY_INP_TMP.Dispose();
            hv_ImageDirectoryIndex.Dispose();
            hv_ImageFilesTmp.Dispose();
            hv_CurrentImageDirectory.Dispose();
            hv_HalconImages.Dispose();
            hv_OS.Dispose();
            hv_Directories.Dispose();
            hv_Index.Dispose();
            hv_Length.Dispose();
            hv_NetworkDrive.Dispose();
            hv_Substring.Dispose();
            hv_FileExists.Dispose();
            hv_AllFiles.Dispose();
            hv_i.Dispose();
            hv_Selection.Dispose();

            return;
        }
        internal 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)
        {
            // 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;
            }
        }
Example #5
0
        /// <summary>
        /// 传入原图 传出分离通道后concat一起的图
        /// </summary>
        /// <param name="ho_i_Image"></param>
        /// <param name="ho_o_Image"></param>
        /// <returns></returns>
        public static HObject Separat_Image(HObject ho_i_Image, out HObject ho_o_Image)
        {
            HObject ho_ObjectSelected = null, ho_Image1 = null;
            HObject ho_Image2 = null, ho_Image3 = null, ho_ObjectsConcat = null;

            // Local control variables

            HTuple hv_Number = null, hv_Index = null, hv_Channels = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_o_Image);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_Image1);
            HOperatorSet.GenEmptyObj(out ho_Image2);
            HOperatorSet.GenEmptyObj(out ho_Image3);
            HOperatorSet.GenEmptyObj(out ho_ObjectsConcat);

            HOperatorSet.CountObj(ho_i_Image, out hv_Number);
            ho_o_Image.Dispose();
            HOperatorSet.GenEmptyObj(out ho_o_Image);
            HTuple end_val3  = hv_Number;
            HTuple step_val3 = 1;

            for (hv_Index = 1; hv_Index.Continue(end_val3, step_val3); hv_Index = hv_Index.TupleAdd(step_val3))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_i_Image, out ho_ObjectSelected, hv_Index);
                HOperatorSet.CountChannels(ho_ObjectSelected, out hv_Channels);
                if ((int)(new HTuple(hv_Channels.TupleEqual(3))) != 0)
                {
                    ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose();
                    HOperatorSet.Decompose3(ho_ObjectSelected, out ho_Image1, out ho_Image2,
                                            out ho_Image3);
                    ho_ObjectsConcat.Dispose();
                    HOperatorSet.ConcatObj(ho_Image1, ho_Image2, out ho_ObjectsConcat);
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.ConcatObj(ho_ObjectsConcat, ho_Image3, out ExpTmpOutVar_0);
                        ho_ObjectsConcat.Dispose();
                        ho_ObjectsConcat = ExpTmpOutVar_0;
                    }
                }
                else
                {
                    ho_ObjectsConcat.Dispose();
                    ho_ObjectsConcat = ho_ObjectSelected.CopyObj(1, -1);
                }

                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.ConcatObj(ho_o_Image, ho_ObjectsConcat, out ExpTmpOutVar_0);
                    ho_o_Image.Dispose();
                    ho_o_Image = ExpTmpOutVar_0;
                }
            }

            ho_ObjectSelected.Dispose();
            ho_Image1.Dispose();
            ho_Image2.Dispose();
            ho_Image3.Dispose();
            ho_ObjectsConcat.Dispose();

            return(ho_o_Image);
        }
Example #6
0
        public static void dispMessage(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
                                       HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
        {
            // Local iconic variables

            // Local control variables

            HTuple hv_GenParamName = null, hv_GenParamValue = null;
            HTuple hv_Color_COPY_INP_TMP       = hv_Color.Clone();
            HTuple hv_Column_COPY_INP_TMP      = hv_Column.Clone();
            HTuple hv_CoordSystem_COPY_INP_TMP = hv_CoordSystem.Clone();
            HTuple hv_Row_COPY_INP_TMP         = hv_Row.Clone();

            // Initialize local and output iconic variables
            //This procedure displays text in a graphics window.
            //
            //Input parameters:
            //WindowHandle: The WindowHandle of the graphics window, where
            //   the message should be displayed
            //String: A tuple of strings containing the text message to be displayed
            //CoordSystem: If set to 'window', the text position is given
            //   with respect to the window coordinate system.
            //   If set to 'image', image coordinates are used.
            //   (This may be useful in zoomed images.)
            //Row: The row coordinate of the desired text position
            //   A tuple of values is allowed to display text at different
            //   positions.
            //Column: The column coordinate of the desired text position
            //   A tuple of values is allowed to display text at different
            //   positions.
            //Color: defines the color of the text as string.
            //   If set to [], '' or 'auto' the currently set color is used.
            //   If a tuple of strings is passed, the colors are used cyclically...
            //   - if |Row| == |Column| == 1: for each new textline
            //   = else for each text position.
            //Box: If Box[0] is set to 'true', the text is written within an orange box.
            //     If set to' false', no box is displayed.
            //     If set to a color string (e.g. 'white', '#FF00CC', etc.),
            //       the text is written in a box of that color.
            //     An optional second value for Box (Box[1]) controls if a shadow is displayed:
            //       'true' -> display a shadow in a default color
            //       'false' -> display no shadow
            //       otherwise -> use given string as color string for the shadow color
            //
            //It is possible to display multiple text strings in a single call.
            //In this case, some restrictions apply:
            //- Multiple text positions can be defined by specifying a tuple
            //  with multiple Row and/or Column coordinates, i.e.:
            //  - |Row| == n, |Column| == n
            //  - |Row| == n, |Column| == 1
            //  - |Row| == 1, |Column| == n
            //- If |Row| == |Column| == 1,
            //  each element of String is display in a new textline.
            //- If multiple positions or specified, the number of Strings
            //  must match the number of positions, i.e.:
            //  - Either |String| == n (each string is displayed at the
            //                          corresponding position),
            //  - or     |String| == 1 (The string is displayed n times).
            //
            //
            //Convert the parameters for disp_text.
            try
            {
                if ((int)((new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
                              new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(new HTuple())))) != 0)
                {
                    return;
                }
                if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
                {
                    hv_Row_COPY_INP_TMP = 12;
                }
                if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
                {
                    hv_Column_COPY_INP_TMP = 12;
                }
                //
                //Convert the parameter Box to generic parameters.
                hv_GenParamName  = new HTuple();
                hv_GenParamValue = new HTuple();
                if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(0))) != 0)
                {
                    if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleEqual("false"))) != 0)
                    {
                        //Display no box
                        hv_GenParamName  = hv_GenParamName.TupleConcat("box");
                        hv_GenParamValue = hv_GenParamValue.TupleConcat("false");
                    }
                    else if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleNotEqual("true"))) != 0)
                    {
                        //Set a color other than the default.
                        hv_GenParamName  = hv_GenParamName.TupleConcat("box_color");
                        hv_GenParamValue = hv_GenParamValue.TupleConcat(hv_Box.TupleSelect(0));
                    }
                }
                if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(1))) != 0)
                {
                    if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleEqual("false"))) != 0)
                    {
                        //Display no shadow.
                        hv_GenParamName  = hv_GenParamName.TupleConcat("shadow");
                        hv_GenParamValue = hv_GenParamValue.TupleConcat("false");
                    }
                    else if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleNotEqual("true"))) != 0)
                    {
                        //Set a shadow color other than the default.
                        hv_GenParamName  = hv_GenParamName.TupleConcat("shadow_color");
                        hv_GenParamValue = hv_GenParamValue.TupleConcat(hv_Box.TupleSelect(1));
                    }
                }
                //Restore default CoordSystem behavior.
                if ((int)(new HTuple(hv_CoordSystem_COPY_INP_TMP.TupleNotEqual("window"))) != 0)
                {
                    hv_CoordSystem_COPY_INP_TMP = "image";
                }
                //
                if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(""))) != 0)
                {
                    //disp_text does not accept an empty string for Color.
                    hv_Color_COPY_INP_TMP = new HTuple();
                }
                //
                HOperatorSet.DispText(hv_WindowHandle, hv_String, hv_CoordSystem_COPY_INP_TMP,
                                      hv_Row_COPY_INP_TMP, hv_Column_COPY_INP_TMP, hv_Color_COPY_INP_TMP, hv_GenParamName,
                                      hv_GenParamValue);
            }
            catch (Exception exc)
            {
                Wells.FrmType.frm_Log.Log(exc.Message, 2);
            }

            return;
        }
Example #7
0
    public void image_cali_map(HObject ho_Image, out HObject ho_TmpObj_RectifiedImage,
                               HTuple hv_CameraParameters, HTuple hv_CameraPose)
    {
        // Local iconic variables

        HObject ho_TmpObj_RectificationMap;

        // Local control variables

        HTuple hv_TmpCtrl_RectificationWidth    = new HTuple();
        HTuple hv_TmpCtrl_RectificationPose     = new HTuple();
        HTuple hv_CameraParameters_COPY_INP_TMP = new HTuple(hv_CameraParameters);
        HTuple hv_CameraPose_COPY_INP_TMP       = new HTuple(hv_CameraPose);

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_TmpObj_RectifiedImage);
        HOperatorSet.GenEmptyObj(out ho_TmpObj_RectificationMap);
        try
        {
            //* GEN CALIDATA
            //Calibration 01: Code generated by Calibration 01
            if ((int)((new HTuple(hv_CameraParameters_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
                          new HTuple(hv_CameraPose_COPY_INP_TMP.TupleEqual(new HTuple())))) != 0)
            {
                hv_CameraParameters_COPY_INP_TMP.Dispose();
                hv_CameraParameters_COPY_INP_TMP    = new HTuple();
                hv_CameraParameters_COPY_INP_TMP[0] = "area_scan_division";
                hv_CameraParameters_COPY_INP_TMP[1] = 0.0395393;
                hv_CameraParameters_COPY_INP_TMP[2] = 29.5225;
                hv_CameraParameters_COPY_INP_TMP[3] = 2.3103e-006;
                hv_CameraParameters_COPY_INP_TMP[4] = 2.4e-006;
                hv_CameraParameters_COPY_INP_TMP[5] = 1969.65;
                hv_CameraParameters_COPY_INP_TMP[6] = 5753.85;
                hv_CameraParameters_COPY_INP_TMP[7] = 4024;
                hv_CameraParameters_COPY_INP_TMP[8] = 3036;
                hv_CameraPose_COPY_INP_TMP.Dispose();
                hv_CameraPose_COPY_INP_TMP    = new HTuple();
                hv_CameraPose_COPY_INP_TMP[0] = 0.00207285;
                hv_CameraPose_COPY_INP_TMP[1] = -0.111611;
                hv_CameraPose_COPY_INP_TMP[2] = 0.434522;
                hv_CameraPose_COPY_INP_TMP[3] = 13.3656;
                hv_CameraPose_COPY_INP_TMP[4] = 0.504334;
                hv_CameraPose_COPY_INP_TMP[5] = 90.2621;
                hv_CameraPose_COPY_INP_TMP[6] = 0;
            }



            //Calibration 01: Sample code generated by Calibration 01
            //Calibration 01: For demonstration purposes, we use the calibration
            //Calibration 01: plate itself as sample object.
            //Calibration 01: Therefore, we load the reference image
            //read_image (Image, 'C:/Users/iwake/OneDrive - wake/Desktop/HalconPractise/HalconProj/Cali/image_02.png')
            //Calibration 01: Extract plate data from the image
            //TmpCtrl_PlateDescription := 'C:/Program Files/MVTec/HALCON-18.11-Steady/calib/caltab_30mm.descr'
            //TmpCtrl_FindCalObjParNames := ['gap_tolerance','alpha','skip_find_caltab']
            //TmpCtrl_FindCalObjParValues := [1,1,'false']
            //Calibration 01: Create calibration model for managing calibration data
            //create_calib_data ('calibration_object', 1, 1, CalibHandle)
            //set_calib_data_cam_param (CalibHandle, 0, [], CameraParameters)
            //set_calib_data_calib_object (CalibHandle, 0, TmpCtrl_PlateDescription)
            //find_calib_object (Image, CalibHandle, 0, 0, 0, TmpCtrl_FindCalObjParNames, TmpCtrl_FindCalObjParValues)
            //get_calib_data_observ_points (CalibHandle, 0, 0, 0, TmpCtrl_MarkRows, TmpCtrl_MarkColumns, TmpCtrl_Ind, CameraPose)
            //Calibration 01: Using the calibration plate as test object, the marks actually
            //Calibration 01: lie above the corrected measurement plane. Therefore, we 'uncorrect'
            //Calibration 01: the plane of measurement by the plate thickness here.
            //set_origin_pose (CameraPose, 0.0, 0.0, 0, CameraPose)


            //read_image (Image, 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/InvV1 (4).jpg')
            //Calibration 01: **********************************************************
            //Calibration 01: Sample Task: Image Rectification
            //Calibration 01: **********************************************************
            //Calibration 01: Choose the desired width of the visible area in world coordinates [mm]
            hv_TmpCtrl_RectificationWidth.Dispose();
            hv_TmpCtrl_RectificationWidth = 75;
            //Calibration 01: Convert to [m]
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                {
                    HTuple
                        ExpTmpLocalVar_TmpCtrl_RectificationWidth = hv_TmpCtrl_RectificationWidth / 1000.0;
                    hv_TmpCtrl_RectificationWidth.Dispose();
                    hv_TmpCtrl_RectificationWidth = ExpTmpLocalVar_TmpCtrl_RectificationWidth;
                }
            }
            //Calibration 01: Adjust origin so the plate is roughly centered
            //* ATTENTION 参数从-0.5 -0.4 改成了-0.5 -0.6
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hv_TmpCtrl_RectificationPose.Dispose();
                HOperatorSet.SetOriginPose(hv_CameraPose_COPY_INP_TMP, -0.5 * hv_TmpCtrl_RectificationWidth,
                                           -0.6 * hv_TmpCtrl_RectificationWidth, 0, out hv_TmpCtrl_RectificationPose);
            }
            //Calibration 01: Generate the rectification map
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                ho_TmpObj_RectificationMap.Dispose();
                HOperatorSet.GenImageToWorldPlaneMap(out ho_TmpObj_RectificationMap, hv_CameraParameters_COPY_INP_TMP,
                                                     hv_TmpCtrl_RectificationPose, 4024, 3036, 3036, 4024, hv_TmpCtrl_RectificationWidth / 3036,
                                                     "bilinear");
            }
            //gen_image_to_world_plane_map (TmpObj_RectificationMap, CameraParameters, TmpCtrl_RectificationPose, 4024, 3036, 3036, 4024, 0.8*TmpCtrl_RectificationWidth / 3036, 'bilinear')
            //Calibration 01: Now, images can be rectified using the rectification map
            ho_TmpObj_RectifiedImage.Dispose();
            HOperatorSet.MapImage(ho_Image, ho_TmpObj_RectificationMap, out ho_TmpObj_RectifiedImage
                                  );
            //stop ()
            ho_TmpObj_RectificationMap.Dispose();

            hv_CameraParameters_COPY_INP_TMP.Dispose();
            hv_CameraPose_COPY_INP_TMP.Dispose();
            hv_TmpCtrl_RectificationWidth.Dispose();
            hv_TmpCtrl_RectificationPose.Dispose();

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

            hv_CameraParameters_COPY_INP_TMP.Dispose();
            hv_CameraPose_COPY_INP_TMP.Dispose();
            hv_TmpCtrl_RectificationWidth.Dispose();
            hv_TmpCtrl_RectificationPose.Dispose();

            throw HDevExpDefaultException;
        }
    }
Example #8
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;
        }
Example #9
0
    public void GetParallelLineFromDistance(HTuple hv_LineInRow, HTuple hv_LineInCol,
                                            HTuple hv_LineInRow1, HTuple hv_LineInCol1, HTuple hv_Distance, HTuple hv_Direction,
                                            HTuple hv_Polarity, out HTuple hv_LineOutRow, out HTuple hv_LineOutCol, out HTuple hv_LineOutRow1,
                                            out HTuple hv_LineOutCol1, out HTuple hv_k, out HTuple hv_b)
    {
        // Local control variables

        HTuple hv_RealDeltaCol, hv_RealDeltaRow, hv_kIn;
        HTuple hv_bIn, hv_k1, hv_b1, hv_k2, hv_b2, hv_Sqrt1, hv_Sqrt2;
        HTuple hv_row1, hv_col1, hv_rowTemp1, hv_colTemp1, hv_row2;
        HTuple hv_col2, hv_rowTemp2, hv_colTemp2;

        // Initialize local and output iconic variables

        hv_LineOutRow  = new HTuple();
        hv_LineOutCol  = new HTuple();
        hv_LineOutRow1 = new HTuple();
        hv_LineOutCol1 = new HTuple();
        HOperatorSet.TupleReal(hv_LineInCol1 - hv_LineInCol, out hv_RealDeltaCol);
        HOperatorSet.TupleReal(hv_LineInRow1 - hv_LineInRow, out hv_RealDeltaRow);

        hv_kIn = hv_RealDeltaCol / hv_RealDeltaRow;
        hv_bIn = hv_LineInCol - (hv_kIn * hv_LineInRow);
        hv_k   = hv_kIn.Clone();
        hv_b   = hv_bIn.Clone();


        hv_k1 = -1 / hv_kIn;
        hv_b1 = ((hv_LineInRow * hv_kIn) + hv_bIn) - (hv_k1 * hv_LineInRow);

        hv_k2 = hv_k1.Clone();
        hv_b2 = ((hv_LineInRow1 * hv_kIn) + hv_bIn) - (hv_k2 * hv_LineInRow1);

        //找出目标点
        HOperatorSet.TupleSqrt((hv_Distance * hv_Distance) / ((hv_k1 * hv_k1) + 1), out hv_Sqrt1);
        HOperatorSet.TupleSqrt((hv_Distance * hv_Distance) / ((hv_k2 * hv_k2) + 1), out hv_Sqrt2);


        hv_row1     = hv_Sqrt1 + hv_LineInRow;
        hv_col1     = (hv_k1 * hv_row1) + hv_b1;
        hv_rowTemp1 = hv_Sqrt2 + hv_LineInRow1;
        hv_colTemp1 = (hv_k2 * hv_rowTemp1) + hv_b2;


        //另一条平行线
        hv_row2     = (-hv_Sqrt1) + hv_LineInRow;
        hv_col2     = (hv_k1 * hv_row2) + hv_b1;
        hv_rowTemp2 = (-hv_Sqrt2) + hv_LineInRow1;
        hv_colTemp2 = (hv_k2 * hv_rowTemp2) + hv_b2;



        if ((int)(new HTuple(hv_Direction.TupleEqual("row"))) != 0)
        {
            if ((int)(new HTuple(hv_Polarity.TupleGreaterEqual(0))) != 0)
            {
                hv_LineOutRow  = hv_row1.Clone();
                hv_LineOutCol  = hv_col1.Clone();
                hv_LineOutRow1 = hv_rowTemp1.Clone();
                hv_LineOutCol1 = hv_colTemp1.Clone();
            }
            else
            {
                hv_LineOutRow  = hv_row2.Clone();
                hv_LineOutCol  = hv_col2.Clone();
                hv_LineOutRow1 = hv_rowTemp2.Clone();
                hv_LineOutCol1 = hv_colTemp2.Clone();
            }
        }

        if ((int)(new HTuple(hv_Direction.TupleEqual("col"))) != 0)
        {
            if ((int)(new HTuple(hv_Polarity.TupleGreaterEqual(0))) != 0)
            {
                if ((int)(new HTuple(hv_col1.TupleGreaterEqual(hv_LineInCol))) != 0)
                {
                    hv_LineOutCol  = hv_col1.Clone();
                    hv_LineOutRow  = hv_row1.Clone();
                    hv_LineOutRow1 = hv_rowTemp1.Clone();
                    hv_LineOutCol1 = hv_colTemp1.Clone();
                }
                else
                {
                    hv_LineOutCol  = hv_col2.Clone();
                    hv_LineOutRow  = hv_row2.Clone();
                    hv_LineOutRow1 = hv_rowTemp2.Clone();
                    hv_LineOutCol1 = hv_colTemp2.Clone();
                }
            }
            else
            {
                if ((int)(new HTuple(hv_col1.TupleGreaterEqual(hv_LineInCol))) != 0)
                {
                    hv_LineOutCol  = hv_col2.Clone();
                    hv_LineOutRow  = hv_row2.Clone();
                    hv_LineOutRow1 = hv_rowTemp2.Clone();
                    hv_LineOutCol1 = hv_colTemp2.Clone();
                }
                else
                {
                    hv_LineOutCol  = hv_col1.Clone();
                    hv_LineOutRow  = hv_row1.Clone();
                    hv_LineOutRow1 = hv_rowTemp1.Clone();
                    hv_LineOutCol1 = hv_colTemp1.Clone();
                }
            }
        }



        return;
    }
Example #10
0
    public void region_ocr_num_svm(HObject ho_Image, HObject ho_ROI_OCR_01_0, HTuple hv_TextModel,
                                   HTuple hv_OcrHandle, out HTuple hv_SymbolNames_OCR_01_0, out HTuple hv_Ocr_Split)
    {
        // Local iconic variables

        HObject ho_TmpObj_Mono, ho_TmpObj_MonoReduced_OCR_01_0;
        HObject ho_TmpObj_Domain, ho_TmpObj_DomainExpanded, ho_TmpObj_DomainTransformedRaw;
        HObject ho_TmpObj_DomainTransformed, ho_TmpObj_ImageTransformed;
        HObject ho_TmpObj_ImageTransformedExpanded, ho_TmpObj_ImageTransformedReduced;
        HObject ho_Symbols_OCR_01_0;

        // Local control variables

        HTuple hv_TmpCtrl_MatrixIdentity = new HTuple();
        HTuple hv_TmpCtrl_ClipRegion = new HTuple(), hv_TmpCtrl_Row1 = new HTuple();
        HTuple hv_TmpCtrl_Col1 = new HTuple(), hv_TmpCtrl_Row2 = new HTuple();
        HTuple hv_TmpCtrl_Col2 = new HTuple(), hv_TmpCtrl_MatrixTranslation = new HTuple();
        HTuple hv_TmpCtrl_MatrixComposite = new HTuple(), hv_TmpCtrl_ResultHandle_OCR_01_0 = new HTuple();
        HTuple hv_OcrHandle_COPY_INP_TMP = new HTuple(hv_OcrHandle);
        HTuple hv_TextModel_COPY_INP_TMP = new HTuple(hv_TextModel);

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_TmpObj_Mono);
        HOperatorSet.GenEmptyObj(out ho_TmpObj_MonoReduced_OCR_01_0);
        HOperatorSet.GenEmptyObj(out ho_TmpObj_Domain);
        HOperatorSet.GenEmptyObj(out ho_TmpObj_DomainExpanded);
        HOperatorSet.GenEmptyObj(out ho_TmpObj_DomainTransformedRaw);
        HOperatorSet.GenEmptyObj(out ho_TmpObj_DomainTransformed);
        HOperatorSet.GenEmptyObj(out ho_TmpObj_ImageTransformed);
        HOperatorSet.GenEmptyObj(out ho_TmpObj_ImageTransformedExpanded);
        HOperatorSet.GenEmptyObj(out ho_TmpObj_ImageTransformedReduced);
        HOperatorSet.GenEmptyObj(out ho_Symbols_OCR_01_0);
        hv_SymbolNames_OCR_01_0 = new HTuple();
        hv_Ocr_Split            = new HTuple();
        try
        {
            //OCR 01: Code generated by OCR 01
            //OCR 01:
            //OCR 01: Prepare text model
            //OCR 01:
            if ((int)(new HTuple(hv_TextModel_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
            {
                hv_TextModel_COPY_INP_TMP.Dispose();
                HOperatorSet.CreateTextModelReader("manual", new HTuple(), out hv_TextModel_COPY_INP_TMP);
                HOperatorSet.SetTextModelParam(hv_TextModel_COPY_INP_TMP, "manual_stroke_width",
                                               5);
                HOperatorSet.SetTextModelParam(hv_TextModel_COPY_INP_TMP, "manual_fragment_size_min",
                                               5);
                HOperatorSet.SetTextModelParam(hv_TextModel_COPY_INP_TMP, "manual_eliminate_border_blobs",
                                               "true");
                HOperatorSet.SetTextModelParam(hv_TextModel_COPY_INP_TMP, "manual_base_line_tolerance",
                                               0.2);
                HOperatorSet.SetTextModelParam(hv_TextModel_COPY_INP_TMP, "manual_max_line_num",
                                               2);
            }

            //OCR 01:
            //OCR 01: Load classifier
            //OCR 01:
            if ((int)(new HTuple(hv_OcrHandle_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
            {
                hv_OcrHandle_COPY_INP_TMP.Dispose();
                HOperatorSet.ReadOcrClassSvm("C:/Users/iwake/OneDrive - wake/Desktop/OCR/NumOcrTrainBlck.osc",
                                             out hv_OcrHandle_COPY_INP_TMP);
            }


            //OCR 01:
            //OCR 01: Extract symbol regions (segmentation step)
            //OCR 01: Only consider first channel for color images
            ho_TmpObj_Mono.Dispose();
            HOperatorSet.AccessChannel(ho_Image, out ho_TmpObj_Mono, 1);
            ho_TmpObj_MonoReduced_OCR_01_0.Dispose();
            HOperatorSet.ReduceDomain(ho_TmpObj_Mono, ho_ROI_OCR_01_0, out ho_TmpObj_MonoReduced_OCR_01_0
                                      );
            //OCR 01:
            //OCR 01: Orientation Correction (for consistent border and domain
            //OCR 01: handling, this is always applied, even with no rotation)
            //OCR 01:
            hv_TmpCtrl_MatrixIdentity.Dispose();
            HOperatorSet.HomMat2dIdentity(out hv_TmpCtrl_MatrixIdentity);
            //OCR 01: Apply transformation to image and domain
            ho_TmpObj_Domain.Dispose();
            HOperatorSet.GetDomain(ho_TmpObj_MonoReduced_OCR_01_0, out ho_TmpObj_Domain
                                   );
            hv_TmpCtrl_ClipRegion.Dispose();
            HOperatorSet.GetSystem("clip_region", out hv_TmpCtrl_ClipRegion);
            HOperatorSet.SetSystem("clip_region", "false");
            ho_TmpObj_DomainExpanded.Dispose();
            HOperatorSet.DilationCircle(ho_TmpObj_Domain, out ho_TmpObj_DomainExpanded,
                                        25);
            ho_TmpObj_DomainTransformedRaw.Dispose();
            HOperatorSet.AffineTransRegion(ho_TmpObj_DomainExpanded, out ho_TmpObj_DomainTransformedRaw,
                                           hv_TmpCtrl_MatrixIdentity, "true");
            hv_TmpCtrl_Row1.Dispose(); hv_TmpCtrl_Col1.Dispose(); hv_TmpCtrl_Row2.Dispose(); hv_TmpCtrl_Col2.Dispose();
            HOperatorSet.SmallestRectangle1(ho_TmpObj_DomainTransformedRaw, out hv_TmpCtrl_Row1,
                                            out hv_TmpCtrl_Col1, out hv_TmpCtrl_Row2, out hv_TmpCtrl_Col2);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hv_TmpCtrl_MatrixTranslation.Dispose();
                HOperatorSet.HomMat2dTranslate(hv_TmpCtrl_MatrixIdentity, -hv_TmpCtrl_Row1,
                                               -hv_TmpCtrl_Col1, out hv_TmpCtrl_MatrixTranslation);
            }
            hv_TmpCtrl_MatrixComposite.Dispose();
            HOperatorSet.HomMat2dCompose(hv_TmpCtrl_MatrixTranslation, hv_TmpCtrl_MatrixIdentity,
                                         out hv_TmpCtrl_MatrixComposite);
            ho_TmpObj_DomainTransformed.Dispose();
            HOperatorSet.AffineTransRegion(ho_TmpObj_Domain, out ho_TmpObj_DomainTransformed,
                                           hv_TmpCtrl_MatrixComposite, "true");
            ho_TmpObj_ImageTransformed.Dispose();
            HOperatorSet.AffineTransImage(ho_TmpObj_MonoReduced_OCR_01_0, out ho_TmpObj_ImageTransformed,
                                          hv_TmpCtrl_MatrixComposite, "constant", "true");
            ho_TmpObj_DomainExpanded.Dispose();
            HOperatorSet.DilationCircle(ho_TmpObj_Domain, out ho_TmpObj_DomainExpanded,
                                        25);
            ho_TmpObj_ImageTransformedExpanded.Dispose();
            HOperatorSet.ExpandDomainGray(ho_TmpObj_ImageTransformed, out ho_TmpObj_ImageTransformedExpanded,
                                          25);
            ho_TmpObj_ImageTransformedReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_TmpObj_ImageTransformed, ho_TmpObj_DomainTransformed,
                                      out ho_TmpObj_ImageTransformedReduced);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                ho_TmpObj_MonoReduced_OCR_01_0.Dispose();
                HOperatorSet.CropPart(ho_TmpObj_ImageTransformedReduced, out ho_TmpObj_MonoReduced_OCR_01_0,
                                      0, 0, (hv_TmpCtrl_Col2 - hv_TmpCtrl_Col1) + 1, (hv_TmpCtrl_Row2 - hv_TmpCtrl_Row1) + 1);
            }
            HOperatorSet.SetSystem("clip_region", hv_TmpCtrl_ClipRegion);
            hv_TmpCtrl_ResultHandle_OCR_01_0.Dispose();
            HOperatorSet.FindText(ho_TmpObj_MonoReduced_OCR_01_0, hv_TextModel_COPY_INP_TMP,
                                  out hv_TmpCtrl_ResultHandle_OCR_01_0);

            //OCR 01:
            //OCR 01: Read text (classification step)
            ho_Symbols_OCR_01_0.Dispose();
            HOperatorSet.GetTextObject(out ho_Symbols_OCR_01_0, hv_TmpCtrl_ResultHandle_OCR_01_0,
                                       "manual_all_lines");
            //dev_display (TmpObj_MonoReduced_OCR_01_0)
            //dev_set_draw ('fill')
            //dev_set_colored (3)
            //dev_display (Symbols_OCR_01_0)
            hv_SymbolNames_OCR_01_0.Dispose();
            HOperatorSet.DoOcrMultiClassSvm(ho_Symbols_OCR_01_0, ho_TmpObj_MonoReduced_OCR_01_0,
                                            hv_OcrHandle_COPY_INP_TMP, out hv_SymbolNames_OCR_01_0);
            hv_Ocr_Split.Dispose();
            ocr_split(hv_SymbolNames_OCR_01_0, out hv_Ocr_Split);
            ho_TmpObj_Mono.Dispose();
            ho_TmpObj_MonoReduced_OCR_01_0.Dispose();
            ho_TmpObj_Domain.Dispose();
            ho_TmpObj_DomainExpanded.Dispose();
            ho_TmpObj_DomainTransformedRaw.Dispose();
            ho_TmpObj_DomainTransformed.Dispose();
            ho_TmpObj_ImageTransformed.Dispose();
            ho_TmpObj_ImageTransformedExpanded.Dispose();
            ho_TmpObj_ImageTransformedReduced.Dispose();
            ho_Symbols_OCR_01_0.Dispose();

            hv_OcrHandle_COPY_INP_TMP.Dispose();
            hv_TextModel_COPY_INP_TMP.Dispose();
            hv_TmpCtrl_MatrixIdentity.Dispose();
            hv_TmpCtrl_ClipRegion.Dispose();
            hv_TmpCtrl_Row1.Dispose();
            hv_TmpCtrl_Col1.Dispose();
            hv_TmpCtrl_Row2.Dispose();
            hv_TmpCtrl_Col2.Dispose();
            hv_TmpCtrl_MatrixTranslation.Dispose();
            hv_TmpCtrl_MatrixComposite.Dispose();
            hv_TmpCtrl_ResultHandle_OCR_01_0.Dispose();

            return;
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_TmpObj_Mono.Dispose();
            ho_TmpObj_MonoReduced_OCR_01_0.Dispose();
            ho_TmpObj_Domain.Dispose();
            ho_TmpObj_DomainExpanded.Dispose();
            ho_TmpObj_DomainTransformedRaw.Dispose();
            ho_TmpObj_DomainTransformed.Dispose();
            ho_TmpObj_ImageTransformed.Dispose();
            ho_TmpObj_ImageTransformedExpanded.Dispose();
            ho_TmpObj_ImageTransformedReduced.Dispose();
            ho_Symbols_OCR_01_0.Dispose();

            hv_OcrHandle_COPY_INP_TMP.Dispose();
            hv_TextModel_COPY_INP_TMP.Dispose();
            hv_TmpCtrl_MatrixIdentity.Dispose();
            hv_TmpCtrl_ClipRegion.Dispose();
            hv_TmpCtrl_Row1.Dispose();
            hv_TmpCtrl_Col1.Dispose();
            hv_TmpCtrl_Row2.Dispose();
            hv_TmpCtrl_Col2.Dispose();
            hv_TmpCtrl_MatrixTranslation.Dispose();
            hv_TmpCtrl_MatrixComposite.Dispose();
            hv_TmpCtrl_ResultHandle_OCR_01_0.Dispose();

            throw HDevExpDefaultException;
        }
    }
Example #11
0
    // Local procedures
    public void GetVerticalFromDistance(HTuple hv_FootRow, HTuple hv_FootCol, HTuple hv_LineRowStart,
                                        HTuple hv_LineColStart, HTuple hv_LineRowEnd, HTuple hv_LineColEnd, HTuple hv_Distance,
                                        HTuple hv_Direction, HTuple hv_Polarity, out HTuple hv_TargetRow, out HTuple hv_TargetCol,
                                        out HTuple hv_k, out HTuple hv_b, out HTuple hv_kIn, out HTuple hv_bIn)
    {
        // Local control variables

        HTuple hv_RealDeltaCol, hv_RealDeltaRow, hv_Sqrt;
        HTuple hv_row1, hv_col1, hv_row2, hv_col2, hv_TargetCo1 = new HTuple();

        // Initialize local and output iconic variables

        hv_TargetRow = new HTuple();
        hv_TargetCol = new HTuple();
        HOperatorSet.TupleReal(hv_LineColEnd - hv_LineColStart, out hv_RealDeltaCol);
        HOperatorSet.TupleReal(hv_LineRowEnd - hv_LineRowStart, out hv_RealDeltaRow);

        hv_kIn = hv_RealDeltaCol / hv_RealDeltaRow;
        hv_bIn = hv_FootCol - (hv_kIn * hv_FootRow);

        hv_k = -1 / hv_kIn;
        hv_b = ((hv_FootRow * hv_kIn) + hv_bIn) - (hv_k * hv_FootRow);

        //找出目标点
        HOperatorSet.TupleSqrt((hv_Distance * hv_Distance) / ((hv_k * hv_k) + 1), out hv_Sqrt);
        hv_row1 = hv_Sqrt + hv_FootRow;
        hv_col1 = (hv_k * hv_row1) + hv_b;

        hv_row2 = (-hv_Sqrt) + hv_FootRow;
        hv_col2 = (hv_k * hv_row2) + hv_b;

        if ((int)(new HTuple(hv_Direction.TupleEqual("row"))) != 0)
        {
            if ((int)(new HTuple(hv_Polarity.TupleGreaterEqual(0))) != 0)
            {
                hv_TargetRow = hv_row1.Clone();
                hv_TargetCol = hv_col1.Clone();
            }
            else
            {
                hv_TargetRow = hv_row2.Clone();
                hv_TargetCol = hv_col2.Clone();
            }
        }

        if ((int)(new HTuple(hv_Direction.TupleEqual("col"))) != 0)
        {
            if ((int)(new HTuple(hv_Polarity.TupleGreaterEqual(0))) != 0)
            {
                if ((int)(new HTuple(hv_col1.TupleGreaterEqual(hv_FootCol))) != 0)
                {
                    hv_TargetCo1 = hv_col1.Clone();
                    hv_TargetRow = hv_row1.Clone();
                }
                else
                {
                    hv_TargetCo1 = hv_col2.Clone();
                    hv_TargetRow = hv_row1.Clone();
                }
            }
            else
            {
                if ((int)(new HTuple(hv_col1.TupleGreaterEqual(hv_FootCol))) != 0)
                {
                    hv_TargetCo1 = hv_col2.Clone();
                    hv_TargetRow = hv_row2.Clone();
                }
                else
                {
                    hv_TargetCo1 = hv_col1.Clone();
                    hv_TargetRow = hv_row1.Clone();
                }
            }
        }


        return;
    }
Example #12
0
    public void GetCalibrationParameters(HTuple hv_DescrFile, HTuple hv_ImageDirectory,
                                         HTuple hv_Focus, HTuple hv_Sx, HTuple hv_Sy, HTuple hv_Width, HTuple hv_Height,
                                         HTuple hv_CameraType, out HTuple hv_interCamera, out HTuple hv_PoseNewOrigin,
                                         out HTuple hv_distance)
    {
        // Local iconic variables

        HObject ho_Image = null, ho_CaltabRegion = null;


        // Local control variables

        HTuple hv_CalibDataID = null, hv_startParam = new HTuple();
        HTuple hv_ImageFiles = null, hv_n = null, hv_RCoord = new HTuple();
        HTuple hv_CCoord = new HTuple(), hv_StartPose = new HTuple();
        HTuple hv_Error = null, hv_Pose = null, hv_cut = null;
        HTuple hv_YX = null, hv_YY = null, hv_X = null, hv_Y = null;
        HTuple hv_X1 = null, hv_Y1 = null, hv_X2 = null, hv_Y2 = null;
        HTuple hv_Distance1 = null, hv_Distance2 = null, hv_OffsetX = null;
        HTuple hv_OffsetY = null, hv_PoseR = null;

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_CaltabRegion);

        HOperatorSet.CreateCalibData("calibration_object", 1, 1, out hv_CalibDataID);

        if ((int)((new HTuple(hv_Width.TupleEqual("area_scan_telecentric_division"))).TupleOr(
                      new HTuple(hv_Width.TupleEqual("area_scan_telecentric_polynomial")))) != 0)
        {
            hv_startParam    = new HTuple();
            hv_startParam[0] = 0;
            hv_startParam    = hv_startParam.TupleConcat(hv_Sx);
            hv_startParam    = hv_startParam.TupleConcat(hv_Sy);
            hv_startParam    = hv_startParam.TupleConcat(hv_Width / 2);
            hv_startParam    = hv_startParam.TupleConcat(hv_Height / 2);
            hv_startParam    = hv_startParam.TupleConcat(hv_Width);
            hv_startParam    = hv_startParam.TupleConcat(hv_Height);

            HOperatorSet.SetCalibDataCamParam(hv_CalibDataID, 0, hv_CameraType, hv_startParam);
        }
        else
        {
            hv_startParam = new HTuple();
            hv_startParam = hv_startParam.TupleConcat(hv_Focus);
            hv_startParam = hv_startParam.TupleConcat(0);
            hv_startParam = hv_startParam.TupleConcat(hv_Sx);
            hv_startParam = hv_startParam.TupleConcat(hv_Sy);
            hv_startParam = hv_startParam.TupleConcat(hv_Width / 2);
            hv_startParam = hv_startParam.TupleConcat(hv_Height / 2);
            hv_startParam = hv_startParam.TupleConcat(hv_Width);
            hv_startParam = hv_startParam.TupleConcat(hv_Height);

            HOperatorSet.SetCalibDataCamParam(hv_CalibDataID, 0, hv_CameraType, hv_startParam);
        }

        HOperatorSet.SetCalibDataCalibObject(hv_CalibDataID, 0, hv_DescrFile);

        list_image_files(hv_ImageDirectory, "default", new HTuple(), out hv_ImageFiles);


        for (hv_n = 0; (int)hv_n <= (int)((new HTuple(hv_ImageFiles.TupleLength())) - 1); hv_n = (int)hv_n + 1)
        {
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(hv_n));

            ho_CaltabRegion.Dispose();
            HOperatorSet.FindCaltab(ho_Image, out ho_CaltabRegion, hv_DescrFile, 3, 112,
                                    5);

            HOperatorSet.FindMarksAndPose(ho_Image, ho_CaltabRegion, hv_DescrFile, hv_startParam,
                                          128, 10, 18, 0.9, 15, 100, out hv_RCoord, out hv_CCoord, out hv_StartPose);

            HOperatorSet.SetCalibDataObservPoints(hv_CalibDataID, 0, 0, 1, hv_RCoord, hv_CCoord,
                                                  "all", hv_StartPose);
        }


        HOperatorSet.CalibrateCameras(hv_CalibDataID, out hv_Error);

        HOperatorSet.GetCalibData(hv_CalibDataID, "camera", 0, "params", out hv_interCamera);

        HOperatorSet.GetCalibData(hv_CalibDataID, "calib_obj_pose", (new HTuple(0)).TupleConcat(
                                      1), "pose", out hv_Pose);

        hv_cut = ((hv_Pose.TupleSelect(5))).TupleInt();

        hv_YX = 100;
        hv_YY = 100;

        HOperatorSet.ImagePointsToWorldPlane(hv_interCamera, hv_Pose, hv_YY, hv_YX, 1,
                                             out hv_X, out hv_Y);

        HOperatorSet.ImagePointsToWorldPlane(hv_interCamera, hv_Pose, hv_YY + 1, hv_YX,
                                             1, out hv_X1, out hv_Y1);

        HOperatorSet.ImagePointsToWorldPlane(hv_interCamera, hv_Pose, hv_YY, hv_YX + 1,
                                             1, out hv_X2, out hv_Y2);

        HOperatorSet.DistancePp(hv_Y, hv_X, hv_Y1, hv_X1, out hv_Distance1);

        HOperatorSet.DistancePp(hv_Y, hv_X, hv_Y2, hv_X2, out hv_Distance2);

        hv_distance = (hv_Distance1 + hv_Distance2) / 2;

        hv_OffsetX = (hv_CCoord.TupleSelect(24)) * hv_distance;
        hv_OffsetY = (hv_RCoord.TupleSelect(24)) * hv_distance;

        hv_PoseR = hv_Pose.Clone();
        if (hv_PoseR == null)
        {
            hv_PoseR = new HTuple();
        }
        hv_PoseR[5] = (hv_Pose.TupleSelect(5)) - hv_cut;

        HOperatorSet.SetOriginPose(hv_PoseR, -hv_OffsetX, -hv_OffsetY, 0, out hv_PoseNewOrigin);

        ho_Image.Dispose();
        ho_CaltabRegion.Dispose();

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

            // Local iconic variables

            HObject ho_Region, ho_ConnectedRegions;
            HObject ho_ConnectedRegions1, ho_SelectedRegions, ho_Region3;
            HObject ho_Region4, ho_Circle, ho_ImageReduced, ho_Region1;
            HObject ho_ConnectedRegions2, ho_SelectedRegions1, ho_RegionFillUp;
            HObject ho_Region2, ho_ConnectedRegions3, ho_SelectedRegions2;
            HObject ho_RegionTrans, ho_RegionDifference, ho_RegionErosion;
            HObject ho_ImageReduced1 = null, ho_ImageMean = null, ho_RegionDynThresh = null;
            HObject ho_RegionDynThresh1 = null, ho_ConnectedRegions4 = null;
            HObject ho_SelectedRegions3 = null;

            // Local control variables

            HTuple hv_n = null, hv_m = null;
            HTuple hv_Row = null, hv_Column = null, hv_Radius = null;
            HTuple hv_Number = null, hv_Area = new HTuple(), hv_Row1 = new HTuple();
            HTuple hv_Column1 = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Region3);
            HOperatorSet.GenEmptyObj(out ho_Region4);
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_Region2);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions3);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference);
            HOperatorSet.GenEmptyObj(out ho_RegionErosion);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_RegionDynThresh);
            HOperatorSet.GenEmptyObj(out ho_RegionDynThresh1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions4);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions3);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            try
            {
                hv_n = 0;
                hv_m = 0;
                ho_Region.Dispose();
                HOperatorSet.Threshold(Image, out ho_Region, 0, this.pzz);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_ConnectedRegions, out ho_ConnectedRegions1);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_SelectedRegions, "max_area",
                                            70);
                HOperatorSet.SmallestCircle(ho_SelectedRegions, out hv_Row, out hv_Column, out hv_Radius);
                ho_Region3.Dispose();
                HOperatorSet.GenRegionPoints(out ho_Region3, hv_Row, hv_Column);
                ho_Region4.Dispose();
                HOperatorSet.GenRegionPoints(out ho_Region4, hv_Row, hv_Column);
                ho_Circle.Dispose();
                HOperatorSet.GenCircle(out ho_Circle, hv_Row, hv_Column, hv_Radius);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Circle, out ho_ImageReduced);
                ho_Region1.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, thv, 255);
                ho_ConnectedRegions2.Dispose();
                HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions2);
                ho_SelectedRegions1.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions2, out ho_SelectedRegions1, "max_area",
                                            70);
                ho_RegionFillUp.Dispose();
                HOperatorSet.FillUp(ho_SelectedRegions1, out ho_RegionFillUp);
                ho_Region2.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region2, this.cd, 255);
                ho_ConnectedRegions3.Dispose();
                HOperatorSet.Connection(ho_Region2, out ho_ConnectedRegions3);
                ho_SelectedRegions2.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions3, out ho_SelectedRegions2, "max_area",
                                            70);
                ho_RegionTrans.Dispose();
                HOperatorSet.ShapeTrans(ho_SelectedRegions2, out ho_RegionTrans, "outer_circle");
                ho_RegionDifference.Dispose();
                HOperatorSet.Difference(ho_RegionFillUp, ho_RegionTrans, out ho_RegionDifference
                                        );
                ho_RegionErosion.Dispose();
                HOperatorSet.ErosionCircle(ho_RegionDifference, out ho_RegionErosion, this.kdz);
                HOperatorSet.CountObj(ho_RegionErosion, out hv_Number);
                if ((int)(new HTuple(hv_Number.TupleEqual(1))) != 0)
                {
                    ho_ImageReduced1.Dispose();
                    HOperatorSet.ReduceDomain(Image, ho_RegionErosion, out ho_ImageReduced1);
                    ho_ImageMean.Dispose();
                    HOperatorSet.MeanImage(ho_ImageReduced1, out ho_ImageMean, this.yd, this.yd);
                    ho_RegionDynThresh.Dispose();
                    HOperatorSet.DynThreshold(ho_ImageReduced1, ho_ImageMean, out ho_RegionDynThresh,
                                              this.bjz, "light");
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.Union2(ho_Region3, ho_RegionDynThresh, out ExpTmpOutVar_0);
                        ho_Region3.Dispose();
                        ho_Region3 = ExpTmpOutVar_0;
                    }
                    ho_RegionDynThresh1.Dispose();
                    HOperatorSet.DynThreshold(ho_ImageReduced1, ho_ImageMean, out ho_RegionDynThresh1,
                                              this.bjz, "dark");
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.Union2(ho_Region3, ho_RegionDynThresh1, out ExpTmpOutVar_0);
                        ho_Region3.Dispose();
                        ho_Region3 = ExpTmpOutVar_0;
                    }
                    ho_ConnectedRegions4.Dispose();
                    HOperatorSet.Connection(ho_Region3, out ho_ConnectedRegions4);
                    ho_SelectedRegions3.Dispose();
                    HOperatorSet.SelectShape(ho_ConnectedRegions4, out ho_SelectedRegions3, "area",
                                             "and", this.mj, 999999999);
                    HOperatorSet.AreaCenter(ho_SelectedRegions3, out hv_Area, out hv_Row1, out hv_Column1);
                    if ((int)(new HTuple((new HTuple(hv_Area.TupleLength())).TupleGreater(0))) != 0)
                    {
                        hv_m = hv_Area.TupleSum();
                        hv_n = hv_Area.TupleMax();
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_Region4, ho_SelectedRegions3, out ExpTmpOutVar_0);
                            ho_Region4.Dispose();
                            ho_Region4 = ExpTmpOutVar_0;
                        }
                    }
                    else
                    {
                        hv_m = 0;
                        hv_n = 0;
                    }
                }
                else
                {
                    hv_m = 99999;
                    hv_n = 99999;
                }
                //HOperatorSet.ClearShapeModel(hv_ModelID);
                HOperatorSet.Union1(ho_Region4, out RegionToDisp);

                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("缺陷总面积");
                hv_result = hv_result.TupleConcat(hv_m.D);
                hv_result = hv_result.TupleConcat("最大面积");
                hv_result = hv_result.TupleConcat(hv_n.D);
                result    = hv_result.Clone();
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions.Dispose();
                ho_Region3.Dispose();
                ho_Region4.Dispose();
                ho_Circle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_RegionFillUp.Dispose();
                ho_Region2.Dispose();
                ho_ConnectedRegions3.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionTrans.Dispose();
                ho_RegionDifference.Dispose();
                ho_RegionErosion.Dispose();
                ho_ImageReduced1.Dispose();
                ho_ImageMean.Dispose();
                ho_RegionDynThresh.Dispose();
                ho_RegionDynThresh1.Dispose();
                ho_ConnectedRegions4.Dispose();
                ho_SelectedRegions3.Dispose();
                algorithm.Region.Dispose();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                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_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions.Dispose();
                ho_Region3.Dispose();
                ho_Region4.Dispose();
                ho_Circle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_RegionFillUp.Dispose();
                ho_Region2.Dispose();
                ho_ConnectedRegions3.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionTrans.Dispose();
                ho_RegionDifference.Dispose();
                ho_RegionErosion.Dispose();
                ho_ImageReduced1.Dispose();
                ho_ImageMean.Dispose();
                ho_RegionDynThresh.Dispose();
                ho_RegionDynThresh1.Dispose();
                ho_ConnectedRegions4.Dispose();
                ho_SelectedRegions3.Dispose();
                algorithm.Region.Dispose();
            }
            finally
            {
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions.Dispose();
                ho_Region3.Dispose();
                ho_Region4.Dispose();
                ho_Circle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_RegionFillUp.Dispose();
                ho_Region2.Dispose();
                ho_ConnectedRegions3.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionTrans.Dispose();
                ho_RegionDifference.Dispose();
                ho_RegionErosion.Dispose();
                ho_ImageReduced1.Dispose();
                ho_ImageMean.Dispose();
                ho_RegionDynThresh.Dispose();
                ho_RegionDynThresh1.Dispose();
                ho_ConnectedRegions4.Dispose();
                ho_SelectedRegions3.Dispose();
                algorithm.Region.Dispose();
            }
        }
Example #14
0
        /// <summary>
        /// 设置字体大小
        /// </summary>
        /// <param name="hv_WindowHandle"></param>
        /// <param name="hv_Size"></param>
        /// <param name="hv_Font"></param>
        /// <param name="hv_Bold"></param>
        /// <param name="hv_Slant"></param>
        public void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font,
                                     HTuple hv_Bold, HTuple hv_Slant)
        {
            // Local iconic variables

            // Local control variables

            HTuple hv_OS = null, hv_Fonts = new HTuple();
            HTuple hv_Style = null, hv_Exception = new HTuple(), hv_AvailableFonts = null;
            HTuple hv_Fdx = null, hv_Indices = new HTuple();
            HTuple hv_Font_COPY_INP_TMP = hv_Font.Clone();
            HTuple hv_Size_COPY_INP_TMP = hv_Size.Clone();

            // Initialize local and output iconic variables
            //This procedure sets the text font of the current window with
            //the specified attributes.
            //
            //Input parameters:
            //WindowHandle: The graphics window for which the font will be set
            //Size: The font size. If Size=-1, the default of 16 is used.
            //Bold: If set to 'true', a bold font is used
            //Slant: If set to 'true', a slanted font is used
            //
            HOperatorSet.GetSystem("operating_system", out hv_OS);
            // dev_get_preferences(...); only in hdevelop
            // dev_set_preferences(...); only in hdevelop
            if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
                          new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0)
            {
                hv_Size_COPY_INP_TMP = 16;
            }
            if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
            {
                //Restore previous behaviour
                hv_Size_COPY_INP_TMP = ((1.13677 * hv_Size_COPY_INP_TMP)).TupleInt();
            }
            if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))) != 0)
            {
                hv_Fonts    = new HTuple();
                hv_Fonts[0] = "Courier";
                hv_Fonts[1] = "Courier 10 Pitch";
                hv_Fonts[2] = "Courier New";
                hv_Fonts[3] = "CourierNew";
            }
            else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0)
            {
                hv_Fonts    = new HTuple();
                hv_Fonts[0] = "Consolas";
                hv_Fonts[1] = "Menlo";
                hv_Fonts[2] = "Courier";
                hv_Fonts[3] = "Courier 10 Pitch";
                hv_Fonts[4] = "FreeMono";
            }
            else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
            {
                hv_Fonts    = new HTuple();
                hv_Fonts[0] = "Luxi Sans";
                hv_Fonts[1] = "DejaVu Sans";
                hv_Fonts[2] = "FreeSans";
                hv_Fonts[3] = "Arial";
            }
            else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
            {
                hv_Fonts    = new HTuple();
                hv_Fonts[0] = "Times New Roman";
                hv_Fonts[1] = "Luxi Serif";
                hv_Fonts[2] = "DejaVu Serif";
                hv_Fonts[3] = "FreeSerif";
                hv_Fonts[4] = "Utopia";
            }
            else
            {
                hv_Fonts = hv_Font_COPY_INP_TMP.Clone();
            }
            hv_Style = "";
            if ((int)(new HTuple(hv_Bold.TupleEqual("true"))) != 0)
            {
                hv_Style = hv_Style + "Bold";
            }
            else if ((int)(new HTuple(hv_Bold.TupleNotEqual("false"))) != 0)
            {
                hv_Exception = "Wrong value of control parameter Bold";
                throw new HalconException(hv_Exception);
            }
            if ((int)(new HTuple(hv_Slant.TupleEqual("true"))) != 0)
            {
                hv_Style = hv_Style + "Italic";
            }
            else if ((int)(new HTuple(hv_Slant.TupleNotEqual("false"))) != 0)
            {
                hv_Exception = "Wrong value of control parameter Slant";
                throw new HalconException(hv_Exception);
            }
            if ((int)(new HTuple(hv_Style.TupleEqual(""))) != 0)
            {
                hv_Style = "Normal";
            }
            HOperatorSet.QueryFont(hv_WindowHandle, out hv_AvailableFonts);
            hv_Font_COPY_INP_TMP = "";
            for (hv_Fdx = 0; (int)hv_Fdx <= (int)((new HTuple(hv_Fonts.TupleLength())) - 1); hv_Fdx = (int)hv_Fdx + 1)
            {
                hv_Indices = hv_AvailableFonts.TupleFind(hv_Fonts.TupleSelect(hv_Fdx));
                if ((int)(new HTuple((new HTuple(hv_Indices.TupleLength())).TupleGreater(0))) != 0)
                {
                    if ((int)(new HTuple(((hv_Indices.TupleSelect(0))).TupleGreaterEqual(0))) != 0)
                    {
                        hv_Font_COPY_INP_TMP = hv_Fonts.TupleSelect(hv_Fdx);
                        break;
                    }
                }
            }
            if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(""))) != 0)
            {
                throw new HalconException("Wrong value of control parameter Font");
            }
            hv_Font_COPY_INP_TMP = (((hv_Font_COPY_INP_TMP + "-") + hv_Style) + "-") + hv_Size_COPY_INP_TMP;
            HOperatorSet.SetFont(hv_WindowHandle, hv_Font_COPY_INP_TMP);
            // dev_set_preferences(...); only in hdevelop

            return;
        }
        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);
        }
    /// <summary>
    /// Evaluates the area covered by the calibration plate in the calibration
    /// image.
    /// </summary>
    public void eval_caltab_size(HObject ho_Image, 
                                HObject ho_Caltab, 
                                HObject ho_Marks,
                                out HTuple hv_SizeScore)
    {
        // Local iconic variables

          HObject ho_Region = null, ho_RegionUnion = null;

          // Local control variables

          HTuple hv_Width, hv_Height, hv_Number, hv_Row1 = new HTuple();
          HTuple hv_Column1 = new HTuple(), hv_Phi1 = new HTuple(), hv_Length1 = new HTuple();
          HTuple hv_Length2 = new HTuple(), hv_Area = new HTuple(), hv_Row = new HTuple();
          HTuple hv_Column = new HTuple(), hv_MinRatio, hv_MaxRatio;
          HTuple hv_Ratio;

          // Initialize local and output iconic variables
          HOperatorSet.GenEmptyObj(out ho_Region);
          HOperatorSet.GenEmptyObj(out ho_RegionUnion);

          hv_SizeScore = 0.0;
          HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
          HOperatorSet.CountObj(ho_Marks, out hv_Number);
          if ((int)(new HTuple(hv_Number.TupleGreaterEqual(4))) != 0)
          {
          //Best approach: Use the surrounding box of the marks as reference size
          ho_Region.Dispose();
          HOperatorSet.GenRegionContourXld(ho_Marks, out ho_Region, "filled");
          ho_RegionUnion.Dispose();
          HOperatorSet.Union1(ho_Region, out ho_RegionUnion);
          HOperatorSet.SmallestRectangle2(ho_RegionUnion, out hv_Row1, out hv_Column1,
              out hv_Phi1, out hv_Length1, out hv_Length2);
          hv_Area = (hv_Length2 * hv_Length1) * 4;
          }
          else
          {
          //If no marks could be found: use the caltab as reference size
          HOperatorSet.AreaCenter(ho_Caltab, out hv_Area, out hv_Row, out hv_Column);
          if ((int)((new HTuple(hv_Area.TupleEqual(0))).TupleOr(new HTuple(hv_Area.TupleEqual(
              new HTuple())))) != 0)
          {
              ho_Region.Dispose();
              ho_RegionUnion.Dispose();

              return;
          }
          }
          hv_MinRatio = 0.015;
          hv_MaxRatio = 0.075;
          hv_Ratio = (hv_Area.TupleReal()) / (hv_Width * hv_Height);
          if ((int)(new HTuple(hv_Ratio.TupleGreater(hv_MinRatio))) != 0)
          {
          hv_SizeScore = (hv_Ratio - hv_MinRatio) / (hv_MaxRatio - hv_MinRatio);
          hv_SizeScore = (((new HTuple(1.0)).TupleConcat(hv_SizeScore))).TupleMin();
          }
          ho_Region.Dispose();
          ho_RegionUnion.Dispose();

          return;
    }
Example #17
0
        //public void Count_Num(HObject ho_MixedImage1, out HObject ho_Contours, out HTuple hv_Number)
        //{

        //    // Local iconic variables
        //    HObject ho_Region, ho_RegionOpening, ho_ConnectedRegions;
        //    HObject ho_Rectangle1, ho_Rectangle2, ho_Rectangle3, ho_RegionUnion;
        //    HObject ho_ConnectedRegions1, ho_DestRegions, ho_RegionDifference;
        //    HObject ho_RegionUnion1, ho_ConnectedRegions2, ho_DestRegions1;
        //    HObject ho_RegionUnion2, ho_RegionIntersection, ho_ConnectedRegions3;
        //    HObject ho_SelectedRegions, ho_RegionUnion3;

        //    // Local control variables

        //    HTuple hv_Area = new HTuple(), hv_Row = new HTuple();
        //    HTuple hv_Column = new HTuple();
        //    // Initialize local and output iconic variables
        //    HOperatorSet.GenEmptyObj(out ho_Contours);
        //    HOperatorSet.GenEmptyObj(out ho_Region);
        //    HOperatorSet.GenEmptyObj(out ho_RegionOpening);
        //    HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
        //    HOperatorSet.GenEmptyObj(out ho_Rectangle1);
        //    HOperatorSet.GenEmptyObj(out ho_Rectangle2);
        //    HOperatorSet.GenEmptyObj(out ho_Rectangle3);
        //    HOperatorSet.GenEmptyObj(out ho_RegionUnion);
        //    HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
        //    HOperatorSet.GenEmptyObj(out ho_DestRegions);
        //    HOperatorSet.GenEmptyObj(out ho_RegionDifference);
        //    HOperatorSet.GenEmptyObj(out ho_RegionUnion1);
        //    HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2);
        //    HOperatorSet.GenEmptyObj(out ho_DestRegions1);
        //    HOperatorSet.GenEmptyObj(out ho_RegionUnion2);
        //    HOperatorSet.GenEmptyObj(out ho_RegionIntersection);
        //    HOperatorSet.GenEmptyObj(out ho_ConnectedRegions3);
        //    HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
        //    HOperatorSet.GenEmptyObj(out ho_RegionUnion3);
        //    //threshold (MixedImage1, Region, 0, 100)

        //    ho_Region.Dispose();
        //    HOperatorSet.FastThreshold(ho_MixedImage1, out ho_Region, 0, 80, 20);
        //    ho_RegionOpening.Dispose();
        //    HOperatorSet.OpeningRectangle1(ho_Region, out ho_RegionOpening, 100, 1);

        //    ho_ConnectedRegions.Dispose();
        //    HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
        //    ho_Rectangle1.Dispose();
        //    HOperatorSet.GenRectangle1(out ho_Rectangle1, 0, 0, 1000, 8192);
        //    ho_Rectangle2.Dispose();
        //    HOperatorSet.GenRectangle1(out ho_Rectangle2, 1000, 0, 2000, 8192);
        //    ho_Rectangle3.Dispose();
        //    HOperatorSet.GenRectangle1(out ho_Rectangle3, 2000, 0, 3000, 8192);


        //    ho_RegionUnion.Dispose();
        //    HOperatorSet.Union2(ho_Rectangle1, ho_Region, out ho_RegionUnion);
        //    ho_ConnectedRegions1.Dispose();
        //    HOperatorSet.Connection(ho_RegionUnion, out ho_ConnectedRegions1);
        //    ho_DestRegions.Dispose();
        //    HOperatorSet.SelectRegionPoint(ho_ConnectedRegions1, out ho_DestRegions, 500,
        //        4096);
        //    ho_RegionDifference.Dispose();
        //    HOperatorSet.Difference(ho_Rectangle2, ho_DestRegions, out ho_RegionDifference
        //        );

        //    ho_RegionUnion1.Dispose();
        //    HOperatorSet.Union2(ho_Rectangle3, ho_Region, out ho_RegionUnion1);
        //    ho_ConnectedRegions2.Dispose();
        //    HOperatorSet.Connection(ho_RegionUnion1, out ho_ConnectedRegions2);
        //    ho_DestRegions1.Dispose();
        //    HOperatorSet.SelectRegionPoint(ho_ConnectedRegions2, out ho_DestRegions1, 2500,
        //        4096);

        //    ho_RegionUnion2.Dispose();
        //    HOperatorSet.Union2(ho_DestRegions1, ho_RegionDifference, out ho_RegionUnion2
        //        );
        //    ho_RegionIntersection.Dispose();
        //    HOperatorSet.Intersection(ho_Region, ho_RegionUnion2, out ho_RegionIntersection
        //        );

        //    ho_ConnectedRegions3.Dispose();
        //    HOperatorSet.Connection(ho_RegionIntersection, out ho_ConnectedRegions3);

        //    ho_SelectedRegions.Dispose();
        //    HOperatorSet.SelectShape(ho_ConnectedRegions3, out ho_SelectedRegions, "area",
        //        "and", 1000, 1000000);

        //    ho_RegionUnion3.Dispose();
        //    HOperatorSet.Union1(ho_SelectedRegions, out ho_RegionUnion3);

        //    ho_Contours.Dispose();
        //    HOperatorSet.GenContourRegionXld(ho_RegionUnion3, out ho_Contours, "border");

        //    HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number);

        //    if ((int)(new HTuple(hv_Number.TupleEqual(1))) != 0)
        //    {
        //        HOperatorSet.AreaCenter(ho_RegionUnion3, out hv_Area, out hv_Row, out hv_Column);
        //        if ((int)(new HTuple(hv_Area.TupleEqual(0))) != 0)
        //        {
        //            hv_Number = 0;
        //        }

        //    }
        //    ho_Region.Dispose();
        //    ho_RegionOpening.Dispose();
        //    ho_ConnectedRegions.Dispose();
        //    ho_Rectangle1.Dispose();
        //    ho_Rectangle2.Dispose();
        //    ho_Rectangle3.Dispose();
        //    ho_RegionUnion.Dispose();
        //    ho_ConnectedRegions1.Dispose();
        //    ho_DestRegions.Dispose();
        //    ho_RegionDifference.Dispose();
        //    ho_RegionUnion1.Dispose();
        //    ho_ConnectedRegions2.Dispose();
        //    ho_DestRegions1.Dispose();
        //    ho_RegionUnion2.Dispose();
        //    ho_RegionIntersection.Dispose();
        //    ho_ConnectedRegions3.Dispose();
        //    ho_SelectedRegions.Dispose();
        //    ho_RegionUnion3.Dispose();

        //    return;
        //}

        public void Count_Num(HObject ho_MixedImage1, out HObject ho_Contours, out HTuple hv_Number)
        {
            // Local iconic variables

            HObject ho_Region, ho_Region1, ho_ConnectedRegions;
            HObject ho_Rectangle1, ho_Rectangle2, ho_Rectangle3, ho_RegionUnion;
            HObject ho_ConnectedRegions1, ho_DestRegions, ho_RegionDifference;
            HObject ho_RegionUnion1, ho_ConnectedRegions2, ho_DestRegions1;
            HObject ho_RegionUnion2, ho_RegionIntersection, ho_ConnectedRegions3;
            HObject ho_SelectedRegions, ho_RegionUnion3;

            // Local control variables

            HTuple hv_Area = new HTuple(), hv_Row = new HTuple();
            HTuple hv_Column = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Contours);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_Rectangle2);
            HOperatorSet.GenEmptyObj(out ho_Rectangle3);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_DestRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_DestRegions1);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion2);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions3);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion3);
            //threshold (MixedImage1, Region, 0, 100)

            ho_Region1.Dispose();
            HOperatorSet.FastThreshold(ho_MixedImage1, out ho_Region1, 0, 80, 20);

            ho_Region.Dispose();
            HOperatorSet.OpeningRectangle1(ho_Region1, out ho_Region, 150, 1);


            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
            ho_Rectangle1.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle1, 0, 0, 1000, 8192);
            ho_Rectangle2.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle2, 1000, 0, 2000, 8192);
            ho_Rectangle3.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle3, 2000, 0, 3000, 8192);


            ho_RegionUnion.Dispose();
            HOperatorSet.Union2(ho_Rectangle1, ho_Region, out ho_RegionUnion);
            ho_ConnectedRegions1.Dispose();
            HOperatorSet.Connection(ho_RegionUnion, out ho_ConnectedRegions1);
            ho_DestRegions.Dispose();
            HOperatorSet.SelectRegionPoint(ho_ConnectedRegions1, out ho_DestRegions, 500,
                                           4096);
            ho_RegionDifference.Dispose();
            HOperatorSet.Difference(ho_Rectangle2, ho_DestRegions, out ho_RegionDifference
                                    );

            ho_RegionUnion1.Dispose();
            HOperatorSet.Union2(ho_Rectangle3, ho_Region, out ho_RegionUnion1);
            ho_ConnectedRegions2.Dispose();
            HOperatorSet.Connection(ho_RegionUnion1, out ho_ConnectedRegions2);
            ho_DestRegions1.Dispose();
            HOperatorSet.SelectRegionPoint(ho_ConnectedRegions2, out ho_DestRegions1, 2500,
                                           4096);

            ho_RegionUnion2.Dispose();
            HOperatorSet.Union2(ho_DestRegions1, ho_RegionDifference, out ho_RegionUnion2
                                );
            ho_RegionIntersection.Dispose();
            HOperatorSet.Intersection(ho_Region, ho_RegionUnion2, out ho_RegionIntersection
                                      );

            ho_ConnectedRegions3.Dispose();
            HOperatorSet.Connection(ho_RegionIntersection, out ho_ConnectedRegions3);

            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions3, out ho_SelectedRegions, "area",
                                     "and", 1000, 1000000);

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

            ho_Contours.Dispose();
            HOperatorSet.GenContourRegionXld(ho_RegionUnion3, out ho_Contours, "border");

            HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number);

            if ((int)(new HTuple(hv_Number.TupleEqual(1))) != 0)
            {
                HOperatorSet.AreaCenter(ho_RegionUnion3, out hv_Area, out hv_Row, out hv_Column);
                if ((int)(new HTuple(hv_Area.TupleEqual(0))) != 0)
                {
                    hv_Number = 0;
                }
            }

            ho_Region.Dispose();
            ho_Region1.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_Rectangle1.Dispose();
            ho_Rectangle2.Dispose();
            ho_Rectangle3.Dispose();
            ho_RegionUnion.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_DestRegions.Dispose();
            ho_RegionDifference.Dispose();
            ho_RegionUnion1.Dispose();
            ho_ConnectedRegions2.Dispose();
            ho_DestRegions1.Dispose();
            ho_RegionUnion2.Dispose();
            ho_RegionIntersection.Dispose();
            ho_ConnectedRegions3.Dispose();
            ho_SelectedRegions.Dispose();
            ho_RegionUnion3.Dispose();

            return;
        }
Example #18
0
        public MeasureResult Action()
        {
            #region 輸出結果
            DistanceResult mResult = null;
            #endregion
            // Local iconic variables

            HObject ho_ROI_Rec = null;
            HObject ho_ROI_Image = null, ho_ROI_Edges = null, ho_ROI_ContoursSplit = null;
            HObject ho_ROI_SortedContours = null, ho_FirstLine = null, ho_SecondLine = 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_B_Row, hv_STD_B_Col;
            HTuple hv_STD_B_V_Row, hv_STD_B_V_Col, hv_B_X, hv_B_Y;
            HTuple hv_B_Pos_Row, hv_B_Pos_Col, hv_ROI_W = new HTuple();
            HTuple hv_ROI_H = new HTuple(), hv_low = new HTuple(), hv_high = new HTuple();
            HTuple hv_algo = new HTuple(), hv_Number = new HTuple(), hv_FirstRowBegin = new HTuple();
            HTuple hv_FirstColBegin = new HTuple(), hv_FirstRowEnd = new HTuple();
            HTuple hv_FirstColEnd = new HTuple(), hv_FirstNr = new HTuple();
            HTuple hv_FirstNc = new HTuple(), hv_FirstDist = new HTuple();
            HTuple hv_SecondRowBegin = new HTuple(), hv_SecondColBegin = new HTuple();
            HTuple hv_SecondRowEnd = new HTuple(), hv_SecondColEnd = new HTuple();
            HTuple hv_SecondNr = new HTuple(), hv_SecondNc = new HTuple();
            HTuple hv_SecondDist = new HTuple(), hv_minDist = new HTuple();
            HTuple hv_maxDist = new HTuple(), hv_STD_Mark_B_C_Row = new HTuple();
            HTuple hv_STD_Mark_B_C_Col = new HTuple(), hv_STD_Mark_B_Start_Row = new HTuple();
            HTuple hv_STD_Mark_B_Start_Col = new HTuple(), hv_STD_Mark_B_End_Row = new HTuple();
            HTuple hv_STD_Mark_B_End_Col = new HTuple(), hv_STD_Mark_B_C_V_Row = new HTuple();
            HTuple hv_STD_Mark_B_C_V_Col = new HTuple(), hv_Mark_B_C_X = new HTuple();
            HTuple hv_Mark_B_C_Y = new HTuple(), hv_Mark_B_C_Pos_Row = new HTuple();
            HTuple hv_Mark_B_C_Pos_Col = new HTuple(), hv_STD_Mark_B_Start_V_Row = new HTuple();
            HTuple hv_STD_Mark_B_Start_V_Col = new HTuple(), hv_Mark_B_Start_X = new HTuple();
            HTuple hv_Mark_B_Start_Y = new HTuple(), hv_Mark_B_Start_Pos_Row = new HTuple();
            HTuple hv_Mark_B_Start_Pos_Col = new HTuple(), hv_STD_Mark_B_End_V_Row = new HTuple();
            HTuple hv_STD_Mark_B_End_V_Col = new HTuple(), hv_Mark_B_End_X = new HTuple();
            HTuple hv_Mark_B_End_Y = new HTuple(), hv_Mark_B_End_Pos_Row = new HTuple();
            HTuple hv_Mark_B_End_Pos_Col = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ROI_Rec);
            HOperatorSet.GenEmptyObj(out ho_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_ROI_Edges);
            HOperatorSet.GenEmptyObj(out ho_ROI_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_ROI_SortedContours);
            HOperatorSet.GenEmptyObj(out ho_FirstLine);
            HOperatorSet.GenEmptyObj(out ho_SecondLine);

            //Measure: SDMS_B
            //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 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());
            }
            //*****B
            //STD B_ 位置
            hv_STD_B_Row = 743;
            hv_STD_B_Col = 1118;

            //STD 向量 STD_B_
            hv_STD_B_V_Row = hv_STD_B_Row - hv_STD_Row;
            hv_STD_B_V_Col = hv_STD_B_Col - hv_STD_Col;

            //B_X, B_Y 分量
            hv_B_X = (hv_STD_B_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_B_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                ));
            hv_B_Y = (hv_STD_B_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_B_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                ));

            //目前圖形 B_ 位置
            hv_B_Pos_Row = (hv_STD_Row + hv_B_Y) + hv_OffsetRow;
            hv_B_Pos_Col = (hv_STD_Col + hv_B_X) + hv_OffsetCol;

            hv_ROI_W = 150;
            hv_ROI_H = 600;
            ho_ROI_Rec.Dispose();
            HOperatorSet.GenRectangle2(out ho_ROI_Rec, hv_B_Pos_Row, hv_B_Pos_Col, hv_Img_Rotate_Angle,
                hv_ROI_W, hv_ROI_H);
            //stop ()
            ho_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_ROI_Rec, out ho_ROI_Image);
            //corner detect
            hv_low = 130;
            hv_high = 180;
            ho_ROI_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_ROI_Image, out ho_ROI_Edges, "lanser2", 0.3, hv_low,
                hv_high);
            //stop ()
            hv_algo = "lines";
            ho_ROI_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_ROI_Edges, out ho_ROI_ContoursSplit, hv_algo,
                9, 4, 2);
            ho_ROI_SortedContours.Dispose();
            HOperatorSet.SortContoursXld(ho_ROI_ContoursSplit, out ho_ROI_SortedContours,
                "upper_left", "true", "row");
            HOperatorSet.CountObj(ho_ROI_SortedContours, out hv_Number);
            if (HDevWindowStack.IsOpen())
            {
                //dev_display (ROI_SortedContours)
            }
            //兩線段
            if ((int)(new HTuple(hv_Number.TupleEqual(2))) != 0)
            {
                ho_FirstLine.Dispose();
                HOperatorSet.SelectObj(ho_ROI_SortedContours, out ho_FirstLine, 1);
                ho_SecondLine.Dispose();
                HOperatorSet.SelectObj(ho_ROI_SortedContours, out ho_SecondLine, 2);
                HOperatorSet.FitLineContourXld(ho_FirstLine, "tukey", -1, 0, 5, 2, out hv_FirstRowBegin,
                    out hv_FirstColBegin, out hv_FirstRowEnd, out hv_FirstColEnd, out hv_FirstNr,
                    out hv_FirstNc, out hv_FirstDist);
                HOperatorSet.FitLineContourXld(ho_SecondLine, "tukey", -1, 0, 5, 2, out hv_SecondRowBegin,
                    out hv_SecondColBegin, out hv_SecondRowEnd, out hv_SecondColEnd, out hv_SecondNr,
                    out hv_SecondNc, out hv_SecondDist);

                //line to line
                //取兩線段的最小距離
                HOperatorSet.DistanceSs(hv_FirstRowBegin, hv_FirstColBegin, hv_FirstRowEnd,
                    hv_FirstColEnd, hv_SecondRowBegin, hv_SecondColBegin, hv_SecondRowEnd,
                    hv_SecondColEnd, out hv_minDist, out hv_maxDist);

                mResult = new DistanceResult()
                {
                    FirstRowBegin = new HTuple(hv_FirstRowBegin),
                    FirstColBegin = new HTuple(hv_FirstColBegin),
                    FirstRowEnd = new HTuple(hv_FirstRowEnd),
                    FirstColEnd = new HTuple(hv_FirstColEnd),
                    SecondRowBegin = new HTuple(hv_SecondRowBegin),
                    SecondColBegin = new HTuple(hv_SecondColBegin),
                    SecondRowEnd = new HTuple(hv_SecondRowEnd),
                    SecondColEnd = new HTuple(hv_SecondColEnd),
                    Angle = new HTuple(this.hv_AllModelAngle),
                    Distance = new HTuple(hv_minDist),
                    Direction = LineDirection.Vertical,
                };

            }

            //*****B End

            ho_ROI_Rec.Dispose();
            ho_ROI_Image.Dispose();
            ho_ROI_Edges.Dispose();
            ho_ROI_ContoursSplit.Dispose();
            ho_ROI_SortedContours.Dispose();
            ho_FirstLine.Dispose();
            ho_SecondLine.Dispose();

            return mResult;
        }
        public void do_inspect1(HObject ho_Image, HTuple hv_RightModelPath, HTuple hv_TestModelPath,
                                out HTuple hv_outport_result)
        {
            // Local iconic variables

            HObject ho_UpRectangle = null, ho_UpImageReduced = null;
            HObject ho_DownRectangle = null, ho_DownImageReduced = null;
            HObject ho_UpSuccessRegions = null, ho_UpSuccessConnectedRegions = null;
            HObject ho_UpSuccessSelectedRegions = null, ho_UpSuccessSelectedRegions1 = null;
            HObject ho_UpFailRegions = null, ho_UpFailConnectedRegions = null;
            HObject ho_UpFailSelectedRegions = null, ho_UpFailSelectedRegions1 = null;
            HObject ho_DownSuccessRegions = null, ho_DownSuccessConnectedRegions = null;
            HObject ho_DownSuccessSelectedRegions = null, ho_DownSuccessSelectedRegions1 = null;
            HObject ho_DownFailRegions = null, ho_DownFailConnectedRegions = null;
            HObject ho_DownFailSelectedRegions = null, ho_DownFailSelectedRegions1 = null;


            // Local control variables

            HTuple hv_Width, hv_Height, hv_RightModelID;
            HTuple hv_RightModelRow, hv_RightModelColumn, hv_RightModelAngle;
            HTuple hv_RightModelScore, hv_TestModelID = new HTuple();
            HTuple hv_UpTestRow = new HTuple(), hv_UpTestColumn = new HTuple();
            HTuple hv_UpTestAngle = new HTuple(), hv_UpTestScore = new HTuple();
            HTuple hv_DownTestRow = new HTuple(), hv_DownTestColumn = new HTuple();
            HTuple hv_DownTestAngle = new HTuple(), hv_DownTestScore = new HTuple();
            HTuple hv_upStartStateArea = new HTuple(), hv_upStartStateRow = new HTuple();
            HTuple hv_upStartStateColumn = new HTuple(), hv_UpSuccessArea = new HTuple();
            HTuple hv_UpSuccessRow = new HTuple(), hv_UpSuccessColumn = new HTuple();
            HTuple hv_UpFailArea = new HTuple(), hv_UpFailRow = new HTuple();
            HTuple hv_UpFailColumn = new HTuple(), hv_downStartStateArea = new HTuple();
            HTuple hv_downStartStateRow = new HTuple(), hv_downStartStateColumn = new HTuple();
            HTuple hv_DownSuccessArea = new HTuple(), hv_DownSuccessRow = new HTuple();
            HTuple hv_DownSuccessColumn = new HTuple(), hv_DownFailArea = new HTuple();
            HTuple hv_DownFailRow = new HTuple(), hv_DownFailColumn = new HTuple();
            HTuple hv_outport_upresult = new HTuple(), hv_outport_downresult = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_UpRectangle);
            HOperatorSet.GenEmptyObj(out ho_UpImageReduced);
            HOperatorSet.GenEmptyObj(out ho_DownRectangle);
            HOperatorSet.GenEmptyObj(out ho_DownImageReduced);
            HOperatorSet.GenEmptyObj(out ho_UpSuccessRegions);
            HOperatorSet.GenEmptyObj(out ho_UpSuccessConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_UpSuccessSelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_UpSuccessSelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_UpFailRegions);
            HOperatorSet.GenEmptyObj(out ho_UpFailConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_UpFailSelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_UpFailSelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_DownSuccessRegions);
            HOperatorSet.GenEmptyObj(out ho_DownSuccessConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_DownSuccessSelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_DownSuccessSelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_DownFailRegions);
            HOperatorSet.GenEmptyObj(out ho_DownFailConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_DownFailSelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_DownFailSelectedRegions1);

            hv_outport_result = new HTuple();
            //read_image (Image, fileName)
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            HOperatorSet.ReadNccModel(hv_RightModelPath, out hv_RightModelID);
            HOperatorSet.FindNccModel(ho_Image, hv_RightModelID, 0, 0, 0.5, 1, 0.5, "true",
                                      0, out hv_RightModelRow, out hv_RightModelColumn, out hv_RightModelAngle,
                                      out hv_RightModelScore);
            if ((int)((new HTuple((new HTuple(hv_RightModelScore.TupleLength())).TupleEqual(
                                      0))).TupleOr(new HTuple(((hv_RightModelScore.TupleSelect(0))).TupleLess(0.75)))) != 0)
            {
                //û�ҵ���λ�����Ҳ���԰�ť��
                hv_outport_result    = new HTuple();
                hv_outport_result[0] = 4;
                hv_outport_result[1] = 4;
            }
            else
            {
                ho_UpRectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_UpRectangle, (hv_RightModelRow.TupleSelect(
                                                                    0)) - 305, (hv_RightModelColumn.TupleSelect(0)) - 815, (hv_RightModelRow.TupleSelect(
                                                                                                                                0)) - 50, (hv_RightModelColumn.TupleSelect(0)) - 70);
                ho_UpImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_UpRectangle, out ho_UpImageReduced);
                ho_DownRectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_DownRectangle, (hv_RightModelRow.TupleSelect(
                                                                      0)) - 30, (hv_RightModelColumn.TupleSelect(0)) - 815, (hv_RightModelRow.TupleSelect(
                                                                                                                                 0)) + 230, (hv_RightModelColumn.TupleSelect(0)) - 70);
                ho_DownImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_DownRectangle, out ho_DownImageReduced
                                          );

                HOperatorSet.ReadNccModel(hv_TestModelPath, out hv_TestModelID);
                HOperatorSet.FindNccModel(ho_UpImageReduced, hv_TestModelID, 0, 0, 0.5, 1,
                                          0.5, "true", 0, out hv_UpTestRow, out hv_UpTestColumn, out hv_UpTestAngle,
                                          out hv_UpTestScore);
                HOperatorSet.FindNccModel(ho_DownImageReduced, hv_TestModelID, 0, 0, 0.5, 1,
                                          0.5, "true", 0, out hv_DownTestRow, out hv_DownTestColumn, out hv_DownTestAngle,
                                          out hv_DownTestScore);

                ho_UpSuccessRegions.Dispose();
                HOperatorSet.Threshold(ho_UpImageReduced, out ho_UpSuccessRegions, 0, 2);
                HOperatorSet.AreaCenter(ho_UpSuccessRegions, out hv_upStartStateArea, out hv_upStartStateRow,
                                        out hv_upStartStateColumn);

                ho_UpSuccessConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_UpSuccessRegions, out ho_UpSuccessConnectedRegions
                                        );
                ho_UpSuccessSelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_UpSuccessConnectedRegions, out ho_UpSuccessSelectedRegions,
                                         "rectangularity", "and", 0.8, 1);
                ho_UpSuccessSelectedRegions1.Dispose();
                HOperatorSet.SelectShape(ho_UpSuccessSelectedRegions, out ho_UpSuccessSelectedRegions1,
                                         "area", "and", 10000, 158720);
                HOperatorSet.AreaCenter(ho_UpSuccessSelectedRegions1, out hv_UpSuccessArea,
                                        out hv_UpSuccessRow, out hv_UpSuccessColumn);

                ho_UpFailRegions.Dispose();
                HOperatorSet.Threshold(ho_UpImageReduced, out ho_UpFailRegions, 138, 140);
                ho_UpFailConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_UpFailRegions, out ho_UpFailConnectedRegions);
                ho_UpFailSelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_UpFailConnectedRegions, out ho_UpFailSelectedRegions,
                                         "rectangularity", "and", 0.8, 1);
                ho_UpFailSelectedRegions1.Dispose();
                HOperatorSet.SelectShape(ho_UpFailSelectedRegions, out ho_UpFailSelectedRegions1,
                                         "area", "and", 10000, 158720);
                HOperatorSet.AreaCenter(ho_UpFailSelectedRegions1, out hv_UpFailArea, out hv_UpFailRow,
                                        out hv_UpFailColumn);



                ho_DownSuccessRegions.Dispose();
                HOperatorSet.Threshold(ho_DownImageReduced, out ho_DownSuccessRegions, 0, 2);
                HOperatorSet.AreaCenter(ho_DownSuccessRegions, out hv_downStartStateArea, out hv_downStartStateRow,
                                        out hv_downStartStateColumn);

                ho_DownSuccessConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_DownSuccessRegions, out ho_DownSuccessConnectedRegions
                                        );
                ho_DownSuccessSelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_DownSuccessConnectedRegions, out ho_DownSuccessSelectedRegions,
                                         "rectangularity", "and", 0.8, 1);
                ho_DownSuccessSelectedRegions1.Dispose();
                HOperatorSet.SelectShape(ho_DownSuccessSelectedRegions, out ho_DownSuccessSelectedRegions1,
                                         "area", "and", 10000, 158720);
                HOperatorSet.AreaCenter(ho_DownSuccessSelectedRegions1, out hv_DownSuccessArea,
                                        out hv_DownSuccessRow, out hv_DownSuccessColumn);

                ho_DownFailRegions.Dispose();
                HOperatorSet.Threshold(ho_DownImageReduced, out ho_DownFailRegions, 138, 140);
                ho_DownFailConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_DownFailRegions, out ho_DownFailConnectedRegions
                                        );
                ho_DownFailSelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_DownFailConnectedRegions, out ho_DownFailSelectedRegions,
                                         "rectangularity", "and", 0.8, 1);
                ho_DownFailSelectedRegions1.Dispose();
                HOperatorSet.SelectShape(ho_DownFailSelectedRegions, out ho_DownFailSelectedRegions1,
                                         "area", "and", 10000, 158720);
                HOperatorSet.AreaCenter(ho_DownFailSelectedRegions1, out hv_DownFailArea, out hv_DownFailRow,
                                        out hv_DownFailColumn);

                if ((int)((new HTuple((new HTuple(hv_UpTestScore.TupleLength())).TupleEqual(
                                          0))).TupleOr(new HTuple(((hv_UpTestScore.TupleSelect(0))).TupleLess(0.75)))) != 0)
                {
                    if ((int)((new HTuple((new HTuple(hv_upStartStateArea.TupleLength())).TupleEqual(
                                              0))).TupleOr(new HTuple(hv_upStartStateArea.TupleEqual(0)))) != 0)
                    {
                        //��ʼ״̬
                        hv_outport_upresult = 0;
                    }
                    else
                    {
                        //������
                        hv_outport_upresult = 1;
                    }
                }
                else
                {
                    if ((int)(new HTuple((new HTuple(hv_UpSuccessArea.TupleLength())).TupleGreater(
                                             0))) != 0)
                    {
                        //�Ͽ���Գɹ�
                        hv_outport_upresult = 2;
                    }
                    else if ((int)(new HTuple((new HTuple(hv_UpFailArea.TupleLength()
                                                          )).TupleGreater(0))) != 0)
                    {
                        //�Ͽ����ʧ��
                        hv_outport_upresult = 3;
                    }
                    else
                    {
                        //�ҵ����Խ�����ʶ����֪���Dz��Գɹ����Dz���ʧ��
                        hv_outport_upresult = 4;
                    }
                }

                if ((int)((new HTuple((new HTuple(hv_DownTestScore.TupleLength())).TupleEqual(
                                          0))).TupleOr(new HTuple(((hv_DownTestScore.TupleSelect(0))).TupleLess(0.75)))) != 0)
                {
                    //������

                    if ((int)((new HTuple((new HTuple(hv_downStartStateArea.TupleLength())).TupleEqual(
                                              0))).TupleOr(new HTuple(hv_downStartStateArea.TupleEqual(0)))) != 0)
                    {
                        //��ʼ״̬
                        hv_outport_downresult = 0;
                    }
                    else
                    {
                        //������
                        hv_outport_downresult = 1;
                    }
                }
                else
                {
                    if ((int)(new HTuple((new HTuple(hv_DownSuccessArea.TupleLength())).TupleGreater(
                                             0))) != 0)
                    {
                        //�¿���Գɹ�
                        hv_outport_downresult = 2;
                    }
                    else if ((int)(new HTuple((new HTuple(hv_DownFailArea.TupleLength()
                                                          )).TupleGreater(0))) != 0)
                    {
                        //�¿����ʧ��
                        hv_outport_downresult = 3;
                    }
                    else
                    {
                        //�ҵ����Խ�����ʶ����֪���Dz��Գɹ����Dz���ʧ��
                        hv_outport_downresult = 4;
                    }
                }
                hv_outport_result[0] = hv_outport_upresult.TupleSelect(0);
                hv_outport_result[1] = hv_outport_downresult.TupleSelect(0);
                HOperatorSet.ClearNccModel(hv_TestModelID);
            }
            HOperatorSet.ClearNccModel(hv_RightModelID);
            ho_UpRectangle.Dispose();
            ho_UpImageReduced.Dispose();
            ho_DownRectangle.Dispose();
            ho_DownImageReduced.Dispose();
            ho_UpSuccessRegions.Dispose();
            ho_UpSuccessConnectedRegions.Dispose();
            ho_UpSuccessSelectedRegions.Dispose();
            ho_UpSuccessSelectedRegions1.Dispose();
            ho_UpFailRegions.Dispose();
            ho_UpFailConnectedRegions.Dispose();
            ho_UpFailSelectedRegions.Dispose();
            ho_UpFailSelectedRegions1.Dispose();
            ho_DownSuccessRegions.Dispose();
            ho_DownSuccessConnectedRegions.Dispose();
            ho_DownSuccessSelectedRegions.Dispose();
            ho_DownSuccessSelectedRegions1.Dispose();
            ho_DownFailRegions.Dispose();
            ho_DownFailConnectedRegions.Dispose();
            ho_DownFailSelectedRegions.Dispose();
            ho_DownFailSelectedRegions1.Dispose();

            return;
        }
        private void action()
        {
            HObject ho_Rectangle, ho_ImageReduced, ho_ConnectedRegions2, ho_ObjectSelected3, ho_cl;
            HObject ho_Regions, ho_ObjectSelected, ho_RegionFillUp;
            HObject ho_Contours, ho_Rectangle1, ho_Rectangle3, ho_RegionDifference;
            HObject ho_ImageReduced1, ho_Border, ho_SelectedContours = null;
            HObject ho_ObjectSelected1 = null, ho_ObjectSelected2 = null;
            HObject ho_ContoursSplit1 = null, ho_RegionLines = null, ho_Region = null;
            HObject ho_RegionIntersection = null, ho_ConnectedRegions = null;
            HObject ho_Circle = null, ho_RegionLines1 = null, ho_Region1 = null;
            HObject ho_RegionIntersection1 = null, ho_ConnectedRegions1 = null;
            HObject ho_Circle1 = null;

            // Local control variables


            HTuple hv_Row4 = null;
            HTuple hv_Column4 = null, hv_Phi2 = null, hv_Length12 = null;
            HTuple hv_Length22 = null, hv_Area2 = null, hv_Row5 = null;
            HTuple hv_Column5 = null, hv_p = null, hv_fx = new HTuple();
            HTuple hv_cz = new HTuple(), hv_dz = new HTuple(), hv_lll = null;
            HTuple hv_Mean = null, hv_Deviation1 = null, hv_Length8 = null;
            HTuple hv_Number = new HTuple(), hv_Rowo1 = new HTuple();
            HTuple hv_Colo1 = new HTuple(), hv_Rowo2 = new HTuple();
            HTuple hv_Colo2 = new HTuple(), hv_as = new HTuple(), hv_ac = new HTuple();
            HTuple hv_b = new HTuple(), hv_cs = new HTuple(), hv_cc = new HTuple();
            HTuple hv_ds1 = new HTuple(), hv_ds2 = new HTuple(), hv_dc1 = new HTuple();
            HTuple hv_dc2 = new HTuple(), hv_Length3 = new HTuple();
            HTuple hv_dd = new HTuple(), hv_Floor = new HTuple(), hv_Length4 = new HTuple();
            HTuple hv_Sequence1 = new HTuple(), hv_Length5 = new HTuple();
            HTuple hv_Sequence = new HTuple(), hv_Sequence2 = new HTuple();
            HTuple hv_Reduced1 = new HTuple(), hv_Reduced2 = new HTuple();
            HTuple hv_Reduced3 = new HTuple(), hv_Reduced4 = new HTuple();
            HTuple hv_d1 = new HTuple(), hv_Length6 = new HTuple();
            HTuple hv_Sequence3 = new HTuple(), hv_Area = new HTuple();
            HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
            HTuple hv_Selectedc = new HTuple(), hv_Selectedr = new HTuple();
            HTuple hv_Newtuple = new HTuple(), hv_d2 = new HTuple();
            HTuple hv_Length7 = new HTuple(), hv_Sequence4 = new HTuple();
            HTuple hv_Area1 = new HTuple(), hv_Row3 = new HTuple();
            HTuple hv_Column3 = new HTuple(), hv_Selectedc1 = new HTuple();
            HTuple hv_Selectedr1 = new HTuple(), hv_Newtuple5 = new HTuple();
            HTuple hv_Selectedds1 = new HTuple(), hv_Selecteddc1 = new HTuple();
            HTuple hv_Selectedds2 = new HTuple(), hv_Selecteddc2 = new HTuple();
            HTuple hv_hs1 = new HTuple(), hv_hs1r = new HTuple(), hv_hs1c = new HTuple();
            HTuple hv_hs2 = new HTuple(), hv_hs2r = new HTuple(), hv_hs2c = new HTuple();
            HTuple hv_hc1 = new HTuple(), hv_hc2 = new HTuple(), hv_jdr = new HTuple();
            HTuple hv_jdc = new HTuple(), hv_jds = new HTuple(), hv_jdC = new HTuple();
            HTuple hv_jxr = new HTuple(), hv_jxc = new HTuple(), hv_jxs = new HTuple();
            HTuple hv_jxC = new HTuple(), hv_ydr = new HTuple(), hv_ydc = new HTuple();
            HTuple hv_yds = new HTuple(), hv_ydC = new HTuple(), hv_yxr = new HTuple();
            HTuple hv_yxc = new HTuple(), hv_yxs = new HTuple(), hv_yxC = new HTuple();
            HTuple hv_jin = new HTuple(), hv_yu = new HTuple(), hv_jp = new HTuple(), hv_Indices = new HTuple();
            HTuple hv_yp = new HTuple(), hv_j = new HTuple(), hv_luoju1 = new HTuple(), hv_Area3 = new HTuple(), hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_luoju2 = new HTuple(), hv_dajing = new HTuple();
            HTuple hv_xiaojing = new HTuple(), hv_changdu = new HTuple();
            HTuple hv_tiaoshu = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_Contours);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_Rectangle3);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Border);
            HOperatorSet.GenEmptyObj(out ho_SelectedContours);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected1);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected2);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplit1);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected3);
            HOperatorSet.GenEmptyObj(out ho_cl);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            try
            {
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_centerRowm, hv_centerColumnm, hv_Phim, hv_Length1m,
                                           hv_Length2m);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced);
                ho_Regions.Dispose();
                HOperatorSet.AutoThreshold(ho_ImageReduced, out ho_Regions, 3);

                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_Regions, out ho_ObjectSelected, 1);
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_Regions, out ho_ObjectSelected, 1);
                ho_ConnectedRegions2.Dispose();
                HOperatorSet.Connection(ho_ObjectSelected, out ho_ConnectedRegions2);
                HOperatorSet.AreaCenter(ho_ConnectedRegions2, out hv_Area3, out hv_Row1, out hv_Column1);
                HOperatorSet.TupleFind(hv_Area3, hv_Area3.TupleMax(), out hv_Indices);
                ho_ObjectSelected3.Dispose();
                HOperatorSet.SelectObj(ho_ConnectedRegions2, out ho_ObjectSelected3, hv_Indices + 1);
                ho_cl.Dispose();
                HOperatorSet.ClosingCircle(ho_ObjectSelected3, out ho_cl, 3.5);
                ho_RegionFillUp.Dispose();
                HOperatorSet.FillUp(ho_ObjectSelected3, out ho_RegionFillUp);
                ho_Contours.Dispose();
                HOperatorSet.GenContourRegionXld(ho_RegionFillUp, out ho_Contours, "border");
                HOperatorSet.SmallestRectangle2Xld(ho_Contours, out hv_Row4, out hv_Column4,
                                                   out hv_Phi2, out hv_Length12, out hv_Length22);
                HOperatorSet.AreaCenter(ho_RegionFillUp, out hv_Area2, out hv_Row5, out hv_Column5);
                hv_p = ((hv_Phi2 - hv_Phim)).TupleAbs();
                while ((int)(new HTuple(hv_p.TupleGreater((new HTuple(90)).TupleRad()))) != 0)
                {
                    hv_p = ((hv_p - ((new HTuple(180)).TupleRad()))).TupleAbs();
                }
                if ((int)(new HTuple(hv_p.TupleGreater((new HTuple(45)).TupleRad()))) != 0)
                {
                    hv_fx = hv_Phi2.Clone();
                    hv_cz = hv_Length12.Clone();
                    hv_dz = hv_Length22.Clone();
                }
                else
                {
                    hv_fx = hv_Phi2 + ((new HTuple(90)).TupleRad());
                    hv_cz = hv_Length22.Clone();
                    hv_dz = hv_Length12.Clone();
                }
                hv_lll = ((((hv_cz * hv_dz) * 4) - hv_Area2) / 2) / hv_cz;
                ho_Rectangle1.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row4, hv_Column4, hv_fx, hv_cz,
                                           hv_dz + 5);
                ho_Rectangle3.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle3, hv_Row4, hv_Column4, hv_Phi2, hv_Length12 + 200,
                                           hv_lll);
                ho_RegionDifference.Dispose();
                HOperatorSet.Difference(ho_Rectangle1, ho_Rectangle3, out ho_RegionDifference
                                        );
                ho_ImageReduced1.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageReduced, ho_RegionDifference, out ho_ImageReduced1
                                          );
                HOperatorSet.Intensity(ho_RegionDifference, ho_ImageReduced1, out hv_Mean, out hv_Deviation1);
                ho_Border.Dispose();
                HOperatorSet.ThresholdSubPix(ho_ImageReduced1, out ho_Border, hv_Mean);
                HOperatorSet.LengthXld(ho_Border, out hv_Length8);
                if ((int)(new HTuple((new HTuple(hv_Length8.TupleLength())).TupleGreater(1))) != 0)
                {
                    ho_SelectedContours.Dispose();
                    HOperatorSet.SelectContoursXld(ho_Border, out ho_SelectedContours, "contour_length",
                                                   0.5 * (hv_Length8.TupleMax()), 1.5 * (hv_Length8.TupleMax()), -0.5, 0.5);
                    HOperatorSet.CountObj(ho_SelectedContours, out hv_Number);
                    if ((int)(new HTuple(hv_Number.TupleEqual(2))) != 0)
                    {
                        ho_ObjectSelected1.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedContours, out ho_ObjectSelected1, 1);
                        ho_ObjectSelected2.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedContours, out ho_ObjectSelected2, 2);
                        HOperatorSet.GetContourXld(ho_ObjectSelected1, out hv_Rowo1, out hv_Colo1);
                        HOperatorSet.GetContourXld(ho_ObjectSelected2, out hv_Rowo2, out hv_Colo2);
                        HOperatorSet.TupleTan(hv_fx + ((new HTuple(0.000001)).TupleRad()), out hv_as);
                        HOperatorSet.TupleTan(hv_fx + ((new HTuple(90.000001)).TupleRad()), out hv_ac);
                        hv_b  = -1;
                        hv_cs = (-(hv_Row4 + (5000 * (((hv_fx + ((new HTuple(90)).TupleRad()))).TupleSin()
                                                      )))) - (hv_as * (hv_Column4 - (5000 * (((hv_fx + ((new HTuple(90)).TupleRad()))).TupleCos()
                                                                                             ))));
                        hv_cc = (-(hv_Row4 + (5000 * (hv_fx.TupleSin())))) - (hv_ac * (hv_Column4 - (5000 * (hv_fx.TupleCos()
                                                                                                             ))));
                        hv_ds1 = (((((hv_as * hv_Colo1) + (hv_b * (-hv_Rowo1))) + hv_cs)).TupleAbs()) / (((1 + (hv_as * hv_as))).TupleSqrt()
                                                                                                         );
                        hv_ds2 = (((((hv_as * hv_Colo2) + (hv_b * (-hv_Rowo2))) + hv_cs)).TupleAbs()) / (((1 + (hv_as * hv_as))).TupleSqrt()
                                                                                                         );
                        hv_dc1 = (((((hv_ac * hv_Colo1) + (hv_b * (-hv_Rowo1))) + hv_cc)).TupleAbs()) / (((1 + (hv_ac * hv_ac))).TupleSqrt()
                                                                                                         );
                        hv_dc2 = (((((hv_ac * hv_Colo2) + (hv_b * (-hv_Rowo2))) + hv_cc)).TupleAbs()) / (((1 + (hv_ac * hv_ac))).TupleSqrt()
                                                                                                         );
                        ho_ContoursSplit1.Dispose();
                        HOperatorSet.SegmentContoursXld(ho_ObjectSelected2, out ho_ContoursSplit1,
                                                        "lines_circles", 5, 4, 2);
                        HOperatorSet.LengthXld(ho_ContoursSplit1, out hv_Length3);
                        hv_dd = hv_Length3.TupleMean();
                        HOperatorSet.TupleFloor(hv_dd, out hv_Floor);
                        HOperatorSet.TupleLength(hv_Colo1, out hv_Length4);
                        HOperatorSet.TupleGenSequence(hv_Length4 - hv_Floor, hv_Length4 - 1, 1, out hv_Sequence1);
                        HOperatorSet.TupleLength(hv_Colo2, out hv_Length5);
                        HOperatorSet.TupleGenSequence(0, hv_Floor - 1, 1, out hv_Sequence);
                        HOperatorSet.TupleGenSequence(hv_Length5 - hv_Floor, hv_Length5 - 1, 1, out hv_Sequence2);
                        HOperatorSet.TupleRemove(hv_ds1, hv_Sequence, out hv_Reduced1);
                        HOperatorSet.TupleRemove(hv_ds1, hv_Sequence1, out hv_Reduced2);
                        HOperatorSet.TupleRemove(hv_ds2, hv_Sequence, out hv_Reduced3);
                        HOperatorSet.TupleRemove(hv_ds2, hv_Sequence2, out hv_Reduced4);
                        hv_d1 = hv_Reduced1 - hv_Reduced2;
                        HOperatorSet.TupleLength(hv_d1, out hv_Length6);
                        HOperatorSet.TupleGenSequence(1, hv_Length6, 1, out hv_Sequence3);
                        ho_RegionLines.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLines, 0, 0, 0, hv_Length6);
                        ho_Region.Dispose();
                        HOperatorSet.GenRegionPoints(out ho_Region, hv_d1, hv_Sequence3);
                        ho_RegionIntersection.Dispose();
                        HOperatorSet.Intersection(ho_RegionLines, ho_Region, out ho_RegionIntersection
                                                  );
                        ho_ConnectedRegions.Dispose();
                        HOperatorSet.Connection(ho_RegionIntersection, out ho_ConnectedRegions);
                        HOperatorSet.AreaCenter(ho_ConnectedRegions, out hv_Area, out hv_Row2, out hv_Column2);
                        HOperatorSet.TupleSelect(hv_Colo1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(),
                                                 out hv_Selectedc);
                        HOperatorSet.TupleSelect(hv_Rowo1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(),
                                                 out hv_Selectedr);
                        HOperatorSet.TupleGenConst(new HTuple(hv_Selectedr.TupleLength()), 1, out hv_Newtuple);
                        ho_Circle.Dispose();
                        HOperatorSet.GenCircle(out ho_Circle, hv_Selectedr, hv_Selectedc, hv_Newtuple);
                        hv_d2 = hv_Reduced3 - hv_Reduced4;
                        HOperatorSet.TupleLength(hv_d2, out hv_Length7);
                        HOperatorSet.TupleGenSequence(1, hv_Length7, 1, out hv_Sequence4);
                        ho_RegionLines1.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLines1, 0, 0, 0, hv_Length7);
                        ho_Region1.Dispose();
                        HOperatorSet.GenRegionPoints(out ho_Region1, hv_d2, hv_Sequence4);
                        ho_RegionIntersection1.Dispose();
                        HOperatorSet.Intersection(ho_Region1, ho_RegionLines1, out ho_RegionIntersection1
                                                  );
                        ho_ConnectedRegions1.Dispose();
                        HOperatorSet.Connection(ho_RegionIntersection1, out ho_ConnectedRegions1);
                        HOperatorSet.AreaCenter(ho_ConnectedRegions1, out hv_Area1, out hv_Row3,
                                                out hv_Column3);
                        HOperatorSet.TupleSelect(hv_Colo2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(),
                                                 out hv_Selectedc1);
                        HOperatorSet.TupleSelect(hv_Rowo2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(),
                                                 out hv_Selectedr1);
                        HOperatorSet.TupleGenConst(new HTuple(hv_Selectedr1.TupleLength()), 1, out hv_Newtuple5);
                        ho_Circle1.Dispose();
                        HOperatorSet.GenCircle(out ho_Circle1, hv_Selectedr1, hv_Selectedc1, hv_Newtuple5);
                        HOperatorSet.TupleSelect(hv_ds1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(), out hv_Selectedds1);
                        HOperatorSet.TupleSelect(hv_dc1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(), out hv_Selecteddc1);
                        HOperatorSet.TupleSelect(hv_ds2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(), out hv_Selectedds2);
                        HOperatorSet.TupleSelect(hv_dc2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(), out hv_Selecteddc2);
                        if ((int)(new HTuple((new HTuple(hv_Selectedds1.TupleMean())).TupleLess(hv_Selectedds2.TupleMean()
                                                                                                ))) != 0)
                        {
                            hv_hs1  = hv_Selectedds1.Clone();
                            hv_hs1r = hv_Selectedr.Clone();
                            hv_hs1c = hv_Selectedc.Clone();
                            hv_hs2  = hv_Selectedds2.Clone();
                            hv_hs2r = hv_Selectedr1.Clone();
                            hv_hs2c = hv_Selectedc1.Clone();
                            hv_hc1  = hv_Selecteddc1.Clone();
                            hv_hc2  = hv_Selecteddc2.Clone();
                        }
                        else
                        {
                            hv_hs1  = hv_Selectedds2.Clone();
                            hv_hs2r = hv_Selectedr.Clone();
                            hv_hs2c = hv_Selectedc.Clone();
                            hv_hs2  = hv_Selectedds1.Clone();
                            hv_hs1r = hv_Selectedr1.Clone();
                            hv_hs1c = hv_Selectedc1.Clone();
                            hv_hc2  = hv_Selecteddc1.Clone();
                            hv_hc1  = hv_Selecteddc2.Clone();
                        }
                        hv_jdr = new HTuple();
                        hv_jdc = new HTuple();
                        hv_jds = new HTuple();
                        hv_jdC = new HTuple();
                        hv_jxr = new HTuple();
                        hv_jxc = new HTuple();
                        hv_jxs = new HTuple();
                        hv_jxC = new HTuple();
                        hv_ydr = new HTuple();
                        hv_ydc = new HTuple();
                        hv_yds = new HTuple();
                        hv_ydC = new HTuple();
                        hv_yxr = new HTuple();
                        hv_yxc = new HTuple();
                        hv_yxs = new HTuple();
                        hv_yxC = new HTuple();
                        hv_jin = new HTuple(hv_hs1.TupleLength());
                        hv_yu  = new HTuple(hv_hs2.TupleLength());
                        hv_jp  = hv_hs1.TupleMean();
                        hv_yp  = hv_hs2.TupleMean();
                        HTuple end_val128  = hv_jin - 1;
                        HTuple step_val128 = 1;
                        for (hv_j = 0; hv_j.Continue(end_val128, step_val128); hv_j = hv_j.TupleAdd(step_val128))
                        {
                            if ((int)(new HTuple(((hv_hs1.TupleSelect(hv_j))).TupleLess(hv_jp))) != 0)
                            {
                                hv_jdr = hv_jdr.TupleConcat(hv_hs1r.TupleSelect(hv_j));
                                hv_jdc = hv_jdc.TupleConcat(hv_hs1c.TupleSelect(hv_j));
                                hv_jds = hv_jds.TupleConcat(hv_hs1.TupleSelect(hv_j));
                                hv_jdC = hv_jdC.TupleConcat(hv_hc1.TupleSelect(hv_j));
                            }
                            else
                            {
                                hv_jxr = hv_jxr.TupleConcat(hv_hs1r.TupleSelect(hv_j));
                                hv_jxc = hv_jxc.TupleConcat(hv_hs1c.TupleSelect(hv_j));
                                hv_jxs = hv_jxs.TupleConcat(hv_hs1.TupleSelect(hv_j));
                                hv_jxC = hv_jxC.TupleConcat(hv_hc1.TupleSelect(hv_j));
                            }
                        }
                        HTuple end_val141  = hv_yu - 1;
                        HTuple step_val141 = 1;
                        for (hv_j = 0; hv_j.Continue(end_val141, step_val141); hv_j = hv_j.TupleAdd(step_val141))
                        {
                            if ((int)(new HTuple(((hv_hs2.TupleSelect(hv_j))).TupleGreater(hv_yp))) != 0)
                            {
                                hv_ydr = hv_ydr.TupleConcat(hv_hs2r.TupleSelect(hv_j));
                                hv_ydc = hv_ydc.TupleConcat(hv_hs2c.TupleSelect(hv_j));
                                hv_yds = hv_yds.TupleConcat(hv_hs2.TupleSelect(hv_j));
                                hv_ydC = hv_ydC.TupleConcat(hv_hc2.TupleSelect(hv_j));
                            }
                            else
                            {
                                hv_yxr = hv_yxr.TupleConcat(hv_hs2r.TupleSelect(hv_j));
                                hv_yxc = hv_yxc.TupleConcat(hv_hs2c.TupleSelect(hv_j));
                                hv_yxs = hv_yxs.TupleConcat(hv_hs2.TupleSelect(hv_j));
                                hv_yxC = hv_yxC.TupleConcat(hv_hc2.TupleSelect(hv_j));
                            }
                        }
                        hv_luoju1 = (((hv_hc1.TupleMax()) - (hv_hc1.TupleMin())) / ((new HTuple(hv_hc1.TupleLength()
                                                                                                )) - 1)) * 2;
                        hv_luoju2 = (((hv_hc2.TupleMax()) - (hv_hc2.TupleMin())) / ((new HTuple(hv_hc2.TupleLength()
                                                                                                )) - 1)) * 2;
                        hv_dajing   = (hv_yds.TupleMean()) - (hv_jds.TupleMean());
                        hv_xiaojing = (hv_yxs.TupleMean()) - (hv_jxs.TupleMean());
                        hv_changdu  = (((hv_hc1.TupleConcat(hv_hc2))).TupleMax()) - (((hv_hc1.TupleConcat(
                                                                                           hv_hc2))).TupleMin());
                        hv_tiaoshu = (((((((new HTuple(hv_jds.TupleLength())).TupleConcat(new HTuple(hv_jxs.TupleLength()
                                                                                                     )))).TupleConcat(new HTuple(hv_yds.TupleLength())))).TupleConcat(new HTuple(hv_yxs.TupleLength()
                                                                                                                                                                                 )))).TupleMax();
                    }
                }
                HOperatorSet.Union2(ho_Circle, ho_Circle1, out RegionToDisp);



                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("大径平均值");
                hv_result = hv_result.TupleConcat(hv_dajing.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺纹长度");
                hv_result = hv_result.TupleConcat(hv_changdu.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺纹条数");
                hv_result = hv_result.TupleConcat(hv_tiaoshu.D);
                hv_result = hv_result.TupleConcat("螺距1");
                hv_result = hv_result.TupleConcat(hv_luoju1.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺距2");
                hv_result = hv_result.TupleConcat(hv_luoju2.D * pixeldist);
                result    = hv_result.Clone();

                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Regions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_RegionFillUp.Dispose();
                ho_Contours.Dispose();
                ho_Rectangle1.Dispose();
                ho_Rectangle3.Dispose();
                ho_RegionDifference.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Border.Dispose();
                ho_SelectedContours.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_ObjectSelected2.Dispose();
                ho_ContoursSplit1.Dispose();
                ho_RegionLines.Dispose();
                ho_Region.Dispose();
                ho_RegionIntersection.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_Circle.Dispose();
                ho_RegionLines1.Dispose();
                ho_Region1.Dispose();
                ho_RegionIntersection1.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_Circle1.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_ObjectSelected3.Dispose();
                ho_cl.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("螺距1");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺距2");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();

                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Regions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_RegionFillUp.Dispose();
                ho_Contours.Dispose();
                ho_Rectangle1.Dispose();
                ho_Rectangle3.Dispose();
                ho_RegionDifference.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Border.Dispose();
                ho_SelectedContours.Dispose();
                ho_ObjectSelected1.Dispose(); ho_cl.Dispose();
                ho_ObjectSelected2.Dispose();
                ho_ContoursSplit1.Dispose();
                ho_RegionLines.Dispose();
                ho_Region.Dispose();
                ho_RegionIntersection.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_Circle.Dispose();
                ho_RegionLines1.Dispose();
                ho_Region1.Dispose();
                ho_RegionIntersection1.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_Circle1.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_ObjectSelected3.Dispose();
                algorithm.Region.Dispose();
            }
        }
Example #21
0
    // Chapter: Graphics / Text
    // Short Description: Set font independent of OS
    public void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font,
                                 HTuple hv_Bold, HTuple hv_Slant)
    {
        // Local iconic variables

        // Local control variables

        HTuple hv_OS = null, hv_BufferWindowHandle = new HTuple();
        HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
        HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
        HTuple hv_Scale = new HTuple(), hv_Exception = new HTuple();
        HTuple hv_SubFamily = new HTuple(), hv_Fonts = new HTuple();
        HTuple hv_SystemFonts = new HTuple(), hv_Guess = new HTuple();
        HTuple hv_I = new HTuple(), hv_Index = new HTuple(), hv_AllowedFontSizes = new HTuple();
        HTuple hv_Distances = new HTuple(), hv_Indices = new HTuple();
        HTuple hv_FontSelRegexp = new HTuple(), hv_FontsCourier = new HTuple();
        HTuple hv_Bold_COPY_INP_TMP  = hv_Bold.Clone();
        HTuple hv_Font_COPY_INP_TMP  = hv_Font.Clone();
        HTuple hv_Size_COPY_INP_TMP  = hv_Size.Clone();
        HTuple hv_Slant_COPY_INP_TMP = hv_Slant.Clone();

        // Initialize local and output iconic variables
        //This procedure sets the text font of the current window with
        //the specified attributes.
        //It is assumed that following fonts are installed on the system:
        //Windows: Courier New, Arial Times New Roman
        //Mac OS X: CourierNewPS, Arial, TimesNewRomanPS
        //Linux: courier, helvetica, times
        //Because fonts are displayed smaller on Linux than on Windows,
        //a scaling factor of 1.25 is used the get comparable results.
        //For Linux, only a limited number of font sizes is supported,
        //to get comparable results, it is recommended to use one of the
        //following sizes: 9, 11, 14, 16, 20, 27
        //(which will be mapped internally on Linux systems to 11, 14, 17, 20, 25, 34)
        //
        //Input parameters:
        //WindowHandle: The graphics window for which the font will be set
        //Size: The font size. If Size=-1, the default of 16 is used.
        //Bold: If set to 'true', a bold font is used
        //Slant: If set to 'true', a slanted font is used
        //
        HOperatorSet.GetSystem("operating_system", out hv_OS);
        // dev_get_preferences(...); only in hdevelop
        // dev_set_preferences(...); only in hdevelop
        if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
                      new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0)
        {
            hv_Size_COPY_INP_TMP = 16;
        }
        if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
        {
            //Set font on Windows systems
            try
            {
                //Check, if font scaling is switched on
                //open_window(...);
                HOperatorSet.SetFont(hv_ExpDefaultWinHandle, "-Consolas-16-*-0-*-*-1-");
                HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, "test_string", out hv_Ascent,
                                              out hv_Descent, out hv_Width, out hv_Height);
                //Expected width is 110
                hv_Scale             = 110.0 / hv_Width;
                hv_Size_COPY_INP_TMP = ((hv_Size_COPY_INP_TMP * hv_Scale)).TupleInt();
                //close_window(...);
            }
            // catch (Exception)
            catch (HalconException HDevExpDefaultException1)
            {
                HDevExpDefaultException1.ToHTuple(out hv_Exception);
                //throw (Exception)
            }
            if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))).TupleOr(
                          new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0)
            {
                hv_Font_COPY_INP_TMP = "Courier New";
            }
            else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0)
            {
                hv_Font_COPY_INP_TMP = "Consolas";
            }
            else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
            {
                hv_Font_COPY_INP_TMP = "Arial";
            }
            else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
            {
                hv_Font_COPY_INP_TMP = "Times New Roman";
            }
            if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
            {
                hv_Bold_COPY_INP_TMP = 1;
            }
            else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
            {
                hv_Bold_COPY_INP_TMP = 0;
            }
            else
            {
                hv_Exception = "Wrong value of control parameter Bold";
                throw new HalconException(hv_Exception);
            }
            if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
            {
                hv_Slant_COPY_INP_TMP = 1;
            }
            else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
            {
                hv_Slant_COPY_INP_TMP = 0;
            }
            else
            {
                hv_Exception = "Wrong value of control parameter Slant";
                throw new HalconException(hv_Exception);
            }
            try
            {
                HOperatorSet.SetFont(hv_ExpDefaultWinHandle, ((((((("-" + hv_Font_COPY_INP_TMP) + "-") + hv_Size_COPY_INP_TMP) + "-*-") + hv_Slant_COPY_INP_TMP) + "-*-*-") + hv_Bold_COPY_INP_TMP) + "-");
            }
            // catch (Exception)
            catch (HalconException HDevExpDefaultException1)
            {
                HDevExpDefaultException1.ToHTuple(out hv_Exception);
                //throw (Exception)
            }
        }
        else if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Dar"))) != 0)
        {
            //Set font on Mac OS X systems. Since OS X does not have a strict naming
            //scheme for font attributes, we use tables to determine the correct font
            //name.
            hv_SubFamily = 0;
            if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
            {
                hv_SubFamily = hv_SubFamily.TupleBor(1);
            }
            else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleNotEqual("false"))) != 0)
            {
                hv_Exception = "Wrong value of control parameter Slant";
                throw new HalconException(hv_Exception);
            }
            if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
            {
                hv_SubFamily = hv_SubFamily.TupleBor(2);
            }
            else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleNotEqual("false"))) != 0)
            {
                hv_Exception = "Wrong value of control parameter Bold";
                throw new HalconException(hv_Exception);
            }
            if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0)
            {
                hv_Fonts    = new HTuple();
                hv_Fonts[0] = "Menlo-Regular";
                hv_Fonts[1] = "Menlo-Italic";
                hv_Fonts[2] = "Menlo-Bold";
                hv_Fonts[3] = "Menlo-BoldItalic";
            }
            else if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))).TupleOr(
                               new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0)
            {
                hv_Fonts    = new HTuple();
                hv_Fonts[0] = "CourierNewPSMT";
                hv_Fonts[1] = "CourierNewPS-ItalicMT";
                hv_Fonts[2] = "CourierNewPS-BoldMT";
                hv_Fonts[3] = "CourierNewPS-BoldItalicMT";
            }
            else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
            {
                hv_Fonts    = new HTuple();
                hv_Fonts[0] = "ArialMT";
                hv_Fonts[1] = "Arial-ItalicMT";
                hv_Fonts[2] = "Arial-BoldMT";
                hv_Fonts[3] = "Arial-BoldItalicMT";
            }
            else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
            {
                hv_Fonts    = new HTuple();
                hv_Fonts[0] = "TimesNewRomanPSMT";
                hv_Fonts[1] = "TimesNewRomanPS-ItalicMT";
                hv_Fonts[2] = "TimesNewRomanPS-BoldMT";
                hv_Fonts[3] = "TimesNewRomanPS-BoldItalicMT";
            }
            else
            {
                //Attempt to figure out which of the fonts installed on the system
                //the user could have meant.
                HOperatorSet.QueryFont(hv_ExpDefaultWinHandle, out hv_SystemFonts);
                hv_Fonts = new HTuple();
                hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP);
                hv_Guess = new HTuple();
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP);
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Regular");
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "MT");
                for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                {
                    HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                    if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                    {
                        if (hv_Fonts == null)
                        {
                            hv_Fonts = new HTuple();
                        }
                        hv_Fonts[0] = hv_Guess.TupleSelect(hv_I);
                        break;
                    }
                }
                //Guess name of slanted font
                hv_Guess = new HTuple();
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Italic");
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-ItalicMT");
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Oblique");
                for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                {
                    HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                    if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                    {
                        if (hv_Fonts == null)
                        {
                            hv_Fonts = new HTuple();
                        }
                        hv_Fonts[1] = hv_Guess.TupleSelect(hv_I);
                        break;
                    }
                }
                //Guess name of bold font
                hv_Guess = new HTuple();
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Bold");
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldMT");
                for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                {
                    HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                    if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                    {
                        if (hv_Fonts == null)
                        {
                            hv_Fonts = new HTuple();
                        }
                        hv_Fonts[2] = hv_Guess.TupleSelect(hv_I);
                        break;
                    }
                }
                //Guess name of bold slanted font
                hv_Guess = new HTuple();
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldItalic");
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldItalicMT");
                hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldOblique");
                for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1)
                {
                    HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index);
                    if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0)
                    {
                        if (hv_Fonts == null)
                        {
                            hv_Fonts = new HTuple();
                        }
                        hv_Fonts[3] = hv_Guess.TupleSelect(hv_I);
                        break;
                    }
                }
            }
            hv_Font_COPY_INP_TMP = hv_Fonts.TupleSelect(hv_SubFamily);
            try
            {
                HOperatorSet.SetFont(hv_ExpDefaultWinHandle, (hv_Font_COPY_INP_TMP + "-") + hv_Size_COPY_INP_TMP);
            }
            // catch (Exception)
            catch (HalconException HDevExpDefaultException1)
            {
                HDevExpDefaultException1.ToHTuple(out hv_Exception);
                //throw (Exception)
            }
        }
        else
        {
            //Set font for UNIX systems
            hv_Size_COPY_INP_TMP   = hv_Size_COPY_INP_TMP * 1.25;
            hv_AllowedFontSizes    = new HTuple();
            hv_AllowedFontSizes[0] = 11;
            hv_AllowedFontSizes[1] = 14;
            hv_AllowedFontSizes[2] = 17;
            hv_AllowedFontSizes[3] = 20;
            hv_AllowedFontSizes[4] = 25;
            hv_AllowedFontSizes[5] = 34;
            if ((int)(new HTuple(((hv_AllowedFontSizes.TupleFind(hv_Size_COPY_INP_TMP))).TupleEqual(
                                     -1))) != 0)
            {
                hv_Distances = ((hv_AllowedFontSizes - hv_Size_COPY_INP_TMP)).TupleAbs();
                HOperatorSet.TupleSortIndex(hv_Distances, out hv_Indices);
                hv_Size_COPY_INP_TMP = hv_AllowedFontSizes.TupleSelect(hv_Indices.TupleSelect(
                                                                           0));
            }
            if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(
                                                                                                   "Courier")))) != 0)
            {
                hv_Font_COPY_INP_TMP = "courier";
            }
            else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
            {
                hv_Font_COPY_INP_TMP = "helvetica";
            }
            else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
            {
                hv_Font_COPY_INP_TMP = "times";
            }
            if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
            {
                hv_Bold_COPY_INP_TMP = "bold";
            }
            else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
            {
                hv_Bold_COPY_INP_TMP = "medium";
            }
            else
            {
                hv_Exception = "Wrong value of control parameter Bold";
                throw new HalconException(hv_Exception);
            }
            if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
            {
                if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("times"))) != 0)
                {
                    hv_Slant_COPY_INP_TMP = "i";
                }
                else
                {
                    hv_Slant_COPY_INP_TMP = "o";
                }
            }
            else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
            {
                hv_Slant_COPY_INP_TMP = "r";
            }
            else
            {
                hv_Exception = "Wrong value of control parameter Slant";
                throw new HalconException(hv_Exception);
            }
            try
            {
                HOperatorSet.SetFont(hv_ExpDefaultWinHandle, ((((((("-adobe-" + hv_Font_COPY_INP_TMP) + "-") + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*");
            }
            // catch (Exception)
            catch (HalconException HDevExpDefaultException1)
            {
                HDevExpDefaultException1.ToHTuple(out hv_Exception);
                if ((int)((new HTuple(((hv_OS.TupleSubstr(0, 4))).TupleEqual("Linux"))).TupleAnd(
                              new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0)
                {
                    HOperatorSet.QueryFont(hv_ExpDefaultWinHandle, out hv_Fonts);
                    hv_FontSelRegexp = (("^-[^-]*-[^-]*[Cc]ourier[^-]*-" + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP;
                    hv_FontsCourier  = ((hv_Fonts.TupleRegexpSelect(hv_FontSelRegexp))).TupleRegexpMatch(
                        hv_FontSelRegexp);
                    if ((int)(new HTuple((new HTuple(hv_FontsCourier.TupleLength())).TupleEqual(
                                             0))) != 0)
                    {
                        hv_Exception = "Wrong font name";
                        //throw (Exception)
                    }
                    else
                    {
                        try
                        {
                            HOperatorSet.SetFont(hv_ExpDefaultWinHandle, (((hv_FontsCourier.TupleSelect(
                                                                                0)) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*");
                        }
                        // catch (Exception)
                        catch (HalconException HDevExpDefaultException2)
                        {
                            HDevExpDefaultException2.ToHTuple(out hv_Exception);
                            //throw (Exception)
                        }
                    }
                }
                //throw (Exception)
            }
        }
        // dev_set_preferences(...); only in hdevelop

        return;
    }
Example #22
0
        //}
        public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
                                 HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
        {
            // Local iconic variables

            // Local control variables

            HTuple hv_Red = null, hv_Green = null, hv_Blue = null;
            HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null;
            HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null;
            HTuple hv_WidthWin = null, hv_HeightWin = null, hv_MaxAscent = null;
            HTuple hv_MaxDescent = null, hv_MaxWidth = null, hv_MaxHeight = null;
            HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRow = new HTuple();
            HTuple hv_FactorColumn = new HTuple(), hv_UseShadow = null;
            HTuple hv_ShadowColor = null, hv_Exception = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
            HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
            HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
            HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
            HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
            HTuple hv_CurrentColor        = new HTuple();
            HTuple hv_Box_COPY_INP_TMP    = hv_Box.Clone();
            HTuple hv_Color_COPY_INP_TMP  = hv_Color.Clone();
            HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
            HTuple hv_Row_COPY_INP_TMP    = hv_Row.Clone();
            HTuple hv_String_COPY_INP_TMP = hv_String.Clone();

            // Initialize local and output iconic variables
            //This procedure displays text in a graphics window.
            //
            //Input parameters:
            //WindowHandle: The WindowHandle of the graphics window, where
            //   the message should be displayed
            //String: A tuple of strings containing the text message to be displayed
            //CoordSystem: If set to 'window', the text position is given
            //   with respect to the window coordinate system.
            //   If set to 'image', image coordinates are used.
            //   (This may be useful in zoomed images.)
            //Row: The row coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Column: The column coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Color: defines the color of the text as string.
            //   If set to [], '' or 'auto' the currently set color is used.
            //   If a tuple of strings is passed, the colors are used cyclically
            //   for each new textline.
            //Box: If Box[0] is set to 'true', the text is written within an orange box.
            //     If set to' false', no box is displayed.
            //     If set to a color string (e.g. 'white', '#FF00CC', etc.),
            //       the text is written in a box of that color.
            //     An optional second value for Box (Box[1]) controls if a shadow is displayed:
            //       'true' -> display a shadow in a default color
            //       'false' -> display no shadow (same as if no second value is given)
            //       otherwise -> use given string as color string for the shadow color
            //
            //Prepare window
            HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue);
            HOperatorSet.GetPart(hv_WindowHandle, out hv_Row1Part, out hv_Column1Part, out hv_Row2Part,
                                 out hv_Column2Part);
            HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowWin, out hv_ColumnWin,
                                          out hv_WidthWin, out hv_HeightWin);
            HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
            //
            //default settings
            if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Row_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Column_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
            {
                hv_Color_COPY_INP_TMP = "";
            }
            //
            hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n");
            //
            //Estimate extentions of text depending on font size.
            HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent,
                                        out hv_MaxWidth, out hv_MaxHeight);
            if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
            {
                hv_R1 = hv_Row_COPY_INP_TMP.Clone();
                hv_C1 = hv_Column_COPY_INP_TMP.Clone();
            }
            else
            {
                //Transform image to window coordinates
                hv_FactorRow    = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);
                hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);
                hv_R1           = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow;
                hv_C1           = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn;
            }
            //
            //Display text box depending on text size
            hv_UseShadow   = 1;
            hv_ShadowColor = "gray";
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
            {
                if (hv_Box_COPY_INP_TMP == null)
                {
                    hv_Box_COPY_INP_TMP = new HTuple();
                }
                hv_Box_COPY_INP_TMP[0] = "#fce9d4";
                hv_ShadowColor         = "#f28d26";
            }
            if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
                                     1))) != 0)
            {
                if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
                {
                    //Use default ShadowColor set above
                }
                else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
                                              "false"))) != 0)
                {
                    hv_UseShadow = 0;
                }
                else
                {
                    hv_ShadowColor = hv_Box_COPY_INP_TMP[1];
                    //Valid color?
                    try
                    {
                        HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                                                  1));
                    }
                    // catch (Exception)
                    catch (HalconException HDevExpDefaultException1)
                    {
                        HDevExpDefaultException1.ToHTuple(out hv_Exception);
                        hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)";
                        throw new HalconException(hv_Exception);
                    }
                }
            }
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
            {
                //Valid color?
                try
                {
                    HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
                }
                // catch (Exception)
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)";
                    throw new HalconException(hv_Exception);
                }
                //Calculate box extents
                hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " ";
                hv_Width = new HTuple();
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                                      )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    HOperatorSet.GetStringExtents(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
                                                      hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
                    hv_Width = hv_Width.TupleConcat(hv_W);
                }
                hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                            ));
                hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
                hv_R2         = hv_R1 + hv_FrameHeight;
                hv_C2         = hv_C1 + hv_FrameWidth;
                //Display rectangles
                HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode);
                HOperatorSet.SetDraw(hv_WindowHandle, "fill");
                //Set shadow color
                HOperatorSet.SetColor(hv_WindowHandle, hv_ShadowColor);
                if ((int)(hv_UseShadow) != 0)
                {
                    HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1, hv_C2 + 1);
                }
                //Set box color
                HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
                HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
                HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode);
            }
            //Write text.
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                                  )) - 1); hv_Index = (int)hv_Index + 1)
            {
                hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                                                                                           )));
                if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
                                                                                                  "auto")))) != 0)
                {
                    HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor);
                }
                else
                {
                    HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
                }
                hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
                HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_C1);
                HOperatorSet.WriteString(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
                                             hv_Index));
            }
            //Reset changed window settings
            HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
            HOperatorSet.SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
                                 hv_Column2Part);

            return;
        }
Example #23
0
    // Chapter: Graphics / Text
    // Short Description: This procedure writes a text message.
    public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
                             HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
    {
        // Local iconic variables

        // Local control variables

        HTuple hv_M = null, hv_N = null, hv_Red = null;
        HTuple hv_Green = null, hv_Blue = null, hv_RowI1Part = null;
        HTuple hv_ColumnI1Part = null, hv_RowI2Part = null, hv_ColumnI2Part = null;
        HTuple hv_RowIWin = null, hv_ColumnIWin = null, hv_WidthWin = null;
        HTuple hv_HeightWin = null, hv_I = null, hv_RowI = new HTuple();
        HTuple hv_ColumnI = new HTuple(), hv_StringI = new HTuple();
        HTuple hv_MaxAscent = new HTuple(), hv_MaxDescent = new HTuple();
        HTuple hv_MaxWidth = new HTuple(), hv_MaxHeight = new HTuple();
        HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRowI = new HTuple();
        HTuple hv_FactorColumnI = new HTuple(), hv_UseShadow = new HTuple();
        HTuple hv_ShadowColor = new HTuple(), hv_Exception = new HTuple();
        HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
        HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
        HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
        HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
        HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
        HTuple hv_CurrentColor        = new HTuple();
        HTuple hv_Box_COPY_INP_TMP    = hv_Box.Clone();
        HTuple hv_Color_COPY_INP_TMP  = hv_Color.Clone();
        HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
        HTuple hv_Row_COPY_INP_TMP    = hv_Row.Clone();
        HTuple hv_String_COPY_INP_TMP = hv_String.Clone();

        // Initialize local and output iconic variables
        //This procedure displays text in a graphics window.
        //
        //Input parameters:
        //WindowHandle: The WindowHandle of the graphics window, where
        //   the message should be displayed
        //String: A tuple of strings containing the text message to be displayed
        //CoordSystem: If set to 'window', the text position is given
        //   with respect to the window coordinate system.
        //   If set to 'image', image coordinates are used.
        //   (This may be useful in zoomed images.)
        //Row: The row coordinate of the desired text position
        //   If set to -1, a default value of 12 is used.
        //   A tuple of values is allowed to display text at different
        //   positions.
        //Column: The column coordinate of the desired text position
        //   If set to -1, a default value of 12 is used.
        //   A tuple of values is allowed to display text at different
        //   positions.
        //Color: defines the color of the text as string.
        //   If set to [], '' or 'auto' the currently set color is used.
        //   If a tuple of strings is passed, the colors are used cyclically...
        //   - if |Row| == |Column| == 1: for each new textline
        //   = else for each text position.
        //Box: If Box[0] is set to 'true', the text is written within an orange box.
        //     If set to' false', no box is displayed.
        //     If set to a color string (e.g. 'white', '#FF00CC', etc.),
        //       the text is written in a box of that color.
        //     An optional second value for Box (Box[1]) controls if a shadow is displayed:
        //       'true' -> display a shadow in a default color
        //       'false' -> display no shadow
        //       otherwise -> use given string as color string for the shadow color
        //
        //It is possible to display multiple text strings in a single call.
        //In this case, some restrictions apply:
        //- Multiple text positions can be defined by specifying a tuple
        //  with multiple Row and/or Column coordinates, i.e.:
        //  - |Row| == n, |Column| == n
        //  - |Row| == n, |Column| == 1
        //  - |Row| == 1, |Column| == n
        //- If |Row| == |Column| == 1,
        //  each element of String is display in a new textline.
        //- If multiple positions or specified, the number of Strings
        //  must match the number of positions, i.e.:
        //  - Either |String| == n (each string is displayed at the
        //                          corresponding position),
        //  - or     |String| == 1 (The string is displayed n times).
        //
        if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
        {
            hv_Color_COPY_INP_TMP = "";
        }
        if ((int)(new HTuple(hv_Box_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
        {
            hv_Box_COPY_INP_TMP = "false";
        }
        //
        //
        //Check conditions
        //
        hv_M = (new HTuple(hv_Row_COPY_INP_TMP.TupleLength())) * (new HTuple(hv_Column_COPY_INP_TMP.TupleLength()
                                                                             ));
        hv_N = new HTuple(hv_Row_COPY_INP_TMP.TupleLength());
        if ((int)((new HTuple(hv_M.TupleEqual(0))).TupleOr(new HTuple(hv_String_COPY_INP_TMP.TupleEqual(
                                                                          new HTuple())))) != 0)
        {
            return;
        }
        if ((int)(new HTuple(hv_M.TupleNotEqual(1))) != 0)
        {
            //Multiple positions
            //
            //Expand single parameters
            if ((int)(new HTuple((new HTuple(hv_Row_COPY_INP_TMP.TupleLength())).TupleEqual(
                                     1))) != 0)
            {
                hv_N = new HTuple(hv_Column_COPY_INP_TMP.TupleLength());
                HOperatorSet.TupleGenConst(hv_N, hv_Row_COPY_INP_TMP, out hv_Row_COPY_INP_TMP);
            }
            else if ((int)(new HTuple((new HTuple(hv_Column_COPY_INP_TMP.TupleLength()
                                                  )).TupleEqual(1))) != 0)
            {
                HOperatorSet.TupleGenConst(hv_N, hv_Column_COPY_INP_TMP, out hv_Column_COPY_INP_TMP);
            }
            else if ((int)(new HTuple((new HTuple(hv_Column_COPY_INP_TMP.TupleLength()
                                                  )).TupleNotEqual(new HTuple(hv_Row_COPY_INP_TMP.TupleLength())))) != 0)
            {
                throw new HalconException("Number of elements in Row and Column does not match.");
            }
            if ((int)(new HTuple((new HTuple(hv_String_COPY_INP_TMP.TupleLength())).TupleEqual(
                                     1))) != 0)
            {
                HOperatorSet.TupleGenConst(hv_N, hv_String_COPY_INP_TMP, out hv_String_COPY_INP_TMP);
            }
            else if ((int)(new HTuple((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                  )).TupleNotEqual(hv_N))) != 0)
            {
                throw new HalconException("Number of elements in Strings does not match number of positions.");
            }
            //
        }
        //
        //Prepare window
        HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue);
        HOperatorSet.GetPart(hv_WindowHandle, out hv_RowI1Part, out hv_ColumnI1Part,
                             out hv_RowI2Part, out hv_ColumnI2Part);
        HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowIWin, out hv_ColumnIWin,
                                      out hv_WidthWin, out hv_HeightWin);
        HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
        //
        //Loop over all positions
        HTuple end_val89  = hv_N - 1;
        HTuple step_val89 = 1;

        for (hv_I = 0; hv_I.Continue(end_val89, step_val89); hv_I = hv_I.TupleAdd(step_val89))
        {
            hv_RowI    = hv_Row_COPY_INP_TMP.TupleSelect(hv_I);
            hv_ColumnI = hv_Column_COPY_INP_TMP.TupleSelect(hv_I);
            //Allow multiple strings for a single position.
            if ((int)(new HTuple(hv_N.TupleEqual(1))) != 0)
            {
                hv_StringI = hv_String_COPY_INP_TMP.Clone();
            }
            else
            {
                //In case of multiple positions, only single strings
                //are allowed per position.
                //For line breaks, use \n in this case.
                hv_StringI = hv_String_COPY_INP_TMP.TupleSelect(hv_I);
            }
            //Default settings
            //-1 is mapped to 12.
            if ((int)(new HTuple(hv_RowI.TupleEqual(-1))) != 0)
            {
                hv_RowI = 12;
            }
            if ((int)(new HTuple(hv_ColumnI.TupleEqual(-1))) != 0)
            {
                hv_ColumnI = 12;
            }
            //
            //Split string into one string per line.
            hv_StringI = ((("" + hv_StringI) + "")).TupleSplit("\n");
            //
            //Estimate extentions of text depending on font size.
            HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent,
                                        out hv_MaxWidth, out hv_MaxHeight);
            if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
            {
                hv_R1 = hv_RowI.Clone();
                hv_C1 = hv_ColumnI.Clone();
            }
            else
            {
                //Transform image to window coordinates.
                hv_FactorRowI    = (1.0 * hv_HeightWin) / ((hv_RowI2Part - hv_RowI1Part) + 1);
                hv_FactorColumnI = (1.0 * hv_WidthWin) / ((hv_ColumnI2Part - hv_ColumnI1Part) + 1);
                hv_R1            = (((hv_RowI - hv_RowI1Part) + 0.5) * hv_FactorRowI) - 0.5;
                hv_C1            = (((hv_ColumnI - hv_ColumnI1Part) + 0.5) * hv_FactorColumnI) - 0.5;
            }
            //
            //Display text box depending on text size.
            hv_UseShadow   = 1;
            hv_ShadowColor = "gray";
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
            {
                if (hv_Box_COPY_INP_TMP == null)
                {
                    hv_Box_COPY_INP_TMP = new HTuple();
                }
                hv_Box_COPY_INP_TMP[0] = "#fce9d4";
                hv_ShadowColor         = "#f28d26";
            }
            if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
                                     1))) != 0)
            {
                if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
                {
                    //Use default ShadowColor set above
                }
                else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
                                              "false"))) != 0)
                {
                    hv_UseShadow = 0;
                }
                else
                {
                    hv_ShadowColor = hv_Box_COPY_INP_TMP.TupleSelect(1);
                    //Valid color?
                    try
                    {
                        HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                                                  1));
                    }
                    // catch (Exception)
                    catch (HalconException HDevExpDefaultException1)
                    {
                        HDevExpDefaultException1.ToHTuple(out hv_Exception);
                        hv_Exception = new HTuple("Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)");
                        throw new HalconException(hv_Exception);
                    }
                }
            }
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
            {
                //Valid color?
                try
                {
                    HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                                              0));
                }
                // catch (Exception)
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    hv_Exception = new HTuple("Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)");
                    throw new HalconException(hv_Exception);
                }
                //Calculate box extents
                hv_StringI = (" " + hv_StringI) + " ";
                hv_Width   = new HTuple();
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_StringI.TupleLength()
                                                                      )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    HOperatorSet.GetStringExtents(hv_WindowHandle, hv_StringI.TupleSelect(hv_Index),
                                                  out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
                    hv_Width = hv_Width.TupleConcat(hv_W);
                }
                hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_StringI.TupleLength()));
                hv_FrameWidth  = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
                hv_R2          = hv_R1 + hv_FrameHeight;
                hv_C2          = hv_C1 + hv_FrameWidth;
                //Display rectangles
                HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode);
                HOperatorSet.SetDraw(hv_WindowHandle, "fill");
                //Set shadow color
                HOperatorSet.SetColor(hv_WindowHandle, hv_ShadowColor);
                if ((int)(hv_UseShadow) != 0)
                {
                    HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1,
                                                hv_C2 + 1);
                }
                //Set box color
                HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
                HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
                HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode);
            }
            //Write text.
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_StringI.TupleLength())) - 1); hv_Index = (int)hv_Index + 1)
            {
                //Set color
                if ((int)(new HTuple(hv_N.TupleEqual(1))) != 0)
                {
                    //Wiht a single text position, each text line
                    //may get a different color.
                    hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                                                                                               )));
                }
                else
                {
                    //With multiple text positions, each position
                    //gets a single color for all text lines.
                    hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_I % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                                                                                           )));
                }
                if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
                                                                                                  "auto")))) != 0)
                {
                    //Valid color?
                    try
                    {
                        HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor);
                    }
                    // catch (Exception)
                    catch (HalconException HDevExpDefaultException1)
                    {
                        HDevExpDefaultException1.ToHTuple(out hv_Exception);
                        hv_Exception = ((("Wrong value of control parameter Color[" + (hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                                                                                                              )))) + "] == '") + hv_CurrentColor) + "' (must be a valid color string)";
                        throw new HalconException(hv_Exception);
                    }
                }
                else
                {
                    HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
                }
                //Finally display text
                hv_RowI = hv_R1 + (hv_MaxHeight * hv_Index);
                HOperatorSet.SetTposition(hv_WindowHandle, hv_RowI, hv_C1);
                HOperatorSet.WriteString(hv_WindowHandle, hv_StringI.TupleSelect(hv_Index));
            }
        }
        //Reset changed window settings
        HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
        HOperatorSet.SetPart(hv_WindowHandle, hv_RowI1Part, hv_ColumnI1Part, hv_RowI2Part,
                             hv_ColumnI2Part);

        return;
    }
Example #24
0
        public void DealSmoothing(ParRaisedEdgeSmooth parRaisedEdgeSmooth, HObject ho_ImageBinary, HObject ho_RegionBinary, ResultRaisedEdge result, Hashtable htResult)
        {
            ParSmooth par = parRaisedEdgeSmooth.g_ParSmooth;

            #region 定义
            HObject ho_ROI              = null;
            HObject ho_FilterROI        = null;
            HObject ho_FilterInverseROI = null;
            // Local control variables
            HTuple hv_Width = null, hv_Height = null;
            HTuple hv_RowInit = new HTuple(), hv_ColInit = new HTuple();
            HTuple hv_Row1 = null, hv_Col1 = null;
            HTuple hv_Row2 = null, hv_Col2 = null;
            HTuple hv_AreaDilation = new HTuple(), hv_RowDilation = new HTuple();
            HTuple hv_ColumnDilation = new HTuple(), hv_NumberSmooth = new HTuple();
            HTuple hv_RowS = new HTuple();
            HTuple hv_ColS = new HTuple(), hv_InvertedCol = new HTuple();
            HTuple hv_InvertedRow = new HTuple(), hv_ConcatCol = new HTuple();
            HTuple hv_ConcatRow = new HTuple(), hv_NumberRegionDilation1 = new HTuple();

            // Local iconic variables
            //怎么把图片和region导入
            HObject ho_ImageReduced;
            HObject ho_Region, ho_RegionDilation;
            HObject ho_rectErosion1, ho_BorderOrigin;
            HObject ho_ConnectedRegionDilation = null;
            HObject ho_BinImage = null;
            HObject ho_ImageBinReduced = null, ho_BorderOriginal = null, ho_SmoothedBoaderOriginal = null;
            HObject ho_SelectedSmoothedContours = null, ho_ObjectSelected = null;
            HObject ho_ContourConcat = null, ho_RegionParall = null, ho_ImageParall = null;
            HObject ho_RegionError = null, ho_ConnectedRegions = null, ho_RegionErosionError = null;
            HObject ho_RegionDilationError = null;

            //求取缺陷
            HObject ho_BorderFull           = null;
            HObject ho_SmoothedContoursFull = null;
            HObject ho_ParaFull             = null;
            HObject ho_ParaRegionFull       = null;
            HObject ho_RegionROIFull        = null;
            HObject ho_ErrorFull            = null;
            HObject ho_FilterErrorFull      = null;//崩缺求取中,剪掉台阶面崩缺后的缺陷
            HObject ho_ErrorRegioinFull     = null;
            HObject ho_ErrorConnectFull     = null;
            HObject ho_ErrorSelectFull      = null;
            HObject ho_ErrorXldFull         = null;

            HObject ho_SmoothedContoursFullLongest = null;
            HOperatorSet.GenEmptyObj(out ho_SmoothedContoursFullLongest);

            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_rectErosion1);
            HOperatorSet.GenEmptyObj(out ho_BorderOrigin);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionDilation);
            HOperatorSet.GenEmptyObj(out ho_BinImage);
            HOperatorSet.GenEmptyObj(out ho_ImageBinReduced);
            HOperatorSet.GenEmptyObj(out ho_BorderOriginal);
            HOperatorSet.GenEmptyObj(out ho_SmoothedBoaderOriginal);
            HOperatorSet.GenEmptyObj(out ho_SelectedSmoothedContours);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_ContourConcat);
            HOperatorSet.GenEmptyObj(out ho_RegionParall);
            HOperatorSet.GenEmptyObj(out ho_ImageParall);
            HOperatorSet.GenEmptyObj(out ho_RegionError);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionErosionError);
            HOperatorSet.GenEmptyObj(out ho_RegionDilationError);
            HOperatorSet.GenEmptyObj(out ho_BorderFull);
            HOperatorSet.GenEmptyObj(out ho_SmoothedContoursFull);


            HOperatorSet.GenEmptyObj(out ho_FilterInverseROI);
            HOperatorSet.GenEmptyObj(out ho_FilterErrorFull);
            HOperatorSet.GenEmptyObj(out ho_ParaFull);
            HOperatorSet.GenEmptyObj(out ho_ParaRegionFull);
            #endregion 定义

            try
            {
                int smooth = (par.SmoothValue / 2) * 2 + 1;//平滑系数只能是奇数

                HOperatorSet.GetImageSize(ho_ImageBinary, out hv_Width, out hv_Height);
                //求取ROI
                FunROI funCirROI = new FunROI();
                bool   blResult  = false;
                string anno      = "";
                ho_ROI = funCirROI.CreateOneROI(parRaisedEdgeSmooth.g_ParROI.g_ParROIExcute_L[0], htResult, out blResult, out anno);


                //对ROI区域进行腐蚀,剔除多余轮廓的边界
                HOperatorSet.ErosionCircle(ho_ROI, out ho_rectErosion1, 20);
                HOperatorSet.ReduceDomain(ho_ImageBinary, ho_rectErosion1, out ho_ImageReduced);

                //通过ROI截图
                HOperatorSet.Intersection(ho_RegionBinary, ho_ROI, out ho_RegionDilation);

                //对二值化图片求取轮廓
                HOperatorSet.ThresholdSubPix(ho_ImageReduced, out ho_BorderOrigin, 100);

                //迭代十次
                for (int i = 0; i < par.Num; i++)
                {
                    hv_Row1 = new HTuple();
                    hv_Col1 = new HTuple();
                    hv_Row2 = new HTuple();
                    hv_Col2 = new HTuple();

                    //剔除主体之外的噪声点
                    ho_ConnectedRegionDilation.Dispose();
                    HOperatorSet.Connection(ho_RegionDilation, out ho_ConnectedRegionDilation);
                    HOperatorSet.AreaCenter(ho_ConnectedRegionDilation, out hv_AreaDilation, out hv_RowDilation, out hv_ColumnDilation);
                    ho_RegionDilation.Dispose();

                    //保留主体
                    HOperatorSet.SelectShape(ho_ConnectedRegionDilation, out ho_RegionDilation, "area", "and", 50000, int.MaxValue);
                    //主题二值化以后的图片
                    ho_BinImage.Dispose();
                    HOperatorSet.RegionToBin(ho_RegionDilation, out ho_BinImage, 255, 0, hv_Width, hv_Height);

                    //二值化缩减轮廓
                    ho_ImageBinReduced.Dispose();
                    HOperatorSet.ReduceDomain(ho_BinImage, ho_rectErosion1, out ho_ImageBinReduced);

                    //***************************
                    //生成带有方向梯度的边界,用平滑后的边界和原始边界的平移生成轮廓处理
                    //生成内边界
                    ho_BorderOriginal.Dispose();
                    HOperatorSet.ThresholdSubPix(ho_ImageBinReduced, out ho_BorderOriginal, 100);

                    //角点非圆弧
                    ho_SmoothedBoaderOriginal.Dispose();
                    HOperatorSet.SmoothContoursXld(ho_BorderOriginal, out ho_SmoothedBoaderOriginal, smooth);

                    //筛选轮廓
                    ho_SelectedSmoothedContours.Dispose();
                    HOperatorSet.SelectContoursXld(ho_SmoothedBoaderOriginal, out ho_SelectedSmoothedContours, "contour_length", par.SelectAreaLow, 99999999, -0.5, 0.5);
                    HOperatorSet.CountObj(ho_SelectedSmoothedContours, out hv_NumberSmooth);
                    if ((int)(new HTuple(hv_NumberSmooth.TupleGreater(1))) != 0)
                    {
                        int numSmooth = hv_NumberSmooth.ToIArr()[0] - 1;
                        for (int j = 0; j < numSmooth; j++)
                        {
                            ho_ObjectSelected.Dispose();
                            HOperatorSet.SelectObj(ho_SelectedSmoothedContours, out ho_ObjectSelected, j + 1);
                            HOperatorSet.GetContourXld(ho_ObjectSelected, out hv_RowS, out hv_ColS);
                            HOperatorSet.TupleConcat(hv_RowInit, hv_RowS, out hv_RowInit);
                            HOperatorSet.TupleConcat(hv_ColInit, hv_ColS, out hv_ColInit);
                        }
                    }
                    else
                    {
                        HOperatorSet.GetContourXld(ho_SelectedSmoothedContours, out hv_RowInit, out hv_ColInit);
                    }

                    //生成平行轮廓的点
                    HTuple[] rowCol = null;
                    GenParallPoint(par.Position, par.DefectType, (int)par.GapIn, (int)par.GapOut, hv_RowInit, hv_ColInit, out rowCol);
                    hv_Row1 = rowCol[0];
                    hv_Col1 = rowCol[1];
                    hv_Row2 = rowCol[2];
                    hv_Col2 = rowCol[3];
                    //生成平行轮廓包围的区域
                    HOperatorSet.TupleInverse(hv_Col2, out hv_InvertedCol);
                    HOperatorSet.TupleInverse(hv_Row2, out hv_InvertedRow);

                    HOperatorSet.TupleConcat(hv_Col1, hv_InvertedCol, out hv_ConcatCol);
                    HOperatorSet.TupleConcat(hv_Row1, hv_InvertedRow, out hv_ConcatRow);

                    //select_contours_xld (SmoothedContours, SelectedSmoothedContours, 'contour_length', 50, 99999999, -0.5, 0.5)

                    //HOperatorSet.SelectContoursXld(ho_ContourConcat, out ho_ContourConcat, "contour_length", par.ErrorAreaValue, 99999999, -0.5, 0.5);

                    if (hv_ConcatRow.Length == 0)
                    {
                        result.LevelError_e = LevelError_enum.Error;
                        result.Annotation   = par.NameCell.ToString() + "hv_ConcatRow的数量为0";
                        result.SetDefault();
                        return;
                    }

                    ho_ContourConcat.Dispose();
                    HOperatorSet.GenContourPolygonXld(out ho_ContourConcat, hv_ConcatRow, hv_ConcatCol);

                    //生成平行轮廓region
                    ho_RegionParall.Dispose();
                    HOperatorSet.GenRegionContourXld(ho_ContourConcat, out ho_RegionParall, "filled");

                    ho_ImageParall.Dispose();
                    HOperatorSet.ReduceDomain(ho_BinImage, ho_RegionParall, out ho_ImageParall);
                    ho_RegionError.Dispose();
                    HOperatorSet.Threshold(ho_ImageParall, out ho_RegionError, 100, 255);
                    ho_ConnectedRegions.Dispose();
                    HOperatorSet.Connection(ho_RegionError, out ho_ConnectedRegions);

                    //轮廓的内边界先外移1个像素,再膨胀靠近(忽略最小1个像素的地方)
                    ho_RegionErosionError.Dispose();
                    HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_RegionErosionError, "area", "and", par.ErrorAreaValue, 99999999);

                    //膨胀缺陷
                    ho_RegionDilationError.Dispose();
                    HOperatorSet.DilationCircle(ho_RegionErosionError, out ho_RegionDilationError, par.DilationDefectValue);

                    //剔除残留
                    HOperatorSet.Difference(ho_RegionDilation, ho_RegionDilationError, out ho_RegionDilation);

                    HOperatorSet.CountObj(ho_RegionDilationError, out hv_NumberRegionDilation1);
                    if ((int)(new HTuple(hv_NumberRegionDilation1.TupleEqual(0))) != 0)
                    {
                        break;
                    }
                }

                //完整图片
                ho_BinImage.Dispose();
                HOperatorSet.RegionToBin(ho_RegionDilation, out ho_BinImage, 255, 0, hv_Width, hv_Height);

                ho_ImageBinReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_BinImage, ho_rectErosion1, out ho_ImageBinReduced);

                //完整轮廓
                ho_BorderFull.Dispose();
                HOperatorSet.ThresholdSubPix(ho_ImageBinReduced, out ho_BorderFull, 100);

                ho_SmoothedContoursFull.Dispose();
                HOperatorSet.SmoothContoursXld(ho_BorderFull, out ho_SmoothedContoursFull, smooth);

                HTuple hv_ho_SmoothedContoursFullNumber = null;
                HOperatorSet.CountObj(ho_SmoothedContoursFull, out hv_ho_SmoothedContoursFullNumber);//计算平滑区域的xld个数

                //如果完整轮廓为空,则返回
                if (hv_ho_SmoothedContoursFullNumber == 0)
                {
                    result.LevelError_e = LevelError_enum.Error;
                    result.Annotation   = par.NameCell.ToString() + "ho_SmoothedContoursFull的数量为0";
                    result.SetDefault();
                    return;
                }
                //计算完整轮廓的长度
                HTuple hv_SmoothedContoursFullLength = null;
                HOperatorSet.LengthXld(ho_SmoothedContoursFull, out hv_SmoothedContoursFullLength);
                //找到完整轮廓中最长的一段,剔除干扰小段
                HOperatorSet.SelectObj(ho_SmoothedContoursFull, out ho_SmoothedContoursFullLongest, (((hv_SmoothedContoursFullLength.TupleSortIndex()
                                                                                                       )).TupleSelect((new HTuple(hv_SmoothedContoursFullLength.TupleLength())) - 1)) + 1);


                HTuple[] rowColFull = null;
                HOperatorSet.GetContourXld(ho_SmoothedContoursFullLongest, out hv_RowInit, out hv_ColInit);
                GenParallPoint(par.Position, par.DefectType, 0, (int)par.GapOut, hv_RowInit, hv_ColInit, out rowColFull);
                hv_Row1 = rowColFull[0];
                hv_Col1 = rowColFull[1];
                hv_Row2 = rowColFull[2];
                hv_Col2 = rowColFull[3];
                //生成平行轮廓包围的区域
                HOperatorSet.TupleInverse(hv_Col2, out hv_InvertedCol);
                HOperatorSet.TupleInverse(hv_Row2, out hv_InvertedRow);

                HOperatorSet.TupleConcat(hv_Col1, hv_InvertedCol, out hv_ConcatCol);
                HOperatorSet.TupleConcat(hv_Row1, hv_InvertedRow, out hv_ConcatRow);

                ho_ContourConcat.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_ContourConcat, hv_ConcatRow, hv_ConcatCol);
                HOperatorSet.GenRegionContourXld(ho_ContourConcat, out ho_RegionROIFull, "filled");
                HOperatorSet.ReduceDomain(ho_ImageBinary, ho_RegionROIFull, out ho_ErrorFull);

                ho_FilterErrorFull.Dispose();
                ho_FilterInverseROI.Dispose();
                if (par.DefectType == "Shell" && parRaisedEdgeSmooth.g_ParROI.g_ParROIExcute_L.Count > 1)
                {
                    ho_FilterROI = funCirROI.CreateOneROI(parRaisedEdgeSmooth.g_ParROI.g_ParROIExcute_L[1], htResult, out blResult, out anno);
                    HOperatorSet.Complement(ho_FilterROI, out ho_FilterInverseROI);
                    HOperatorSet.ReduceDomain(ho_ErrorFull, ho_FilterInverseROI, out ho_FilterErrorFull);
                }
                else
                {
                    ho_FilterErrorFull = ho_ErrorFull.Clone();
                }

                HOperatorSet.Threshold(ho_FilterErrorFull, out ho_ErrorRegioinFull, 100, 255);
                HOperatorSet.Connection(ho_ErrorRegioinFull, out ho_ErrorConnectFull);

                HTuple numError = 0;
                HTuple rowFull  = null;
                HTuple colFull  = null;
                HTuple disMin   = null;
                HTuple disMax   = null;

                HTuple disMinSort      = null;
                HTuple disMinSortIndex = null;
                HTuple areaError       = null;
                HTuple rowError        = null;
                HTuple colError        = null;
                HOperatorSet.CountObj(ho_ErrorConnectFull, out numError);
                HObject ho_ErrorSelectedPart;
                for (int i = 0; i < numError; i++)
                {
                    HOperatorSet.SelectObj(ho_ErrorConnectFull, out ho_ErrorSelectedPart, i + 1);
                    HOperatorSet.GenContourRegionXld(ho_ErrorSelectedPart, out ho_ErrorXldFull, "border");
                    HOperatorSet.GetContourXld(ho_ErrorXldFull, out rowFull, out colFull);
                    HOperatorSet.DistancePc(ho_SmoothedContoursFullLongest, rowFull, colFull, out disMin, out disMax);
                    HOperatorSet.TupleSort(disMin, out disMinSort);
                    HOperatorSet.TupleSortIndex(disMin, out disMinSortIndex);
                    //for (int j = 0; j < disMax.Length; j++)
                    //{
                    if (disMinSort[disMinSort.Length - 1] > par.NormalTh)
                    {
                        HOperatorSet.AreaCenter(ho_ErrorSelectedPart, out areaError, out rowError, out colError);
                        result.X_L.Add(colFull[disMinSortIndex[disMinSort.Length - 1].I]);
                        result.Y_L.Add(rowFull[disMinSortIndex[disMinSort.Length - 1].I]);
                        result.R_L.Add(0);
                        double dist = disMinSort[disMinSort.Length - 1];
                        result.Height_L.Add(Math.Round(dist, 1));
                        result.Area_L.Add(areaError.ToDArr()[0]);
                        break;
                    }
                    //}
                }

                //区域个数
                result.Num = result.X_L.Count;
                if (result.Num == 0)
                {
                    //如果前一次结果为error,那么即使这次的结果是OK,也要保持ERROR的状态
                    if (result.LevelError_e == LevelError_enum.Error)
                    {
                        result.SetDefault();
                    }
                    else
                    {
                        result.LevelError_e = LevelError_enum.OK;
                        result.Annotation   = par.NameCell.ToString() + "缺陷个数为0";
                        result.SetDefault();
                    }
                }

                //添加显示
                AddDisplay(par.g_ParOutput, result);
            }
            catch (Exception ex)
            {
                result.LevelError_e = LevelError_enum.Error;
                result.Annotation   = par.NameCell.ToString() + ex.Message.ToString();
                result.SetDefault();
                Log.L_I.WriteError(NameClass, ex);
            }
            finally
            {
                #region 记录
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_ImageBinary", ho_ImageBinary);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_ROI", ho_ROI);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_FilterROI", ho_FilterROI);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_rectErosion1", ho_rectErosion1);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_ImageReduced", ho_ImageReduced);

                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_Region", ho_Region);

                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_RegionDilation", ho_RegionDilation);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_BorderOrigin", ho_BorderOrigin);

                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_ConnectedRegionDilation", ho_ConnectedRegionDilation);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_BinImage", ho_BinImage);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_BorderOriginal", ho_BorderOriginal);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_SmoothedBoaderOriginal", ho_SmoothedBoaderOriginal);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_SelectedSmoothedContours", ho_SelectedSmoothedContours);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_ObjectSelected", ho_ObjectSelected);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_ContourConcat", ho_ContourConcat);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_RegionParall", ho_RegionParall);


                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_FilterErrorFull", ho_FilterErrorFull);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_ImageParall", ho_ImageParall);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_RegionError", ho_RegionError);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_ConnectedRegions", ho_ConnectedRegions);

                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_RegionErosionError", ho_RegionErosionError);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_RegionDilationError", ho_RegionDilationError);

                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_BorderFull", ho_BorderFull);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_SmoothedContoursFull", ho_SmoothedContoursFull);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_SmoothedContoursFullLongest", ho_SmoothedContoursFullLongest);

                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_RegionROIFull", ho_RegionROIFull);
                RecordHoject(parRaisedEdgeSmooth, result, NameClass, "ho_ErrorConnectFull", ho_ErrorConnectFull);
                #endregion 记录
            }
        }
Example #25
0
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Region, ho_SmoothedImage, ho_ReducedImage;
        HObject ho_ModelImages, ho_ModelRegions, ho_ConnectedRegions;
        HObject ho_SelectedRegions, ho_ModelContours, ho_TestImage = null;

        // Local control variables

        HTuple hv_WindowHandle = null, hv_Colors = null;
        HTuple hv_MinScore = null, hv_NumMatches = null, hv_AcqHandle = null;
        HTuple hv_Size = null, hv_Coeffs = null, hv_ModelID = null;
        HTuple hv_NumLevels = null, hv_AngleStart = null, hv_AngleExtent = null;
        HTuple hv_AngleStep = null, hv_ScaleMin = null, hv_ScaleMax = null;
        HTuple hv_ScaleStep = null, hv_Metric = null, hv_MinContrast = null;
        HTuple hv_keepGrabbing = null, hv_Row = new HTuple(), hv_Column = new HTuple();
        HTuple hv_Button = new HTuple(), hv_Exception = new HTuple();
        HTuple hv_ErrorCode = new HTuple(), hv_Angle = new HTuple();
        HTuple hv_Score = new HTuple(), hv_ScoreLength = new HTuple();
        HTuple hv_BestMatchScore = new HTuple(), hv_location = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Region);
        HOperatorSet.GenEmptyObj(out ho_SmoothedImage);
        HOperatorSet.GenEmptyObj(out ho_ReducedImage);
        HOperatorSet.GenEmptyObj(out ho_ModelImages);
        HOperatorSet.GenEmptyObj(out ho_ModelRegions);
        HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
        HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
        HOperatorSet.GenEmptyObj(out ho_ModelContours);
        HOperatorSet.GenEmptyObj(out ho_TestImage);
        try
        {
            // import(...); only in hdevelop
            // import(...); only in hdevelop

            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.CloseWindow(HDevWindowStack.Pop());
            }
            HOperatorSet.SetWindowAttr("background_color", "white");
            HOperatorSet.OpenWindow(0, 0, 1024, 768, 0, "", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
            // dev_update_time(...); only in hdevelop
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3);
            }
            hv_Colors     = new HTuple();
            hv_Colors[0]  = "red";
            hv_Colors[1]  = "green";
            hv_Colors[2]  = "cyan";
            hv_MinScore   = 0.5;
            hv_NumMatches = 1;

            //global object Image


            HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1,
                                          "default", -1, "false", "default", "00111cf51192_SENSORTECHNOLOGYCOLTD_STCGE83A",
                                          0, -1, out hv_AcqHandle);
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTimeRaw", 9000);
            disp_message(hv_WindowHandle, "Press F5 to begin live grab. Press Left mouse click to exit live acquisition...",
                         "window", -1, -1, "black", "true");
            // stop(...); only in hdevelop
            abshear_async_acquisition(hv_WindowHandle, hv_AcqHandle);
            disp_message(hv_WindowHandle, "Hold down Left Mouse Button to select region",
                         "window", -1, -1, "black", "true");

            ho_Region.Dispose();
            HOperatorSet.DrawRegion(out ho_Region, hv_WindowHandle);

            HOperatorSet.InfoSmooth("deriche2", 0.3, out hv_Size, out hv_Coeffs);
            ho_SmoothedImage.Dispose();
            HOperatorSet.SmoothImage(ExpGetGlobalVar_Image(), out ho_SmoothedImage, "deriche2",
                                     0.3);

            ho_ReducedImage.Dispose();
            HOperatorSet.ReduceDomain(ho_SmoothedImage, ho_Region, out ho_ReducedImage);

            ho_ModelImages.Dispose(); ho_ModelRegions.Dispose();
            HOperatorSet.InspectShapeModel(ho_ReducedImage, out ho_ModelImages, out ho_ModelRegions,
                                           1, 160);

            //Since the shape models contain a few extraneous edges, they will be
            //removed here to give a slightly nicer visualization.
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_ModelRegions, out ho_ConnectedRegions);
            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                                     "and", 20, 100000);
            ho_ModelRegions.Dispose();
            HOperatorSet.Union1(ho_SelectedRegions, out ho_ModelRegions);
            ho_ModelContours.Dispose();
            HOperatorSet.GenContoursSkeletonXld(ho_ModelRegions, out ho_ModelContours,
                                                1, "filter");

            HOperatorSet.CreateShapeModel(ho_ReducedImage, "auto", -0.39, 0.79, "auto",
                                          "auto", "use_polarity", "auto", "auto", out hv_ModelID);
            HOperatorSet.GetShapeModelParams(hv_ModelID, out hv_NumLevels, out hv_AngleStart,
                                             out hv_AngleExtent, out hv_AngleStep, out hv_ScaleMin, out hv_ScaleMax,
                                             out hv_ScaleStep, out hv_Metric, out hv_MinContrast);

            abshear_serialize_and_save(ho_SmoothedImage, hv_ModelID);

            hv_keepGrabbing = 1;
            while ((int)(hv_keepGrabbing) != 0)
            {
                try
                {
                    HOperatorSet.GetMposition(hv_WindowHandle, out hv_Row, out hv_Column, out hv_Button);
                }
                // catch (Exception)
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    hv_ErrorCode = hv_Exception.TupleSelect(0);
                }

                if ((int)(new HTuple(hv_Button.TupleEqual(1))) != 0)
                {
                    hv_keepGrabbing = 0;
                }

                //Grab and display an image for the matching
                ho_TestImage.Dispose();
                HOperatorSet.GrabImageAsync(out ho_TestImage, hv_AcqHandle, -1);
                ho_SmoothedImage.Dispose();
                HOperatorSet.SmoothImage(ho_TestImage, out ho_SmoothedImage, "deriche2",
                                         0.3);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_TestImage, HDevWindowStack.GetActive());
                }
                HOperatorSet.FindShapeModel(ho_TestImage, hv_ModelID, -0.39, 0.79, hv_MinScore,
                                            hv_NumMatches, 0.5, "least_squares", 0, 0.9, out hv_Row, out hv_Column,
                                            out hv_Angle, out hv_Score);

                HDevWindowStack.SetActive(hv_WindowHandle);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                }

                dev_display_shape_matching_results(hv_ModelID, "green", hv_Row, hv_Column,
                                                   hv_Angle, 1.0, 1.0, 0);
                HOperatorSet.TupleLength(hv_Score, out hv_ScoreLength);
                if ((int)(new HTuple(hv_ScoreLength.TupleGreater(0))) != 0)
                {
                    HOperatorSet.TupleFirstN(hv_Score, 0, out hv_BestMatchScore);
                    disp_message(hv_WindowHandle, "BestMatch: " + hv_BestMatchScore, "window",
                                 -1, -1, "black", "true");
                    hv_location = (("Location: " + hv_Row) + new HTuple(", ")) + hv_Column;
                    disp_message(hv_WindowHandle, hv_location, "window", 40, -1, "black", "true");
                }
                else
                {
                    disp_message(hv_WindowHandle, "No match found...", "window", -1, -1, "black",
                                 "true");
                }
            }

            HOperatorSet.CloseFramegrabber(hv_AcqHandle);
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_Region.Dispose();
            ho_SmoothedImage.Dispose();
            ho_ReducedImage.Dispose();
            ho_ModelImages.Dispose();
            ho_ModelRegions.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();
            ho_ModelContours.Dispose();
            ho_TestImage.Dispose();

            throw HDevExpDefaultException;
        }
        ho_Region.Dispose();
        ho_SmoothedImage.Dispose();
        ho_ReducedImage.Dispose();
        ho_ModelImages.Dispose();
        ho_ModelRegions.Dispose();
        ho_ConnectedRegions.Dispose();
        ho_SelectedRegions.Dispose();
        ho_ModelContours.Dispose();
        ho_TestImage.Dispose();
    }
        internal static void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem, HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
        {
            // Local control variables

            HTuple hv_Red, hv_Green, hv_Blue, hv_Row1Part;
            HTuple hv_Column1Part, hv_Row2Part, hv_Column2Part, hv_RowWin;
            HTuple hv_ColumnWin, hv_WidthWin, hv_HeightWin, hv_MaxAscent;
            HTuple hv_MaxDescent, hv_MaxWidth, hv_MaxHeight, hv_R1 = new HTuple();
            HTuple hv_C1 = new HTuple(), hv_FactorRow = new HTuple(), hv_FactorColumn = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Index = new HTuple(), hv_Ascent = new HTuple();
            HTuple hv_Descent = new HTuple(), hv_W = new HTuple(), hv_H = new HTuple();
            HTuple hv_FrameHeight = new HTuple(), hv_FrameWidth = new HTuple();
            HTuple hv_R2 = new HTuple(), hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
            HTuple hv_Exception = new HTuple(), hv_CurrentColor = new HTuple();

            HTuple hv_Color_COPY_INP_TMP  = hv_Color.Clone();
            HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
            HTuple hv_Row_COPY_INP_TMP    = hv_Row.Clone();
            HTuple hv_String_COPY_INP_TMP = hv_String.Clone();

            // Initialize local and output iconic variables

            HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue);
            HOperatorSet.GetPart(hv_WindowHandle, out hv_Row1Part, out hv_Column1Part, out hv_Row2Part,
                                 out hv_Column2Part);
            HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowWin, out hv_ColumnWin,
                                          out hv_WidthWin, out hv_HeightWin);
            HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
            //
            //default settings
            if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Row_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Column_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
            {
                hv_Color_COPY_INP_TMP = "";
            }
            //
            hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n");
            //
            //Estimate extentions of text depending on font size.
            HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent,
                                        out hv_MaxWidth, out hv_MaxHeight);
            if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
            {
                hv_R1 = hv_Row_COPY_INP_TMP.Clone();
                hv_C1 = hv_Column_COPY_INP_TMP.Clone();
            }
            else
            {
                //transform image to window coordinates
                hv_FactorRow    = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);
                hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);
                hv_R1           = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow;
                hv_C1           = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn;
            }
            //
            //display text box depending on text size
            if ((int)(new HTuple(hv_Box.TupleEqual("true"))) != 0)
            {
                //calculate box extents
                hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " ";
                hv_Width = new HTuple();
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                                      )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    HOperatorSet.GetStringExtents(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
                                                      hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
                    hv_Width = hv_Width.TupleConcat(hv_W);
                }
                hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                            ));
                hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
                hv_R2         = hv_R1 + hv_FrameHeight;
                hv_C2         = hv_C1 + hv_FrameWidth;
                //display rectangles
                HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode);
                HOperatorSet.SetDraw(hv_WindowHandle, "fill");
                HOperatorSet.SetColor(hv_WindowHandle, "light gray");
                HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 3, hv_C1 + 3, hv_R2 + 3, hv_C2 + 3);
                HOperatorSet.SetColor(hv_WindowHandle, "white");
                HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
                HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode);
            }
            else if ((int)(new HTuple(hv_Box.TupleNotEqual("false"))) != 0)
            {
                hv_Exception = "Wrong value of control parameter Box";
                throw new HalconException(hv_Exception);
            }
            //Write text.
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                                  )) - 1); hv_Index = (int)hv_Index + 1)
            {
                hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                                                                                           )));
                if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
                                                                                                  "auto")))) != 0)
                {
                    HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor);
                }
                else
                {
                    HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
                }
                hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
                HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_C1);
                HOperatorSet.WriteString(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
                                             hv_Index));
            }
            //reset changed window settings
            HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
            HOperatorSet.SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
                                 hv_Column2Part);

            return;
        }
        private void action()
        {
            HTuple  hv_Area1 = null, hv_Row = null, hv_Column = null;
            HTuple  hv_Area2 = null, hv_Area3 = null, hv_Area4 = null, hv_Area5 = null, hv_Area6 = null;
            HTuple  hv_l1 = null, hv_l2 = null;
            HObject ho_Region, ho_Rectangle1, ho_Rectangle2, ho_Rectangle5, ho_Rectangle6, ho_RegionIntersection5, ho_RegionIntersection6;
            HObject ho_Rectangle3, ho_Rectangle4, ho_RegionIntersection1;
            HObject ho_RegionIntersection2, ho_RegionIntersection3;
            HObject ho_RegionIntersection4;

            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_Rectangle2);
            HOperatorSet.GenEmptyObj(out ho_Rectangle3);
            HOperatorSet.GenEmptyObj(out ho_Rectangle4);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection1);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection2);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection3);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection4);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection5);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection6);
            HOperatorSet.GenEmptyObj(out ho_Rectangle5);
            HOperatorSet.GenEmptyObj(out ho_Rectangle6);

            ho_Region.Dispose();
            HOperatorSet.Threshold(Image, out ho_Region, 0, 64);
            ho_Rectangle1.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle1, 5, 5, 75, 1900);
            ho_Rectangle2.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle2, 550, 5, 650, 1900);
            ho_Rectangle3.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle3, 1160, 5, 1230, 1900);
            ho_Rectangle4.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle4, 0, 1000, 1236, 1400);
            ho_Rectangle5.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle5, 5, 1000, 75, 1050);
            ho_Rectangle6.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle6, 1160, 1000, 1230, 1050);
            ho_RegionIntersection5.Dispose();
            HOperatorSet.Intersection(ho_Region, ho_Rectangle5, out ho_RegionIntersection5
                                      );
            ho_RegionIntersection6.Dispose();
            HOperatorSet.Intersection(ho_Region, ho_Rectangle6, out ho_RegionIntersection6
                                      );
            ho_RegionIntersection1.Dispose();
            HOperatorSet.Intersection(ho_Region, ho_Rectangle1, out ho_RegionIntersection1
                                      );
            ho_RegionIntersection2.Dispose();
            HOperatorSet.Intersection(ho_Region, ho_Rectangle2, out ho_RegionIntersection2
                                      );
            ho_RegionIntersection3.Dispose();
            HOperatorSet.Intersection(ho_Region, ho_Rectangle3, out ho_RegionIntersection3
                                      );
            ho_RegionIntersection4.Dispose();
            HOperatorSet.Intersection(ho_Region, ho_Rectangle4, out ho_RegionIntersection4
                                      );
            HOperatorSet.AreaCenter(ho_RegionIntersection1, out hv_Area1, out hv_Row, out hv_Column);
            HOperatorSet.AreaCenter(ho_RegionIntersection2, out hv_Area2, out hv_Row, out hv_Column);
            HOperatorSet.AreaCenter(ho_RegionIntersection3, out hv_Area3, out hv_Row, out hv_Column);
            HOperatorSet.AreaCenter(ho_RegionIntersection4, out hv_Area4, out hv_Row, out hv_Column);
            HOperatorSet.AreaCenter(ho_RegionIntersection5, out hv_Area5, out hv_Row, out hv_Column);
            HOperatorSet.AreaCenter(ho_RegionIntersection6, out hv_Area6, out hv_Row, out hv_Column);
            if ((int)((new HTuple(hv_Area5.TupleEqual(0))).TupleAnd(new HTuple(hv_Area6.TupleEqual(
                                                                                   0)))) != 0)
            {
                hv_l1 = (hv_Area2 / 100) - ((hv_Area1 + hv_Area3) / 140);
            }
            else if ((int)((new HTuple(hv_Area5.TupleEqual(0))).TupleAnd(new HTuple(hv_Area6.TupleGreater(
                                                                                        0)))) != 0)
            {
                hv_l1 = (hv_Area2 / 100) - (hv_Area1 / 70);
            }
            else if ((int)((new HTuple(hv_Area5.TupleGreater(0))).TupleAnd(new HTuple(hv_Area6.TupleEqual(
                                                                                          0)))) != 0)
            {
                hv_l1 = (hv_Area2 / 100) - (hv_Area3 / 70);
            }
            hv_l2 = (double)hv_Area4 / 400;
            HTuple hv_result = GetHv_result();

            hv_result = hv_result.TupleConcat("length1");
            hv_result = hv_result.TupleConcat(hv_l1 * pixeldist);
            hv_result = hv_result.TupleConcat("length2");
            hv_result = hv_result.TupleConcat(hv_l2 * pixeldist);
            result    = hv_result.Clone();

            ho_Region.Dispose();
            ho_Rectangle1.Dispose();
            ho_Rectangle2.Dispose();
            ho_Rectangle3.Dispose();
            ho_Rectangle4.Dispose();
            ho_Rectangle5.Dispose();
            ho_Rectangle6.Dispose();
            ho_RegionIntersection1.Dispose();
            ho_RegionIntersection2.Dispose();
            ho_RegionIntersection3.Dispose();
            ho_RegionIntersection4.Dispose();
            ho_RegionIntersection5.Dispose();
            ho_RegionIntersection6.Dispose();
        }
    /// <summary>
    /// Evaluates the area covered by the calibration plate in the calibration
    /// image.
    /// </summary>
    public void eval_caltab_size(HObject ho_Image,
                                 HObject ho_Caltab,
                                 HObject ho_Marks,
                                 out HTuple hv_SizeScore)
    {
        // Local iconic variables

        HObject ho_Region = null, ho_RegionUnion = null;


        // Local control variables

        HTuple hv_Width, hv_Height, hv_Number, hv_Row1 = new HTuple();
        HTuple hv_Column1 = new HTuple(), hv_Phi1 = new HTuple(), hv_Length1 = new HTuple();
        HTuple hv_Length2 = new HTuple(), hv_Area = new HTuple(), hv_Row = new HTuple();
        HTuple hv_Column = new HTuple(), hv_MinRatio, hv_MaxRatio;
        HTuple hv_Ratio;

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Region);
        HOperatorSet.GenEmptyObj(out ho_RegionUnion);

        hv_SizeScore = 0.0;
        HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
        HOperatorSet.CountObj(ho_Marks, out hv_Number);
        if ((int)(new HTuple(hv_Number.TupleGreaterEqual(4))) != 0)
        {
            //Best approach: Use the surrounding box of the marks as reference size
            ho_Region.Dispose();
            HOperatorSet.GenRegionContourXld(ho_Marks, out ho_Region, "filled");
            ho_RegionUnion.Dispose();
            HOperatorSet.Union1(ho_Region, out ho_RegionUnion);
            HOperatorSet.SmallestRectangle2(ho_RegionUnion, out hv_Row1, out hv_Column1,
                                            out hv_Phi1, out hv_Length1, out hv_Length2);
            hv_Area = (hv_Length2 * hv_Length1) * 4;
        }
        else
        {
            //If no marks could be found: use the caltab as reference size
            HOperatorSet.AreaCenter(ho_Caltab, out hv_Area, out hv_Row, out hv_Column);
            if ((int)((new HTuple(hv_Area.TupleEqual(0))).TupleOr(new HTuple(hv_Area.TupleEqual(
                                                                                 new HTuple())))) != 0)
            {
                ho_Region.Dispose();
                ho_RegionUnion.Dispose();

                return;
            }
        }
        hv_MinRatio = 0.015;
        hv_MaxRatio = 0.075;
        hv_Ratio    = (hv_Area.TupleReal()) / (hv_Width * hv_Height);
        if ((int)(new HTuple(hv_Ratio.TupleGreater(hv_MinRatio))) != 0)
        {
            hv_SizeScore = (hv_Ratio - hv_MinRatio) / (hv_MaxRatio - hv_MinRatio);
            hv_SizeScore = (((new HTuple(1.0)).TupleConcat(hv_SizeScore))).TupleMin();
        }
        ho_Region.Dispose();
        ho_RegionUnion.Dispose();

        return;
    }
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image, ho_GrayImage3, ho_ImageGauss;
        HObject ho_ModelContours = null, ho_TransContours = null, ho_Rectangle = null;
        HObject ho_ImageReduced = null;

        // Local control variables

        HTuple hv_T1 = null, hv_CheckNO = null, hv_GODS = null;
        HTuple hv_Minspec = null, hv_Setnumber = null, hv_ocvgod = null;
        HTuple hv_RowPlus = null, hv_ColPlus = null, hv_path = null;
        HTuple hv_FileHandle = null, hv_OutLine = null, hv_IsEOF = null;
        HTuple hv_CheckNumber = null, hv_GreedinessNumber = null;
        HTuple hv_Index = null, hv_checkNO = new HTuple(), hv_gods = new HTuple();
        HTuple hv_Mins = new HTuple(), hv_Setn = new HTuple();
        HTuple hv_ocvgodfile = new HTuple(), hv_Setocvchar = new HTuple();
        HTuple hv_ocvchar = new HTuple(), hv_Setrowplus = new HTuple();
        HTuple hv_Setcolplus = new HTuple(), hv_ModelID1 = null;
        HTuple hv_ModelID2 = null, hv_ModelID3 = null, hv_ModelID4 = null;
        HTuple hv_ModelID5 = null, hv_ModelID6 = null, hv_ModelIDs = null;
        HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = new HTuple();
        HTuple hv_Index2 = null, hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
        HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
        HTuple hv_RefRow = new HTuple(), hv_RefColumn = new HTuple();
        HTuple hv_HomMat2D = new HTuple(), hv_Row = new HTuple();
        HTuple hv_Column = new HTuple(), hv_Angle = new HTuple();
        HTuple hv_Score = new HTuple(), hv_I = new HTuple(), hv_OCVHandle = new HTuple();
        HTuple hv_Quality = new HTuple(), hv_Index1 = new HTuple();
        HTuple hv_T2 = null;

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_GrayImage3);
        HOperatorSet.GenEmptyObj(out ho_ImageGauss);
        HOperatorSet.GenEmptyObj(out ho_ModelContours);
        HOperatorSet.GenEmptyObj(out ho_TransContours);
        HOperatorSet.GenEmptyObj(out ho_Rectangle);
        HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        HOperatorSet.CountSeconds(out hv_T1);

        //**外部控制参数读入***
        hv_CheckNO   = new HTuple();
        hv_GODS      = new HTuple();
        hv_Minspec   = new HTuple();
        hv_Setnumber = new HTuple();
        hv_ocvgod    = new HTuple();
        hv_RowPlus   = new HTuple();
        hv_ColPlus   = new HTuple();
        hv_path      = "matchingpara_fengwu_hougai.txt";
        HOperatorSet.OpenFile(hv_path, "input", out hv_FileHandle);
        HOperatorSet.FreadLine(hv_FileHandle, out hv_OutLine, out hv_IsEOF);
        HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                     0), out hv_CheckNumber);
        HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                     1), out hv_GreedinessNumber);
        HTuple end_val15  = hv_CheckNumber - 1;
        HTuple step_val15 = 1;

        for (hv_Index = 0; hv_Index.Continue(end_val15, step_val15); hv_Index = hv_Index.TupleAdd(step_val15))
        {
            HOperatorSet.FreadLine(hv_FileHandle, out hv_OutLine, out hv_IsEOF);
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         0), out hv_checkNO);
            if (hv_CheckNO == null)
            {
                hv_CheckNO = new HTuple();
            }
            hv_CheckNO[hv_Index] = hv_checkNO;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         1), out hv_gods);
            if (hv_GODS == null)
            {
                hv_GODS = new HTuple();
            }
            hv_GODS[hv_Index] = hv_gods;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         2), out hv_Mins);
            if (hv_Minspec == null)
            {
                hv_Minspec = new HTuple();
            }
            hv_Minspec[hv_Index] = hv_Mins;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         3), out hv_Setn);
            if (hv_Setnumber == null)
            {
                hv_Setnumber = new HTuple();
            }
            hv_Setnumber[hv_Index] = hv_Setn;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         4), out hv_ocvgodfile);
            if (hv_ocvgod == null)
            {
                hv_ocvgod = new HTuple();
            }
            hv_ocvgod[hv_Index] = hv_ocvgodfile;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         5), out hv_Setocvchar);
            if (hv_ocvchar == null)
            {
                hv_ocvchar = new HTuple();
            }
            hv_ocvchar[hv_Index] = hv_Setocvchar;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         6), out hv_Setrowplus);
            if (hv_RowPlus == null)
            {
                hv_RowPlus = new HTuple();
            }
            hv_RowPlus[hv_Index] = hv_Setrowplus;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         7), out hv_Setcolplus);
            if (hv_ColPlus == null)
            {
                hv_ColPlus = new HTuple();
            }
            hv_ColPlus[hv_Index] = hv_Setcolplus;
        }

        HOperatorSet.CloseFile(hv_FileHandle);

        //**Log 文件创建***

        HOperatorSet.OpenFile("matching_fengwu_result.csv", "output", out hv_FileHandle);


        //**模板读入***

        HOperatorSet.ReadShapeModel("god_fengwu/01_MOVIE_O.shm", out hv_ModelID1);
        HOperatorSet.ReadShapeModel("god_fengwu/02_MENU_M.shm", out hv_ModelID2);
        HOperatorSet.ReadShapeModel("god_fengwu/03_MOVIE_E.shm", out hv_ModelID3);
        HOperatorSet.ReadShapeModel("god_fengwu/04_jiajian.shm", out hv_ModelID4);
        HOperatorSet.ReadShapeModel("god_fengwu/05_C.shm", out hv_ModelID5);
        HOperatorSet.ReadShapeModel("god_fengwu/06_MOVIE_V.shm", out hv_ModelID6);

        hv_ModelIDs = new HTuple();
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID1);
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID2);
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID3);
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID4);
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID5);
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID6);


        ho_Image.Dispose();
        HOperatorSet.ReadImage(out ho_Image, "fw10.bmp");
        HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
        dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
        HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
        ho_GrayImage3.Dispose();
        HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage3);
        ho_ImageGauss.Dispose();
        HOperatorSet.GaussImage(ho_Image, out ho_ImageGauss, 5);

        HTuple end_val61  = hv_CheckNumber - 1;
        HTuple step_val61 = 1;

        int idx = 0;

        for (hv_Index2 = 0; hv_Index2.Continue(end_val61, step_val61); hv_Index2 = hv_Index2.TupleAdd(step_val61))
        {
            //Matching 01: ************************************************
            //Matching 01: BEGIN of generated code for model initialization
            //Matching 01: ************************************************
            HOperatorSet.SetSystem("border_shape_models", "false");
            //
            //Matching 01: Read the shape model from file
            //
            //Matching 01: Get the model contour for transforming it later into the image
            ho_ModelContours.Dispose();
            HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelIDs.TupleSelect(
                                                   hv_Index2), 1);
            //
            //Matching 01: Set a reference position to show the model
            HOperatorSet.SmallestRectangle1Xld(ho_ModelContours, out hv_Row1, out hv_Column1,
                                               out hv_Row2, out hv_Column2);
            hv_RefRow    = ((hv_Row2.TupleMax()) - (hv_Row1.TupleMin())) / 2;
            hv_RefColumn = ((hv_Column2.TupleMax()) - (hv_Column1.TupleMin())) / 2;
            HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out hv_HomMat2D);
            ho_TransContours.Dispose();
            HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours,
                                               hv_HomMat2D);
            //
            //Matching 01: Display the model contours
            HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
            HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");
            HOperatorSet.DispObj(ho_TransContours, hv_ExpDefaultWinHandle);
            //
            //Matching 01: END of generated code for model initialization
            //Matching 01:  * * * * * * * * * * * * * * * * * * * * * * *
            //Matching 01: BEGIN of generated code for model application
            //
            //Matching 01: Loop over all specified test images

            //Matching 01: Find the model
            HOperatorSet.FindShapeModel(ho_ImageGauss, hv_ModelIDs.TupleSelect(hv_Index2),
                                        (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), hv_Minspec.TupleSelect(
                                            hv_Index2), hv_Setnumber.TupleSelect(hv_Index2), 0.5, "least_squares",
                                        (new HTuple(3)).TupleConcat(1), hv_GreedinessNumber, out hv_Row, out hv_Column,
                                        out hv_Angle, out hv_Score);
            //
            //Matching 01: Transform the model contours into the detected positions
            HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
            for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Score.TupleLength())) - 1); hv_I = (int)hv_I + 1)
            {
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2D);
                HOperatorSet.HomMat2dRotate(hv_HomMat2D, hv_Angle.TupleSelect(hv_I), 0, 0,
                                            out hv_HomMat2D);
                HOperatorSet.HomMat2dTranslate(hv_HomMat2D, hv_Row.TupleSelect(hv_I), hv_Column.TupleSelect(
                                                   hv_I), out hv_HomMat2D);
                ho_TransContours.Dispose();
                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours,
                                                   hv_HomMat2D);
                //HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
                // HOperatorSet.DispObj(ho_TransContours, hv_ExpDefaultWinHandle);
            }

            //
            //Matching 01: Clear model when done

            HOperatorSet.ClearShapeModel(hv_ModelIDs.TupleSelect(hv_Index2));
            //Matching 01: *******************************************
            //Matching 01: END of generated code for model application
            //Matching 01: *******************************************
            //
            //

            //**Matching  Log数据写入外部文件***


            if ((int)(new HTuple(hv_I.TupleEqual(hv_Setnumber.TupleSelect(hv_Index2)))) != 0)
            {
                HOperatorSet.ReadOcv(hv_ocvgod.TupleSelect(hv_Index2), out hv_OCVHandle);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row - (hv_RowPlus.TupleSelect(
                                                                           hv_Index2)), hv_Column - (hv_ColPlus.TupleSelect(hv_Index2)), hv_Row + (hv_RowPlus.TupleSelect(
                                                                                                                                                       hv_Index2)), hv_Column + (hv_ColPlus.TupleSelect(hv_Index2)));
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageGauss, ho_Rectangle, out ho_ImageReduced
                                          );
                HOperatorSet.DoOcvSimple(ho_ImageReduced, hv_OCVHandle, hv_ocvchar.TupleSelect(
                                             hv_Index2), "true", "true", "true", "true", 5, out hv_Quality);
                cs_Quality[idx] = hv_Quality;
                cs_x[idx]       = hv_Column;
                cs_y[idx]       = hv_Row;
                idx++;

                HTuple end_val126  = hv_I;
                HTuple step_val126 = 1;
                for (hv_Index1 = 1; hv_Index1.Continue(end_val126, step_val126); hv_Index1 = hv_Index1.TupleAdd(step_val126))
                {
                    HOperatorSet.FwriteString(hv_FileHandle, ((((((((((((hv_CheckNO.TupleSelect(
                                                                             hv_Index2)) + new HTuple(",")) + (hv_GODS.TupleSelect(hv_Index2))) + new HTuple(",")) + "OK") + new HTuple(",")) + hv_I) + new HTuple(",")) + (hv_Score.TupleSelect(
                                                                                                                                                                                                                                hv_Index1 - 1))) + new HTuple(",")) + "Quality") + new HTuple(",")) + hv_Quality);
                    HOperatorSet.FnewLine(hv_FileHandle);
                }
            }
            else
            {
                HOperatorSet.FwriteString(hv_FileHandle, ((((((((hv_CheckNO.TupleSelect(hv_Index2)) + new HTuple(",")) + (hv_GODS.TupleSelect(
                                                                                                                              hv_Index2))) + new HTuple(",")) + "NG") + new HTuple(",")) + hv_I) + new HTuple(",")) + (hv_Setnumber.TupleSelect(
                                                                                                                                                                                                                           hv_Index2)));
                HOperatorSet.FnewLine(hv_FileHandle);
            }
        }


        HOperatorSet.CountSeconds(out hv_T2);
        HOperatorSet.FwriteString(hv_FileHandle, ("执行时间:" + (hv_T2 - hv_T1)) + "S");
        HOperatorSet.CloseFile(hv_FileHandle);

        ho_Image.Dispose();
        ho_GrayImage3.Dispose();
        ho_ImageGauss.Dispose();
        ho_ModelContours.Dispose();
        ho_TransContours.Dispose();
        ho_Rectangle.Dispose();
        ho_ImageReduced.Dispose();
    }
Example #30
0
        // Chapter: Graphics / Text
        // Short Description: Set font independent of OS
        public void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font,
                                     HTuple hv_Bold, HTuple hv_Slant)
        {
            // Local control variables

            HTuple hv_OS = null;
            HTuple hv_Exception = new HTuple(), hv_BoldString = new HTuple();
            HTuple hv_SlantString = new HTuple(), hv_AllowedFontSizes = new HTuple();
            HTuple hv_Distances = new HTuple(), hv_Indices = new HTuple();
            HTuple hv_Fonts = new HTuple(), hv_FontSelRegexp = new HTuple();
            HTuple hv_FontsCourier = new HTuple();

            HTuple hv_Bold_COPY_INP_TMP  = hv_Bold.Clone();
            HTuple hv_Font_COPY_INP_TMP  = hv_Font.Clone();
            HTuple hv_Size_COPY_INP_TMP  = hv_Size.Clone();
            HTuple hv_Slant_COPY_INP_TMP = hv_Slant.Clone();

            // Initialize local and output iconic variables

            //This procedure sets the text font of the current window with
            //the specified attributes.
            //It is assumed that following fonts are installed on the system:
            //Windows: Courier New, Arial Times New Roman
            //Mac OS X: CourierNewPS, Arial, TimesNewRomanPS
            //Linux: courier, helvetica, times
            //Because fonts are displayed smaller on Linux than on Windows,
            //a scaling factor of 1.25 is used the get comparable results.
            //For Linux, only a limited number of font sizes is supported,
            //to get comparable results, it is recommended to use one of the
            //following sizes: 9, 11, 14, 16, 20, 27
            //(which will be mapped internally on Linux systems to 11, 14, 17, 20, 25, 34)
            //
            //Input parameters:
            //WindowHandle: The graphics window for which the font will be set
            //Size: The font size. If Size=-1, the default of 16 is used.
            //Bold: If set to 'true', a bold font is used
            //Slant: If set to 'true', a slanted font is used
            //
            HOperatorSet.GetSystem("operating_system", out hv_OS);
            // dev_get_preferences(...); only in hdevelop
            // dev_set_preferences(...); only in hdevelop
            if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
                          new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0)
            {
                hv_Size_COPY_INP_TMP = 16;
            }
            if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
            {
                //Set font on Windows systems
                if ((int)((new HTuple((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(
                                          new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(
                                                                                                                           "courier")))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Courier New";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Arial";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Times New Roman";
                }
                if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = 1;
                }
                else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = 0;
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Bold";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_Slant_COPY_INP_TMP = 1;
                }
                else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Slant_COPY_INP_TMP = 0;
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Slant";
                    throw new HalconException(hv_Exception);
                }
                try
                {
                    HOperatorSet.SetFont(hv_WindowHandle, ((((((("-" + hv_Font_COPY_INP_TMP) + "-") + hv_Size_COPY_INP_TMP) + "-*-") + hv_Slant_COPY_INP_TMP) + "-*-*-") + hv_Bold_COPY_INP_TMP) + "-");
                }
                // catch (Exception)
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    //throw (Exception)
                }
            }
            else if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Dar"))) != 0)
            {
                //Set font on Mac OS X systems
                if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_BoldString = "Bold";
                }
                else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_BoldString = "";
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Bold";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_SlantString = "Italic";
                }
                else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_SlantString = "";
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Slant";
                    throw new HalconException(hv_Exception);
                }
                if ((int)((new HTuple((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(
                                          new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(
                                                                                                                           "courier")))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "CourierNewPS";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "Arial";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "TimesNewRomanPS";
                }
                if ((int)((new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))).TupleOr(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual(
                                                                                                       "true")))) != 0)
                {
                    hv_Font_COPY_INP_TMP = ((hv_Font_COPY_INP_TMP + "-") + hv_BoldString) + hv_SlantString;
                }
                hv_Font_COPY_INP_TMP = hv_Font_COPY_INP_TMP + "MT";
                try
                {
                    HOperatorSet.SetFont(hv_WindowHandle, (hv_Font_COPY_INP_TMP + "-") + hv_Size_COPY_INP_TMP);
                }
                // catch (Exception)
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    //throw (Exception)
                }
            }
            else
            {
                //Set font for UNIX systems
                hv_Size_COPY_INP_TMP   = hv_Size_COPY_INP_TMP * 1.25;
                hv_AllowedFontSizes    = new HTuple();
                hv_AllowedFontSizes[0] = 11;
                hv_AllowedFontSizes[1] = 14;
                hv_AllowedFontSizes[2] = 17;
                hv_AllowedFontSizes[3] = 20;
                hv_AllowedFontSizes[4] = 25;
                hv_AllowedFontSizes[5] = 34;
                if ((int)(new HTuple(((hv_AllowedFontSizes.TupleFind(hv_Size_COPY_INP_TMP))).TupleEqual(
                                         -1))) != 0)
                {
                    hv_Distances = ((hv_AllowedFontSizes - hv_Size_COPY_INP_TMP)).TupleAbs();
                    HOperatorSet.TupleSortIndex(hv_Distances, out hv_Indices);
                    hv_Size_COPY_INP_TMP = hv_AllowedFontSizes.TupleSelect(hv_Indices.TupleSelect(
                                                                               0));
                }
                if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(
                                                                                                       "Courier")))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "courier";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "helvetica";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
                {
                    hv_Font_COPY_INP_TMP = "times";
                }
                if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = "bold";
                }
                else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Bold_COPY_INP_TMP = "medium";
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Bold";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0)
                {
                    if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("times"))) != 0)
                    {
                        hv_Slant_COPY_INP_TMP = "i";
                    }
                    else
                    {
                        hv_Slant_COPY_INP_TMP = "o";
                    }
                }
                else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0)
                {
                    hv_Slant_COPY_INP_TMP = "r";
                }
                else
                {
                    hv_Exception = "Wrong value of control parameter Slant";
                    throw new HalconException(hv_Exception);
                }
                try
                {
                    HOperatorSet.SetFont(hv_WindowHandle, ((((((("-adobe-" + hv_Font_COPY_INP_TMP) + "-") + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*");
                }
                // catch (Exception)
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    if ((int)((new HTuple(((hv_OS.TupleSubstr(0, 4))).TupleEqual("Linux"))).TupleAnd(
                                  new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0)
                    {
                        HOperatorSet.QueryFont(hv_WindowHandle, out hv_Fonts);
                        hv_FontSelRegexp = (("^-[^-]*-[^-]*[Cc]ourier[^-]*-" + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP;
                        hv_FontsCourier  = ((hv_Fonts.TupleRegexpSelect(hv_FontSelRegexp))).TupleRegexpMatch(
                            hv_FontSelRegexp);
                        if ((int)(new HTuple((new HTuple(hv_FontsCourier.TupleLength())).TupleEqual(
                                                 0))) != 0)
                        {
                            hv_Exception = "Wrong font name";
                            //throw (Exception)
                        }
                        else
                        {
                            try
                            {
                                HOperatorSet.SetFont(hv_WindowHandle, (((hv_FontsCourier.TupleSelect(
                                                                             0)) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*");
                            }
                            // catch (Exception)
                            catch (HalconException HDevExpDefaultException2)
                            {
                                HDevExpDefaultException2.ToHTuple(out hv_Exception);
                                //throw (Exception)
                            }
                        }
                    }
                    //throw (Exception)
                }
            }
            // dev_set_preferences(...); only in hdevelop

            return;
        }
        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);
        }
Example #32
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;
        }
Example #33
0
        public static void list_image_files(HTuple hv_ImageDirectory,
                                            HTuple hv_Extensions,
                                            HTuple hv_Options,
                                            out HTuple hv_ImageFiles)
        {
            // Local iconic variables
            // Local control variables
            HTuple hv_HalconImages = null, hv_OS = null;
            HTuple hv_Directories = null, hv_Index = null, hv_Length = null;
            HTuple hv_network_drive = null, hv_Substring = new HTuple();
            HTuple hv_FileExists = new HTuple(), hv_AllFiles = new HTuple();
            HTuple hv_i = new HTuple(), hv_Selection = new HTuple();
            HTuple hv_Extensions_COPY_INP_TMP     = hv_Extensions.Clone();
            HTuple hv_ImageDirectory_COPY_INP_TMP = hv_ImageDirectory.Clone();

            // Initialize local and output iconic variables
            //This procedure returns all files in a given directory
            //with one of the suffixes specified in Extensions.
            //
            //input parameters:
            //ImageDirectory: as the name says
            //   If a tuple of directories is given, only the images in the first
            //   existing directory are returned.
            //   If a local directory is not found, the directory is searched
            //   under %HALCONIMAGES%/ImageDirectory. If %HALCONIMAGES% is not set,
            //   %HALCONROOT%/images is used instead.
            //Extensions: A string tuple containing the extensions to be found
            //   e.g. ['png','tif',jpg'] or others
            //If Extensions is set to 'default' or the empty string '',
            //   all image suffixes supported by HALCON are used.
            //Options: as in the operator list_files, except that the 'files'
            //   option is always used. Note that the 'directories' option
            //   has no effect but increases runtime, because only files are
            //   returned.
            //
            //output parameter:
            //ImageFiles: A tuple of all found image file names
            //
            if ((int)((new HTuple((new HTuple(hv_Extensions_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
                                      new HTuple(hv_Extensions_COPY_INP_TMP.TupleEqual(""))))).TupleOr(new HTuple(hv_Extensions_COPY_INP_TMP.TupleEqual(
                                                                                                                      "default")))) != 0)
            {
                hv_Extensions_COPY_INP_TMP     = new HTuple();
                hv_Extensions_COPY_INP_TMP[0]  = "ima";
                hv_Extensions_COPY_INP_TMP[1]  = "tif";
                hv_Extensions_COPY_INP_TMP[2]  = "tiff";
                hv_Extensions_COPY_INP_TMP[3]  = "gif";
                hv_Extensions_COPY_INP_TMP[4]  = "bmp";
                hv_Extensions_COPY_INP_TMP[5]  = "jpg";
                hv_Extensions_COPY_INP_TMP[6]  = "jpeg";
                hv_Extensions_COPY_INP_TMP[7]  = "jp2";
                hv_Extensions_COPY_INP_TMP[8]  = "jxr";
                hv_Extensions_COPY_INP_TMP[9]  = "png";
                hv_Extensions_COPY_INP_TMP[10] = "pcx";
                hv_Extensions_COPY_INP_TMP[11] = "ras";
                hv_Extensions_COPY_INP_TMP[12] = "xwd";
                hv_Extensions_COPY_INP_TMP[13] = "pbm";
                hv_Extensions_COPY_INP_TMP[14] = "pnm";
                hv_Extensions_COPY_INP_TMP[15] = "pgm";
                hv_Extensions_COPY_INP_TMP[16] = "ppm";
                //
            }
            if ((int)(new HTuple(hv_ImageDirectory_COPY_INP_TMP.TupleEqual(""))) != 0)
            {
                hv_ImageDirectory_COPY_INP_TMP = ".";
            }
            HOperatorSet.GetSystem("image_dir", out hv_HalconImages);
            HOperatorSet.GetSystem("operating_system", out hv_OS);
            if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
            {
                hv_HalconImages = hv_HalconImages.TupleSplit(";");
            }
            else
            {
                hv_HalconImages = hv_HalconImages.TupleSplit(":");
            }
            hv_Directories = hv_ImageDirectory_COPY_INP_TMP.Clone();
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_HalconImages.TupleLength()
                                                                  )) - 1); hv_Index = (int)hv_Index + 1)
            {
                hv_Directories = hv_Directories.TupleConcat(((hv_HalconImages.TupleSelect(hv_Index)) + "/") + hv_ImageDirectory_COPY_INP_TMP);
            }
            HOperatorSet.TupleStrlen(hv_Directories, out hv_Length);
            HOperatorSet.TupleGenConst(new HTuple(hv_Length.TupleLength()), 0, out hv_network_drive);
            if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
            {
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_Length.TupleLength())) - 1); hv_Index = (int)hv_Index + 1)
                {
                    if ((int)(new HTuple(((((hv_Directories.TupleSelect(hv_Index))).TupleStrlen()
                                           )).TupleGreater(1))) != 0)
                    {
                        HOperatorSet.TupleStrFirstN(hv_Directories.TupleSelect(hv_Index), 1, out hv_Substring);
                        if ((int)(new HTuple(hv_Substring.TupleEqual("//"))) != 0)
                        {
                            if (hv_network_drive == null)
                            {
                                hv_network_drive = new HTuple();
                            }
                            hv_network_drive[hv_Index] = 1;
                        }
                    }
                }
            }
            hv_ImageFiles = new HTuple();
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_Directories.TupleLength()
                                                                  )) - 1); hv_Index = (int)hv_Index + 1)
            {
                HOperatorSet.FileExists(hv_Directories.TupleSelect(hv_Index), out hv_FileExists);
                if ((int)(hv_FileExists) != 0)
                {
                    HOperatorSet.ListFiles(hv_Directories.TupleSelect(hv_Index), (new HTuple("files")).TupleConcat(
                                               hv_Options), out hv_AllFiles);
                    hv_ImageFiles = new HTuple();
                    for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_Extensions_COPY_INP_TMP.TupleLength()
                                                                  )) - 1); hv_i = (int)hv_i + 1)
                    {
                        HOperatorSet.TupleRegexpSelect(hv_AllFiles, (((".*" + (hv_Extensions_COPY_INP_TMP.TupleSelect(
                                                                                   hv_i))) + "$")).TupleConcat("ignore_case"), out hv_Selection);
                        hv_ImageFiles = hv_ImageFiles.TupleConcat(hv_Selection);
                    }
                    HOperatorSet.TupleRegexpReplace(hv_ImageFiles, (new HTuple("\\\\")).TupleConcat(
                                                        "replace_all"), "/", out hv_ImageFiles);
                    if ((int)(hv_network_drive.TupleSelect(hv_Index)) != 0)
                    {
                        HOperatorSet.TupleRegexpReplace(hv_ImageFiles, (new HTuple("//")).TupleConcat(
                                                            "replace_all"), "/", out hv_ImageFiles);
                        hv_ImageFiles = "/" + hv_ImageFiles;
                    }
                    else
                    {
                        HOperatorSet.TupleRegexpReplace(hv_ImageFiles, (new HTuple("//")).TupleConcat(
                                                            "replace_all"), "/", out hv_ImageFiles);
                    }
                    return;
                }
            }
        }
    /// <summary>
    /// Evaluates the sharpness of the calibration plate in the calibration
    /// image.
    /// </summary>
    public void eval_caltab_focus(HObject ho_Image, 
                                 HObject ho_Marks,
                                 HTuple hv_Contrast,
                                 out HTuple hv_FocusScore)
    {
        // Local iconic variables

        HObject ho_Region, ho_RegionUnion, ho_ImageReduced;
        HObject ho_DerivGauss;

        // Local control variables

        HTuple hv_Number, hv_MeanGradient, hv_Deviation;
        HTuple hv_MinScore, hv_RawResult;

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Region);
        HOperatorSet.GenEmptyObj(out ho_RegionUnion);
        HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        HOperatorSet.GenEmptyObj(out ho_DerivGauss);

        hv_FocusScore = 0.0;
        if ((int)(new HTuple(hv_Contrast.TupleEqual(0))) != 0)
        {
          ho_Region.Dispose();
          ho_RegionUnion.Dispose();
          ho_ImageReduced.Dispose();
          ho_DerivGauss.Dispose();

          return;
        }
        HOperatorSet.CountObj(ho_Marks, out hv_Number);
        if ((int)(new HTuple(hv_Number.TupleLess(3))) != 0)
        {
          ho_Region.Dispose();
          ho_RegionUnion.Dispose();
          ho_ImageReduced.Dispose();
          ho_DerivGauss.Dispose();

          return;
        }
        ho_Region.Dispose();
        HOperatorSet.GenRegionContourXld(ho_Marks, out ho_Region, "margin");
        ho_RegionUnion.Dispose();
        HOperatorSet.Union1(ho_Region, out ho_RegionUnion);
        ho_ImageReduced.Dispose();
        HOperatorSet.ReduceDomain(ho_Image, ho_RegionUnion, out ho_ImageReduced);
        ho_DerivGauss.Dispose();
        HOperatorSet.DerivateGauss(ho_ImageReduced, out ho_DerivGauss, 0.7, "gradient");
        HOperatorSet.Intensity(ho_Region, ho_DerivGauss, out hv_MeanGradient, out hv_Deviation);
        hv_MinScore = 0.25;
        //Normalize the Gradient with the contrast
        hv_RawResult = hv_MeanGradient / hv_Contrast;
        hv_FocusScore = (((hv_RawResult * 4.5)).TupleSort())[(new HTuple((new HTuple(hv_RawResult.TupleLength()
        )) / 20.0)).TupleRound()];
        hv_FocusScore = ((((((((hv_FocusScore - hv_MinScore)).TupleConcat(0.0))).TupleMax()
        )).TupleConcat(1.0))).TupleMin();
        ho_Region.Dispose();
        ho_RegionUnion.Dispose();
        ho_ImageReduced.Dispose();
        ho_DerivGauss.Dispose();

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

            // Local iconic variables

            HObject ho_Rectanglem;
            HObject ho_Regionm;
            HObject ho_Regionn, ho_Regiont, ho_ImageAffinTrans = null;
            HObject ho_ImageReduced = null, ho_Region = null, ho_RegionDilation1 = null;
            HObject ho_ConnectedRegions2 = null, ho_SelectedRegions2 = null;
            HObject ho_RegionTrans2 = null, ho_RegionDifference2 = null;
            HObject ho_RegionErosion1 = null, ho_ConnectedRegions3 = null;
            HObject ho_SelectedRegions3 = null, ho_RegionTrans3 = null;
            HObject ho_ImageReduced3 = null, ho_Region3 = null, ho_ConnectedRegions4 = null;
            HObject ho_SelectedRegions4 = null, ho_RegionErosion = null;
            HObject ho_RegionDilation = null, ho_RegionClosing = null, ho_ConnectedRegions = null;
            HObject ho_SelectedRegions = null, ho_ObjectSelected = null;
            HObject ho_RegionTrans = null, ho_ImageReduced1 = null, ho_Region1 = null;
            HObject ho_RegionDifference = null, ho_ObjectSelected1 = null;
            HObject ho_RegionTrans1 = null, ho_ImageReduced2 = null, ho_Region2 = null;
            HObject ho_RegionDifference1 = null, ho_ConnectedRegions1 = null;
            HObject ho_SelectedRegions1 = null, ho_RegionAffineTrans = null;

            // Local control variables

            HTuple hv_m = null, hv_n = null, hv_m1 = null, hv_n1 = null;
            HTuple hv_h1 = null, hv_w1 = null, hv_h2 = null, hv_w2 = null;
            HTuple hv_Row = null, hv_Column = null, hv_Angle = null;
            HTuple hv_Score = null, hv_HomMat2D = new HTuple(), hv_Area1 = new HTuple();
            HTuple hv_Row4 = new HTuple(), hv_Column4 = new HTuple();
            HTuple hv_Number = new HTuple(), hv_Row1 = new HTuple();
            HTuple hv_Column1 = new HTuple(), hv_Row2 = new HTuple();
            HTuple hv_Column2 = new HTuple(), hv_Row11 = new HTuple();
            HTuple hv_Column11 = new HTuple(), hv_Row21 = new HTuple();
            HTuple hv_Column21 = new HTuple(), hv_Area = new HTuple();
            HTuple hv_Row3 = new HTuple(), hv_Column3 = new HTuple();
            HTuple hv_HomMat2D1 = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectanglem);
            HOperatorSet.GenEmptyObj(out ho_Regionm);
            HOperatorSet.GenEmptyObj(out ho_Regionn);
            HOperatorSet.GenEmptyObj(out ho_Regiont);
            HOperatorSet.GenEmptyObj(out ho_ImageAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans2);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference2);
            HOperatorSet.GenEmptyObj(out ho_RegionErosion1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions3);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions3);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans3);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced3);
            HOperatorSet.GenEmptyObj(out ho_Region3);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions4);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions4);
            HOperatorSet.GenEmptyObj(out ho_RegionErosion);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected1);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced2);
            HOperatorSet.GenEmptyObj(out ho_Region2);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            try
            {
                hv_m  = 0;
                hv_n  = 0;
                hv_m1 = 0;
                hv_n1 = 0;
                hv_h1 = 0;
                hv_w1 = 0;
                hv_h2 = 0;
                hv_w2 = 0;
                ho_Regionm.Dispose();
                HOperatorSet.GenRegionPoints(out ho_Regionm, DRows, DColumns);
                ho_Regionn.Dispose();
                HOperatorSet.GenRegionPoints(out ho_Regionn, DRows, DColumns);
                ho_Regiont.Dispose();
                HOperatorSet.GenRegionPoints(out ho_Regiont, DRows, DColumns);
                ho_Rectanglem.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectanglem, DRows, DColumns, DLength1s, DLength2s);
                HOperatorSet.ReadShapeModel(PathHelper.currentProductPath + @"\waiguan1.shm", out hv_ModelID);
                HOperatorSet.FindShapeModel(Image, hv_ModelID, -6.29, 6.29, 0, 1, 0.5, "least_squares",
                                            0, 0.9, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
                if ((int)(new HTuple((new HTuple(hv_Score.TupleLength())).TupleEqual(1))) != 0)
                {
                    HOperatorSet.VectorAngleToRigid(hv_Row, hv_Column, hv_Angle, DRow1s, DColumn1s,
                                                    0, out hv_HomMat2D);
                    ho_ImageAffinTrans.Dispose();
                    HOperatorSet.AffineTransImage(Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                                  "constant", "false");
                    ho_ImageReduced.Dispose();
                    HOperatorSet.ReduceDomain(ho_ImageAffinTrans, ho_Rectanglem, out ho_ImageReduced
                                              );
                    ho_Region.Dispose();
                    HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, thv, 255);
                    //
                    ho_RegionDilation1.Dispose();
                    HOperatorSet.DilationCircle(ho_Region, out ho_RegionDilation1, this.afs);
                    ho_ConnectedRegions2.Dispose();
                    HOperatorSet.Connection(ho_RegionDilation1, out ho_ConnectedRegions2);
                    ho_SelectedRegions2.Dispose();
                    HOperatorSet.SelectShapeStd(ho_ConnectedRegions2, out ho_SelectedRegions2,
                                                "max_area", 70);
                    ho_RegionTrans2.Dispose();
                    HOperatorSet.ShapeTrans(ho_SelectedRegions2, out ho_RegionTrans2, "convex");
                    ho_RegionDifference2.Dispose();
                    HOperatorSet.Difference(ho_RegionTrans2, ho_SelectedRegions2, out ho_RegionDifference2
                                            );
                    ho_RegionErosion1.Dispose();
                    HOperatorSet.ErosionCircle(ho_RegionDifference2, out ho_RegionErosion1, this.apz);
                    ho_ConnectedRegions3.Dispose();
                    HOperatorSet.Connection(ho_RegionErosion1, out ho_ConnectedRegions3);
                    ho_SelectedRegions3.Dispose();
                    HOperatorSet.SelectShapeStd(ho_ConnectedRegions3, out ho_SelectedRegions3,
                                                "max_area", 70);
                    ho_RegionTrans3.Dispose();
                    HOperatorSet.ShapeTrans(ho_SelectedRegions3, out ho_RegionTrans3, "convex");
                    ho_ImageReduced3.Dispose();
                    HOperatorSet.ReduceDomain(ho_ImageReduced, ho_RegionTrans3, out ho_ImageReduced3
                                              );
                    ho_Region3.Dispose();
                    HOperatorSet.Threshold(ho_ImageReduced3, out ho_Region3, thv, 255);
                    ho_ConnectedRegions4.Dispose();
                    HOperatorSet.Connection(ho_Region3, out ho_ConnectedRegions4);
                    ho_SelectedRegions4.Dispose();
                    HOperatorSet.SelectShape(ho_ConnectedRegions4, out ho_SelectedRegions4, "area",
                                             "and", this.kdz, 9900999);
                    HOperatorSet.AreaCenter(ho_SelectedRegions4, out hv_Area1, out hv_Row4, out hv_Column4);
                    if ((int)(new HTuple((new HTuple(hv_Area1.TupleLength())).TupleGreater(0))) != 0)
                    {
                        hv_m1 = hv_Area1.TupleMax();
                        hv_n1 = hv_Area1.TupleSum();
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_Regionn, ho_SelectedRegions4, out ExpTmpOutVar_0);
                            ho_Regionn.Dispose();
                            ho_Regionn = ExpTmpOutVar_0;
                        }
                    }
                    else
                    {
                        hv_m1 = 0;
                        hv_n1 = 0;
                    }


//
                    ho_RegionErosion.Dispose();
                    HOperatorSet.ErosionRectangle1(ho_Region, out ho_RegionErosion, this.cd, this.pzz);
                    ho_RegionDilation.Dispose();
                    HOperatorSet.DilationRectangle1(ho_RegionErosion, out ho_RegionDilation, this.cd,
                                                    this.pzz);
                    ho_RegionClosing.Dispose();
                    HOperatorSet.ClosingRectangle1(ho_RegionDilation, out ho_RegionClosing, this.yd,
                                                   this.bjz);
                    ho_ConnectedRegions.Dispose();
                    HOperatorSet.Connection(ho_RegionClosing, out ho_ConnectedRegions);
                    ho_SelectedRegions.Dispose();
                    HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, (new HTuple("height")).TupleConcat(
                                                 "width"), "and", (new HTuple(this.yd)).TupleConcat(this.bjz), (new HTuple(9999)).TupleConcat(
                                                 5000));
                    HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number);
                    if ((int)(new HTuple(hv_Number.TupleEqual(2))) != 0)
                    {
                        ho_ObjectSelected.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions, out ho_ObjectSelected, 1);
                        ho_RegionTrans.Dispose();
                        HOperatorSet.ShapeTrans(ho_ObjectSelected, out ho_RegionTrans, "convex");
                        HOperatorSet.SmallestRectangle1(ho_RegionTrans, out hv_Row1, out hv_Column1,
                                                        out hv_Row2, out hv_Column2);
                        hv_h1 = hv_Row2 - hv_Row1;
                        hv_w1 = hv_Column2 - hv_Column1;
                        ho_ImageReduced1.Dispose();
                        HOperatorSet.ReduceDomain(ho_ImageReduced, ho_RegionTrans, out ho_ImageReduced1
                                                  );
                        ho_Region1.Dispose();
                        HOperatorSet.Threshold(ho_ImageReduced1, out ho_Region1, thv, 255);
                        ho_RegionDifference.Dispose();
                        HOperatorSet.Difference(ho_RegionTrans, ho_Region1, out ho_RegionDifference
                                                );
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_Regionm, ho_RegionDifference, out ExpTmpOutVar_0);
                            ho_Regionm.Dispose();
                            ho_Regionm = ExpTmpOutVar_0;
                        }
                        ho_ObjectSelected1.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions, out ho_ObjectSelected1, 2);
                        ho_RegionTrans1.Dispose();
                        HOperatorSet.ShapeTrans(ho_ObjectSelected1, out ho_RegionTrans1, "convex");
                        HOperatorSet.SmallestRectangle1(ho_RegionTrans1, out hv_Row11, out hv_Column11,
                                                        out hv_Row21, out hv_Column21);
                        hv_h2 = hv_Row21 - hv_Row11;
                        hv_w2 = hv_Column21 - hv_Column11;
                        ho_ImageReduced2.Dispose();
                        HOperatorSet.ReduceDomain(ho_ImageReduced, ho_RegionTrans1, out ho_ImageReduced2
                                                  );
                        ho_Region2.Dispose();
                        HOperatorSet.Threshold(ho_ImageReduced2, out ho_Region2, thv, 255);
                        ho_RegionDifference1.Dispose();
                        HOperatorSet.Difference(ho_RegionTrans1, ho_Region2, out ho_RegionDifference1
                                                );
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_Regionm, ho_RegionDifference1, out ExpTmpOutVar_0);
                            ho_Regionm.Dispose();
                            ho_Regionm = ExpTmpOutVar_0;
                        }
                        ho_ConnectedRegions1.Dispose();
                        HOperatorSet.Connection(ho_Regionm, out ho_ConnectedRegions1);
                        ho_SelectedRegions1.Dispose();
                        HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions1, "area",
                                                 "and", this.mj, 99999);
                        HOperatorSet.AreaCenter(ho_SelectedRegions1, out hv_Area, out hv_Row3, out hv_Column3);
                        if ((int)(new HTuple((new HTuple(hv_Area.TupleLength())).TupleGreater(0))) != 0)
                        {
                            hv_m = hv_Area.TupleMax();
                            hv_n = hv_Area.TupleSum();
                            {
                                HObject ExpTmpOutVar_0;
                                HOperatorSet.Union2(ho_Regionn, ho_SelectedRegions1, out ExpTmpOutVar_0
                                                    );
                                ho_Regionn.Dispose();
                                ho_Regionn = ExpTmpOutVar_0;
                            }
                        }
                        else
                        {
                            hv_m = 0;
                            hv_n = 0;
                        }
                    }

                    HOperatorSet.VectorAngleToRigid(DRow1s, DColumn1s, 0, hv_Row, hv_Column,
                                                    hv_Angle, out hv_HomMat2D1);
                    ho_RegionAffineTrans.Dispose();
                    HOperatorSet.AffineTransRegion(ho_Regionn, out ho_RegionAffineTrans, hv_HomMat2D1,
                                                   "nearest_neighbor");
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.Union2(ho_Regiont, ho_RegionAffineTrans, out ExpTmpOutVar_0);
                        ho_Regiont.Dispose();
                        ho_Regiont = ExpTmpOutVar_0;
                    }
                }
                else
                {
                    hv_h1 = 0;
                    hv_w1 = 0;
                    hv_h2 = 0;
                    hv_w2 = 0;
                    hv_m  = 99999;
                    hv_n  = 99999;
                    hv_m1 = 99999;
                    hv_n1 = 99999;
                }


                HOperatorSet.ClearShapeModel(hv_ModelID);
                HOperatorSet.Union1(ho_Regiont, out RegionToDisp);

                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("亮色区缺陷总面积");
                hv_result = hv_result.TupleConcat(hv_n.D);
                hv_result = hv_result.TupleConcat("亮色区最大缺陷面积");
                hv_result = hv_result.TupleConcat(hv_m.D);
                hv_result = hv_result.TupleConcat("暗色区缺陷总面积");
                hv_result = hv_result.TupleConcat(hv_n1.D);
                hv_result = hv_result.TupleConcat("暗色区最大缺陷面积");
                hv_result = hv_result.TupleConcat(hv_m1.D);
                hv_result = hv_result.TupleConcat("高1");
                hv_result = hv_result.TupleConcat(hv_h1.D);
                hv_result = hv_result.TupleConcat("宽1");
                hv_result = hv_result.TupleConcat(hv_w1.D);
                hv_result = hv_result.TupleConcat("高2");
                hv_result = hv_result.TupleConcat(hv_h2.D);
                hv_result = hv_result.TupleConcat("宽2");
                hv_result = hv_result.TupleConcat(hv_w2.D);

                result = hv_result.Clone();
                ho_Rectanglem.Dispose();
                ho_Regionm.Dispose();
                ho_Regionn.Dispose();
                ho_Regiont.Dispose();
                ho_ImageAffinTrans.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_RegionDilation1.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionTrans2.Dispose();
                ho_RegionDifference2.Dispose();
                ho_RegionErosion1.Dispose();
                ho_ConnectedRegions3.Dispose();
                ho_SelectedRegions3.Dispose();
                ho_RegionTrans3.Dispose();
                ho_ImageReduced3.Dispose();
                ho_Region3.Dispose();
                ho_ConnectedRegions4.Dispose();
                ho_SelectedRegions4.Dispose();
                ho_RegionErosion.Dispose();
                ho_RegionDilation.Dispose();
                ho_RegionClosing.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_RegionTrans.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Region1.Dispose();
                ho_RegionDifference.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_RegionTrans1.Dispose();
                ho_ImageReduced2.Dispose();
                ho_Region2.Dispose();
                ho_RegionDifference1.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_RegionAffineTrans.Dispose();
                algorithm.Region.Dispose();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("亮色区缺陷总面积");
                hv_result = hv_result.TupleConcat(9999999);
                hv_result = hv_result.TupleConcat("亮色区最大缺陷面积");
                hv_result = hv_result.TupleConcat(9999999);
                hv_result = hv_result.TupleConcat("暗色区缺陷总面积");
                hv_result = hv_result.TupleConcat(9999999);
                hv_result = hv_result.TupleConcat("暗色区最大缺陷面积");
                hv_result = hv_result.TupleConcat(9999999);
                hv_result = hv_result.TupleConcat("高1");
                hv_result = hv_result.TupleConcat(0);
                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("宽2");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();

                ho_Rectanglem.Dispose();
                ho_Regionm.Dispose();
                ho_Regionn.Dispose();
                ho_Regiont.Dispose();
                ho_ImageAffinTrans.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_RegionDilation1.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionTrans2.Dispose();
                ho_RegionDifference2.Dispose();
                ho_RegionErosion1.Dispose();
                ho_ConnectedRegions3.Dispose();
                ho_SelectedRegions3.Dispose();
                ho_RegionTrans3.Dispose();
                ho_ImageReduced3.Dispose();
                ho_Region3.Dispose();
                ho_ConnectedRegions4.Dispose();
                ho_SelectedRegions4.Dispose();
                ho_RegionErosion.Dispose();
                ho_RegionDilation.Dispose();
                ho_RegionClosing.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_RegionTrans.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Region1.Dispose();
                ho_RegionDifference.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_RegionTrans1.Dispose();
                ho_ImageReduced2.Dispose();
                ho_Region2.Dispose();
                ho_RegionDifference1.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_RegionAffineTrans.Dispose();
                algorithm.Region.Dispose();
            }
            finally
            {
                ho_Rectanglem.Dispose();
                ho_Regionm.Dispose();
                ho_Regionn.Dispose();
                ho_Regiont.Dispose();
                ho_ImageAffinTrans.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_RegionDilation1.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionTrans2.Dispose();
                ho_RegionDifference2.Dispose();
                ho_RegionErosion1.Dispose();
                ho_ConnectedRegions3.Dispose();
                ho_SelectedRegions3.Dispose();
                ho_RegionTrans3.Dispose();
                ho_ImageReduced3.Dispose();
                ho_Region3.Dispose();
                ho_ConnectedRegions4.Dispose();
                ho_SelectedRegions4.Dispose();
                ho_RegionErosion.Dispose();
                ho_RegionDilation.Dispose();
                ho_RegionClosing.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_RegionTrans.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Region1.Dispose();
                ho_RegionDifference.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_RegionTrans1.Dispose();
                ho_ImageReduced2.Dispose();
                ho_Region2.Dispose();
                ho_RegionDifference1.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_RegionAffineTrans.Dispose();
                algorithm.Region.Dispose();
            }
        }
Example #36
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;
        }
Example #37
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;
        }