Example #1
0
        // 2015.06.25
        public void SetTool(CogCaliperTool caliperTool)
        {
            if (m_cogCaliperTool != null)
            {
                m_cogCaliperTool = null;
            }

            m_cogCaliperTool = caliperTool;
        }
Example #2
0
        public ACaliper()
        {
            m_cogCaliperTool = new CogCaliperTool();

            //-------------------------------
            // 2015.05.11
            // default 값
            StringBuilder sb = new StringBuilder(256);

            AUtil.GetPrivateProfileString("CaliperTool", "ContrastThreshold", "5", sb, 32, ASDef._INI_FILE);
            m_cogCaliperTool.RunParams.ContrastThreshold = Convert.ToDouble(sb.ToString());

            AUtil.GetPrivateProfileString("CaliperTool", "FilterHalfSizeInPixels", "2", sb, 32, ASDef._INI_FILE);
            m_cogCaliperTool.RunParams.FilterHalfSizeInPixels = Convert.ToInt32(sb.ToString());
            //-------------------------------

            Init();
        }
Example #3
0
 public ACaliper(Object oTool)
 {
     m_cogCaliperTool = oTool as CogCaliperTool;
     Init();
 }
Example #4
0
        static void Main(string[] args)
        {
            // load image
            var bmp = new System.Drawing.Bitmap(@"D:\test.bmp");

            #region Using Vision Pro
            //vision pro tool initializing
            CogCaliperTool calipertool = new CogCaliperTool();
            //or load .vpp file (specific tool vpp)
            calipertool = CogSerializer.LoadObjectFromFile(@"D:\CaliperTool.vpp") as CogCaliperTool;

            //input processing image
            calipertool.InputImage = new CogImage8Grey(bmp);

            //process
            calipertool.Run();

            //create result record
            var caliper_record = calipertool.CreateLastRunRecord().SubRecords[0];

            //use result
            Console.Write("Edge Position is : X = {0}, Y = {1}", calipertool.Results[0].PositionX, calipertool.Results[0].PositionY);

            //save current tool (it saved specific tool vpp)
            CogSerializer.SaveObjectToFile(calipertool, @"D:\CaliperTool_Saved.vpp");
            #endregion


            #region Using ViDi in Runtime

            //open ViDi control (it has to open only one in the application.)
            ViDi2.Runtime.Local.Control runtime_control = new ViDi2.Runtime.Local.Control();
            //add runtime workspace (and open)
            ViDi2.Runtime.IWorkspace runtime_workspace = runtime_control.Workspaces.Add("TestWorkspace", @"D:\DeeplearningWorkspace.vrws");
            //select stream in workspace
            ViDi2.Runtime.IStream runtime_stream = runtime_workspace.Streams.Single(s => s.Name.Equals("Stream"));


            //pack image for use ViDi
            ViDi2.IImage runtime_ViDiImg = new ViDi2.FormsImage(bmp);
            //process
            ViDi2.ISample runtime_sample = runtime_stream.Process(runtime_ViDiImg);

            //using red result
            ViDi2.IRedMarking redMarking = runtime_sample.Markings["Analyze"] as ViDi2.IRedMarking;

            //marking has a value for each view result
            var r_score  = redMarking.Views[0].Score;
            var r_region = redMarking.Views[0].Regions;
            var r_pose   = redMarking.Views[0].Pose;

            //using blue result
            ViDi2.IBlueMarking blueMarking = runtime_sample.Markings["Locate"] as ViDi2.IBlueMarking;

            var b_features = blueMarking.Views[0].Features;
            var b_pose     = blueMarking.Views[0].Pose;

            //using green result
            ViDi2.IGreenMarking greenMarking = runtime_sample.Markings["Classify"] as ViDi2.IGreenMarking;

            var g_best_tag = greenMarking.Views[0].BestTag;
            var g_tags     = greenMarking.Views[0].Tags;

            //free vidi image
            runtime_ViDiImg.Dispose();

            //close workspace
            runtime_workspace.Close();

            //close vidi control
            runtime_control.Dispose();
            #endregion


            #region Using ViDi - Vision Pro Integration
            //open ViDi control (it has to open only one in the application.)
            ViDi2.Runtime.Local.Control integ_control = new ViDi2.Runtime.Local.Control();
            //add runtime workspace (and open)
            ViDi2.Runtime.IWorkspace integ_workspace = integ_control.Workspaces.Add("TestWorkspace", @"D:\DeeplearningWorkspace.vrws");
            //select stream in workspace
            ViDi2.Runtime.IStream integ_stream = integ_workspace.Streams.Single(s => s.Name.Equals("Stream"));

            //vision pro tool initializing
            CogAffineTransformTool transform_tool = new CogAffineTransformTool();
            //or load .vpp file (specific tool vpp)
            transform_tool = CogSerializer.LoadObjectFromFile(@"D:\TransformTool.vpp") as CogAffineTransformTool;

            //input processing image
            transform_tool.InputImage = new CogImage8Grey(bmp);
            //process Vision Pro
            transform_tool.Run();

            //pack vison pro output image for use ViDi
            ViDi2.IImage integ_ViDiImg = new ViDi2.VisionPro.Image(transform_tool.OutputImage);
            //process ViDi
            var integ_sample = integ_stream.Process(integ_ViDiImg);

            //create vision pro record
            var vp_record = transform_tool.CreateLastRunRecord().SubRecords[0];

            //create ViDi record
            var integ_redMarking = integ_sample.Markings["Analyze"] as ViDi2.IRedMarking;
            var vidi_red_record  = new ViDi2.VisionPro.RedViewRecord(integ_redMarking.Views[0] as ViDi2.IRedView, new ViDi2.VisionPro.Records.DefaultRedToolGraphicCreator());

            var integ_blueMarking = integ_sample.Markings["Locate"] as ViDi2.IBlueMarking;
            var vidi_blue_record  = new ViDi2.VisionPro.BlueViewRecord(integ_blueMarking.Views[0] as ViDi2.IBlueView, new ViDi2.VisionPro.Records.DefaultBlueToolGraphicCreator());

            var integ_greenMarking = integ_sample.Markings["Classify"] as ViDi2.IGreenMarking;
            var vidi_green_record  = new ViDi2.VisionPro.GreenViewRecord(integ_greenMarking.Views[0] as ViDi2.IGreenView, new ViDi2.VisionPro.Records.DefaultGreenToolGraphicCreator());

            //free vidi image
            integ_ViDiImg.Dispose();
            //close workspace
            integ_workspace.Close();
            //close vidi control
            integ_control.Dispose();
            #endregion


            //free image
            bmp.Dispose();
        }
Example #5
0
        public bool LeadAlignSE(CogImage8Grey _SrcImage, CogRectangle _InspRegion, CogLeadFormAlgo _CogLeadFormAlgo)
        {
            if (LeadFormResult.IsGood != true)
            {
                return(true);
            }

            CLogManager.AddInspectionLog(CLogManager.LOG_TYPE.INFO, "LeadAlign - Start", CLogManager.LOG_LEVEL.MID);

            if (false == _CogLeadFormAlgo.IsUseAlign)
            {
                CLogManager.AddInspectionLog(CLogManager.LOG_TYPE.INFO, "LeadAlign Disable - End", CLogManager.LOG_LEVEL.MID);
                return(true);
            }

            bool          _Result             = true;
            List <double> _AlignPositionX     = new List <double>();
            List <double> _AlignPositionY     = new List <double>();
            List <double> _AlignPositionWidth = new List <double>();

            try
            {
                System.Diagnostics.Stopwatch _ProcessWatch = new System.Diagnostics.Stopwatch();
                _ProcessWatch.Reset(); _ProcessWatch.Start();

                SetHardFixedThreshold(_CogLeadFormAlgo.AlignThreshold);
                SetConnectivityMinimum(500);
                SetPolarity(true);

                CogBlobResults _BlobResults    = BlobProc.Execute(_SrcImage, _InspRegion);
                List <double>  _GuidePositionX = GetLeadGuidePosition(_BlobResults);

                #region Caliper Tool Setting => Y좌표 위치 정보 찾기
                LeadFormResult.LeadCount = _GuidePositionX.Count;

                if (_GuidePositionX.Count != _CogLeadFormAlgo.LeadCount)
                {
                    LeadFormResult.IsGood = false;
                    LeadFormResult.NgType = eNgType.LEAD_CNT;
                    LeadFormResult.SearchArea.SetCenterWidthHeight(_InspRegion.CenterX, _InspRegion.CenterY, _InspRegion.Width, _InspRegion.Height);
                    LeadFormResult.LeadCountStatus = LeadFormResult.LeadCount.ToString();
                    return(false);
                }

                for (int iLoopCount = 0; iLoopCount < _GuidePositionX.Count; ++iLoopCount)
                {
                    CogRectangleAffine _CaliperRegion = new CogRectangleAffine();
                    _CaliperRegion.SetCenterLengthsRotationSkew(_GuidePositionX[iLoopCount], _InspRegion.CenterY, _InspRegion.Height, 170, 1.5708, 0);

                    CogCaliperTool _LeadCaliper = new CogCaliperTool();
                    _LeadCaliper.RunParams.EdgeMode      = CogCaliperEdgeModeConstants.SingleEdge;
                    _LeadCaliper.RunParams.Edge0Polarity = CogCaliperPolarityConstants.DarkToLight;
                    _LeadCaliper.RunParams.MaxResults    = 2;
                    _LeadCaliper.Region = _CaliperRegion;

                    _LeadCaliper.InputImage = _SrcImage;
                    _LeadCaliper.Run();

                    if (_LeadCaliper.Results.Count == 1)
                    {
                        _AlignPositionY.Add(_LeadCaliper.Results[0].PositionY);
                    }
                    else if (_LeadCaliper.Results.Count == 2)
                    {
                        _AlignPositionY.Add((_LeadCaliper.Results[0].PositionY < _LeadCaliper.Results[1].PositionY) ? _LeadCaliper.Results[0].PositionY : _LeadCaliper.Results[1].PositionY);
                    }

                    //CogSerializer.SaveObjectToFile(_LeadCaliper, string.Format(@"D:\GuideCaliper{0}.vpp", iLoopCount + 1));
                }
                #endregion

                #region Blob Tool Setting => X좌표 위치 정보 찾기
                for (int iLoopCount = 0; iLoopCount < _GuidePositionX.Count; ++iLoopCount)
                {
                    SetHardFixedThreshold(_CogLeadFormAlgo.AlignThreshold);
                    SetConnectivityMinimum(500);
                    SetPolarity(true);

                    CogRectangle _BlobRegion = new CogRectangle();
                    _BlobRegion.SetCenterWidthHeight(_GuidePositionX[iLoopCount], _AlignPositionY[iLoopCount] + 18, 170, 36);
                    _BlobResults = BlobProc.Execute(_SrcImage, _BlobRegion);

                    if (_BlobResults.GetBlobs().Count == 0)
                    {
                        LeadFormResult.IsGood = false;
                        LeadFormResult.NgType = eNgType.LEAD_CNT;
                        LeadFormResult.SearchArea.SetCenterWidthHeight(_InspRegion.CenterX, _InspRegion.CenterY, _InspRegion.Width, _InspRegion.Height);
                        LeadFormResult.LeadCountStatus = "NG";
                        return(false);
                    }

                    _AlignPositionX.Add(GetCenterXResult(_BlobResults));
                    _AlignPositionWidth.Add(GetWidthResult(_BlobResults));
                }
                #endregion

                #region Align XY 좌표 ADD
                if (_CogLeadFormAlgo.LeadCount == _GuidePositionX.Count)
                {
                    LeadFormResult.AlignResultDataList.Clear();
                    for (int iLoopCount = 0; iLoopCount < _GuidePositionX.Count; ++iLoopCount)
                    {
                        LeadFormAlignResultData _AlignResult = new LeadFormAlignResultData();
                        _AlignResult.CenterX = _AlignPositionX[iLoopCount];
                        _AlignResult.CenterY = _AlignPositionY[iLoopCount];
                        _AlignResult.Width   = _AlignPositionWidth[iLoopCount];
                        _AlignResult.Height  = _AlignPositionWidth[iLoopCount];
                        _AlignResult.IsGood  = true;
                        LeadFormResult.AlignResultDataList.Add(_AlignResult);
                    }
                }

                else
                {
                    LeadFormResult.IsGood          = false;
                    LeadFormResult.NgType          = eNgType.LEAD_CNT;
                    LeadFormResult.LeadCountStatus = _GuidePositionX.Count.ToString();
                    LeadFormResult.SearchArea.SetCenterWidthHeight(_InspRegion.CenterX, _InspRegion.CenterY, _InspRegion.Width, _InspRegion.Height);
                    _Result = false;
                }
                #endregion

                //Lead 간격 조건 검사
                LeadFormResult.AlignOffsetDataList.Clear();
                PointD _AlignOffset = new PointD();
                if (_CogLeadFormAlgo.LeadCount == LeadFormResult.LeadCount)
                {
                    for (int iLoopCount = 0; iLoopCount < _CogLeadFormAlgo.LeadCount; ++iLoopCount)
                    {
                        double _RealCenterX = LeadFormResult.AlignResultDataList[iLoopCount].CenterX * _CogLeadFormAlgo.ResolutionX;
                        double _RealCenterY = LeadFormResult.AlignResultDataList[iLoopCount].CenterY * _CogLeadFormAlgo.ResolutionY;

                        #region X 축 Skew 확인
                        //Align Pitch Spec에서 완전히 벗어났는지 확인
                        if (_RealCenterX > _CogLeadFormAlgo.AlignPositionArray[iLoopCount].X - _CogLeadFormAlgo.AlignPitchSpec &&
                            _RealCenterX < _CogLeadFormAlgo.AlignPositionArray[iLoopCount].X + _CogLeadFormAlgo.AlignPitchSpec)
                        {
                            //Align Skew 가능 범위에 들어와 있는지 확인
                            //Skew 범위 안쪽이면 Skew 여부에 상관없이 GOOD
                            if (_RealCenterX > _CogLeadFormAlgo.AlignPositionArray[iLoopCount].X - _CogLeadFormAlgo.AlignSkewSpec &&
                                _RealCenterX < _CogLeadFormAlgo.AlignPositionArray[iLoopCount].X + _CogLeadFormAlgo.AlignSkewSpec)
                            {
                                if (LeadFormResult.NgType == eNgType.GOOD)
                                {
                                    LeadFormResult.AlignResultDataList[iLoopCount].IsGood = true;
                                }
                                LeadFormResult.EachLeadStatusArray[iLoopCount].SetSkewResult(eLeadStatus.GOOD);
                            }

                            //Skew 범위 < Position < Pitch Err 범위
                            //불량 판정 & Skew 가능 에러로 전달
                            else
                            {
                                LeadFormResult.AlignResultDataList[iLoopCount].IsGood = false;
                                LeadFormResult.IsGood = false;

                                LeadFormResult.EachLeadStatusArray[iLoopCount].SetSkewResult(eLeadStatus.LEAD_SKEW_ENABLE);
                                _Result = false;
                            }
                        }

                        //완전히 벗어나면  Skew 불가능 에러
                        else
                        {
                            LeadFormResult.AlignResultDataList[iLoopCount].IsGood = false;
                            LeadFormResult.IsGood = false;

                            LeadFormResult.EachLeadStatusArray[iLoopCount].SetSkewResult(eLeadStatus.LEAD_SKEW_DISABLE);
                            _Result = false;
                        }
                        #endregion

                        #region Y 축 Skew 확인
                        //Align Pitch Spec에서 완전히 벗어났는지 확인
                        if (_RealCenterY > _CogLeadFormAlgo.AlignPositionArray[iLoopCount].Y - _CogLeadFormAlgo.AlignPitchSpec &&
                            _RealCenterY < _CogLeadFormAlgo.AlignPositionArray[iLoopCount].Y + _CogLeadFormAlgo.AlignPitchSpec)
                        {
                            //Align Skew 가능 범위에 들어와 있는지 확인
                            //Skew 범위 안쪽이면 Skew 여부에 상관없이 GOOD
                            if (_RealCenterY > _CogLeadFormAlgo.AlignPositionArray[iLoopCount].Y - _CogLeadFormAlgo.AlignSkewSpec &&
                                _RealCenterY < _CogLeadFormAlgo.AlignPositionArray[iLoopCount].Y + _CogLeadFormAlgo.AlignSkewSpec)
                            {
                                //if (LeadFormResult.NgType == eNgType.GOOD)
                                //    LeadFormResult.AlignResultDataList[iLoopCount].IsGood = true;
                                LeadFormResult.EachLeadStatusArray[iLoopCount].SetSkewResult(eLeadStatus.GOOD);
                            }

                            //Skew 범위 < Position < Pitch Err 범위
                            //불량 판정 & Skew 가능 에러로 전달
                            else
                            {
                                LeadFormResult.AlignResultDataList[iLoopCount].IsGood = false;
                                LeadFormResult.IsGood = false;

                                LeadFormResult.EachLeadStatusArray[iLoopCount].SetSkewResult(eLeadStatus.LEAD_SKEW_ENABLE);
                                _Result = false;
                            }
                        }

                        else
                        {
                            LeadFormResult.AlignResultDataList[iLoopCount].IsGood = false;
                            LeadFormResult.IsGood = false;

                            LeadFormResult.EachLeadStatusArray[iLoopCount].SetSkewResult(eLeadStatus.LEAD_SKEW_DISABLE);
                            _Result = false;
                        }
                        #endregion

                        _AlignOffset.X = Math.Round(_RealCenterX - _CogLeadFormAlgo.AlignPositionArray[iLoopCount].X, 4);
                        _AlignOffset.Y = Math.Round(_RealCenterY - _CogLeadFormAlgo.AlignPositionArray[iLoopCount].Y, 4);
                        LeadFormResult.AlignOffsetDataList.Add(_AlignOffset);

                        LeadFormResult.EachLeadStatusArray[iLoopCount].SideX = _AlignOffset.X.ToString();
                        LeadFormResult.EachLeadStatusArray[iLoopCount].SideY = _AlignOffset.Y.ToString();
                    }
                }

                else
                {
                    LeadFormResult.IsGood          = false;
                    LeadFormResult.NgType          = eNgType.LEAD_CNT;
                    LeadFormResult.LeadCountStatus = LeadFormResult.LeadCount.ToString();
                    LeadFormResult.SearchArea.SetCenterWidthHeight(_InspRegion.CenterX, _InspRegion.CenterY, _InspRegion.Width, _InspRegion.Height);
                    _Result = false;
                }

                _ProcessWatch.Stop();
                string _ProcessTime = String.Format("LeadAlign Time : {0} ms", _ProcessWatch.Elapsed.TotalSeconds.ToString());
                CLogManager.AddInspectionLog(CLogManager.LOG_TYPE.INFO, _ProcessTime, CLogManager.LOG_LEVEL.LOW);
            }

            catch (Exception ex)
            {
                CLogManager.AddSystemLog(CLogManager.LOG_TYPE.ERR, "LeadAlign - Inspection Exception : " + ex.ToString(), CLogManager.LOG_LEVEL.LOW);
                LeadFormResult.NgType         = eNgType.EMPTY;
                LeadFormResult.IsGood         = false;
                LeadFormResult.LeadBodyStatus = "NG";
                LeadFormResult.SearchArea.SetCenterWidthHeight(_InspRegion.CenterX, _InspRegion.CenterY, _InspRegion.Width, _InspRegion.Height);
                _Result = false;
            }

            return(_Result);
        }
Example #6
0
    //#region "when the tool group is run"

    // The GroupRun function is called when the tool group is run.  The default
    // implementation provided here is equivalent to the normal behavior of the
    // tool group.  Modifying this function will allow you to change the behavior
    // when the tool group is run.
    public override bool GroupRun(ref string message, ref CogToolResultConstants result)
    {
        // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
#if DEBUG
        if (System.Diagnostics.Debugger.IsAttached)
        {
            System.Diagnostics.Debugger.Break();
        }
#endif


        histCntr = 0;
        CogGraphicLabel myBaslerLabel = new CogGraphicLabel();
        CogGraphicLabel myAreaLabel   = new CogGraphicLabel();
        CogGraphicLabel myFlirLabel   = new CogGraphicLabel();
        CogPointMarker  baslerMarker; //= new CogPointMarker();
        double          blobAngle        = 0;
        double          blobAngleRadians = 0;

        //Get references to the tools
        //CogBlobTool blobBaslerTool = (CogBlobTool) toolGroup.Tools["FindWrappersInBasler"];
        CogBlobTool      blobFlirTool      = (CogBlobTool)toolGroup.Tools["PopsicleBlobFinder"];
        CogPMAlignTool   patternBaslerTool = (CogPMAlignTool)toolGroup.Tools["FindWrapperPatterns"];
        CogHistogramTool popsicleHistTool  = (CogHistogramTool)toolGroup.Tools["PopsicleHistogramTool"];
        CogCaliperTool   xDistEdgeFinder   = (CogCaliperTool)toolGroup.Tools["CogCaliperTool_XDist"];

        //Define the regions
        CogRectangleAffine popsicleRegion  = blobFlirTool.Region as CogRectangleAffine;
        CogRectangleAffine histogramRegion = blobFlirTool.Region as CogRectangleAffine;


        //Define the fonts
        System.Drawing.Font myBaslerFont = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        System.Drawing.Font myAreaFont   = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        System.Drawing.Font myFlirFont   = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));


        //Reset stats
        angleFailures   = 0;
        thermalFailures = 0;

        //Reset any labels and rectangles from previous runs
        BaslerLabels.Clear();
        FlirLabels.Clear();
        FlirRectangles.Clear();
        AreaLabel.Clear();


        //***** Run the tools to perform the search in the basler image *****

        //Update status strings for Visual Studio. Do not popup a message box for errors, this causes confusion with the operators.
        toolGroup.SetScriptTerminalData("BaslerStatus", "OK");
        toolGroup.SetScriptTerminalData("FlirStatus", "OK");

        bool acquireException = false;
        try
        {
            //Aquire an image from the Basler, send Exception to Visual Studio
            toolGroup.RunTool(toolGroup.Tools["BaslerAcqFifo"], ref message, ref result);
        }
        catch (Exception ex)
        {
            toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error: " + ex.Message + " Check power connections and communication cables.");
            acquireException = true;
        }

        try
        {
            //Aquire an image from the Flir, send Exception to Visual Studio
            toolGroup.RunTool(toolGroup.Tools["FlirAcqFifo"], ref message, ref result);
        }
        catch (Exception ex)
        {
            toolGroup.SetScriptTerminalData("FlirStatus", "Script Error: " + ex.Message + " Check power connections and communication cables.");
            acquireException = true;
        }
        if (acquireException)
        {
            return(false);
        }

        try
        {
            // Scale the Flir image to zoom in to smaller temperature range
            toolGroup.RunTool(toolGroup.Tools["ScaleFlirImagePmap"], ref message, ref result);
        }
        catch (Exception ex)
        {
            toolGroup.SetScriptTerminalData("FlirStatus", "Script Error: " + ex.Message + " Failed: ScaleFlirImagePmap");
            return(false);
        }

        try
        {
            // Transform the images to calibrated space
            toolGroup.RunTool(toolGroup.Tools["CalBasler"], ref message, ref result);
            toolGroup.RunTool(toolGroup.Tools["CalFlir"], ref message, ref result);
        }
        catch (Exception ex)
        {
            //MessageBox.Show("Calibration tool error: " + ex.Message, "Script Exception");
            toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error: " + ex.Message + " Failed: CalBasler");
            toolGroup.SetScriptTerminalData("FlirStatus", "Script Error: " + ex.Message + " Failed: CalFlir");
            return(false);
        }

        try
        {
            toolGroup.RunTool(toolGroup.Tools["CogPixelMapBasler"], ref message, ref result);
        }
        catch (Exception ex)
        {
            toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error: " + ex.Message + " Failed: CogPixelMapBasler");
            return(false);
        }


        try
        {
            // Run the Flir hist tool
            toolGroup.RunTool(toolGroup.Tools["PopsicleHistogramTool"], ref message, ref result);
        }
        catch (Exception ex)
        {
            toolGroup.SetScriptTerminalData("FlirStatus", "Script Error: " + ex.Message + " Failed: PopsicleHistogramTool");
            return(false);
        }

        try
        {
            // Run the blob tool and get a reference to the results.
            toolGroup.RunTool(patternBaslerTool, ref message, ref result);
        }
        catch (Exception ex)
        {
            toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error: " + ex.Message + " Failed: Basler patternBaslerTool");
            return(false);
        }

        CogPMAlignResults patternResults = patternBaslerTool.Results;

        // Clear list before starting loop
        malBlobPoses.Clear();
        Crosshairs.Clear();

        // Get group input terminal data
        try
        {
            disableHist          = (bool)toolGroup.GetScriptTerminalData("DisableHistogramInspection");
            minPickAngle         = (double)toolGroup.GetScriptTerminalData("MinPickAngle");
            maxPickAngle         = (double)toolGroup.GetScriptTerminalData("MaxPickAngle");
            minPopsicleHistCount = (double)toolGroup.GetScriptTerminalData("MinPopsicleHistCount");
            VisStudio_Running    = (bool)toolGroup.GetScriptTerminalData("VS_Running");
            showArea             = (bool)toolGroup.GetScriptTerminalData("ShowArea");
            showHist             = (bool)toolGroup.GetScriptTerminalData("ShowHistCount");
            visSideXLength       = (double)toolGroup.GetScriptTerminalData("VisSideXLength");
            visSideYLength       = (double)toolGroup.GetScriptTerminalData("VisSideYLength");
            visXwindowMin        = (double)toolGroup.GetScriptTerminalData("VisXwindowMin");
            visXwindowMax        = (double)toolGroup.GetScriptTerminalData("VisXwindowMax");
            visFlirRegionXadj    = (double)toolGroup.GetScriptTerminalData("VisFlirRegionXadj");
            visFlirRegionYadj    = (double)toolGroup.GetScriptTerminalData("VisFlirRegionYadj");
        }
        catch (Exception ex)
        {
            //MessageBox.Show("Getting terminal data exception: ", ex.Message);
            toolGroup.SetScriptTerminalData("BaslerStatus", "Getting script data: " + ex.Message);
        }

        // Set run variables for manual triggering
        if (!VisStudio_Running)
        {
            minPopsicleHistCount = 0;
            minPickAngle         = -20;
            maxPickAngle         = 20;
            showArea             = true;
            showHist             = true;
            visSideXLength       = 200;
            visSideYLength       = 70;
            visXwindowMin        = -2000;
            visXwindowMax        = 2000;
            visFlirRegionXadj    = 0;
            visFlirRegionYadj    = 0;
        }

        // ***************************************
        // ******** Process the patterns *********
        // ***************************************
        try
        {
            foreach (CogPMAlignResult patternResult in patternResults)
            {
                // Set the transform for collections
                CogTransform2DLinear l2d = new CogTransform2DLinear();
                l2d.TranslationX = patternResult.GetPose().TranslationX;
                l2d.TranslationY = patternResult.GetPose().TranslationY;
                l2d.Rotation     = patternResult.GetPose().Rotation;
                blobAngleRadians = patternResult.GetPose().Rotation;

                // Check for clipped Patterns
                if ((l2d.TranslationX < visXwindowMin) || (l2d.TranslationX > visXwindowMax))
                {
                    //MessageBox.Show("Fail");
                    continue;
                }

                // Setup the Caliper tool
                xDistEdgeFinder.Region.CenterX  = l2d.TranslationX;
                xDistEdgeFinder.Region.CenterY  = l2d.TranslationY;
                xDistEdgeFinder.Region.Rotation = l2d.Rotation;

                // Run the Caliper tool and get a reference to the results.
                toolGroup.RunTool(xDistEdgeFinder, ref message, ref result);

                //MessageBox.Show("Caliper result: "+xDistEdgeFinder.Results[0].PositionX.ToString());

                //MessageBox.Show("Diff: "+Math.Abs(xDistEdgeFinder.Results[0].PositionX - l2d.TranslationX).ToString());

                //MessageBox.Show("Caliper width: "+xDistEdgeFinder.Results[0].Width.ToString());


                // Check for low width (end to end product that matched)
                if (xDistEdgeFinder.Results[0].Width < 150)
                {
                    continue;
                }

                // Adjust the X based on Caliper tool result
                l2d.TranslationX = xDistEdgeFinder.Results[0].PositionX;

                // Crosshair setup for the Basler
                baslerMarker             = new CogPointMarker();
                baslerMarker.X           = l2d.TranslationX;
                baslerMarker.Y           = l2d.TranslationY;
                baslerMarker.Color       = CogColorConstants.Green;
                baslerMarker.GraphicType = CogPointMarkerGraphicTypeConstants.Crosshair;

                // Flir region
                CogRectangleAffine myFlirRegion = new CogRectangleAffine();
                myFlirRegion.CenterX     = l2d.TranslationX + visFlirRegionXadj;
                myFlirRegion.CenterY     = l2d.TranslationY + visFlirRegionYadj;
                myFlirRegion.Rotation    = l2d.Rotation;
                myFlirRegion.SideXLength = (double)visSideXLength;
                myFlirRegion.SideYLength = (double)visSideYLength;

                blobFlirTool.Region = myFlirRegion;
                toolGroup.RunTool(blobFlirTool, ref message, ref result);

                popsicleHistTool.Region = myFlirRegion;
                toolGroup.RunTool(popsicleHistTool, ref message, ref result);

                // Get the histogram results from the bin
                binHist = popsicleHistTool.Result.GetHistogram();

                // Count total pixels
                histCntr = 1;
                for (int i = 0; i < blobFlirTool.RunParams.SegmentationParams.HardFixedThreshold; i++)
                {
                    histCntr = histCntr + binHist[i];
                }

                myBaslerLabel = new CogGraphicLabel();
                myBaslerLabel.SetXYText(0, 0, "");
                myAreaLabel = new CogGraphicLabel();
                myFlirLabel = new CogGraphicLabel();

                myBaslerLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                myAreaLabel.Font   = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                myFlirLabel.Font   = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                myFlirRegion.Visible = true;

                myBaslerLabel.Alignment = CogGraphicLabelAlignmentConstants.BaselineCenter;
                myFlirLabel.Alignment   = CogGraphicLabelAlignmentConstants.BaselineCenter;
                myAreaLabel.Alignment   = CogGraphicLabelAlignmentConstants.BaselineCenter;

                // Decide to add the popsicle blob to the collection list
                if ((histCntr < minPopsicleHistCount) && (!disableHist))
                {
                    myBaslerLabel.Color = CogColorConstants.Red;
                    myFlirRegion.Color  = CogColorConstants.Red;


                    // Show the hist count in the Flir image
                    myFlirLabel.Color = CogColorConstants.Red;
                    if (showHist)
                    {
                        myFlirLabel.SetXYText(patternResult.GetPose().TranslationX, patternResult.GetPose().TranslationY, "Size: " + histCntr.ToString());
                    }
                    thermalFailures++;
                }
                else
                {
                    myBaslerLabel.Color = CogColorConstants.Green;
                    myAreaLabel.Color   = CogColorConstants.Green;

                    // If histogram check is disabled, draw rectangle in yellow, else green
                    if (disableHist)
                    {
                        myFlirRegion.Color = CogColorConstants.Yellow;
                    }
                    else
                    {
                        if ((histCntr > 1) && (patternResult.Score >= patternBaslerTool.RunParams.AcceptThreshold))
                        {
                            myFlirRegion.Color = CogColorConstants.Green;
                        }
                        else
                        {
                            myFlirRegion.Color = CogColorConstants.Yellow;
                        }
                    }

                    // Show the hist count in the Flir image
                    if (patternResult.Score >= patternBaslerTool.RunParams.AcceptThreshold)
                    {
                        myFlirLabel.Color = CogColorConstants.Green;
                    }
                    else
                    {
                        myFlirLabel.Color = CogColorConstants.Yellow;
                    }
                    if (showHist)
                    {
                        myFlirLabel.SetXYText(patternResult.GetPose().TranslationX, patternResult.GetPose().TranslationY, "Size: " + histCntr.ToString());
                    }


                    // Convert blob angle to degrees
                    blobAngle = patternResult.GetPose().Rotation * 180 / Math.PI;

                    //MessageBox.Show("blob.Angle:" + blobAngle.ToString());

                    if ((blobAngle > (double)minPickAngle) && (blobAngle < (double)maxPickAngle))
                    {
                        if (patternResult.Score >= patternBaslerTool.RunParams.AcceptThreshold)
                        {
                            malBlobPoses.Add(l2d);
                        }
                        else
                        {
                            myAreaLabel.Color = CogColorConstants.Red;
                        }

                        if (showArea)
                        {
                            myAreaLabel.SetXYText(l2d.TranslationX, l2d.TranslationY - 15, "Score: " + (patternResult.Score * 100).ToString("0"));
                        }
                        AreaLabel.Add(myAreaLabel);
                    }
                    else
                    {
                        myBaslerLabel.Color = CogColorConstants.Red;
                        myBaslerLabel.SetXYText(l2d.TranslationX, l2d.TranslationY, "Angle: " + blobAngle.ToString("0"));

                        myFlirLabel.Color  = CogColorConstants.Red;
                        myFlirRegion.Color = CogColorConstants.Red;
                        angleFailures++;
                    }
                }

                myBaslerLabel.Rotation = blobAngleRadians;

                BaslerLabels.Add(myBaslerLabel);
                FlirLabels.Add(myFlirLabel);
                FlirRectangles.Add(myFlirRegion);
                Crosshairs.Add(baslerMarker);
            }

            // Update group output terminals
            toolGroup.SetScriptTerminalData("AngleFailures", angleFailures);
            toolGroup.SetScriptTerminalData("ThermalFailures", thermalFailures);
            toolGroup.SetScriptTerminalData("BlobCollection", malBlobPoses);
        }
        catch (Exception ex)
        {
            toolGroup.SetScriptTerminalData("BaslerStatus", "Script Error during Pattern processing: " + ex.Message);
            malBlobPoses.Clear(); // Clear positional data for this frame
        }

        // Returning False indicates we ran the tools in script, and they should not be
        // run by VisionPro
        return(false);
    }