public InspectionPattern()
 {
     PatternProc = new CogPMAlignTool();
     PatternProc.Pattern.TrainAlgorithm      = CogPMAlignTrainAlgorithmConstants.PatQuick;
     PatternProc.RunParams.ContrastThreshold = 5;
     PatternResults = new CogPMAlignResults();
 }
        private void AssignToolResult(CogPMAlignTool cogPMAlign)
        {
            CogPMAlignResults cogPMAlignResults = cogPMAlign.Results;

            if (cogPMAlignResults != null)
            {
                AssignToolResult(cogPMAlign.Name, cogPMAlignResults);
            }
        }
 private void AssignToolResult(string parent, CogPMAlignResults results)
 {
     AddResult(parent + ".Count", results.Count);
     for (int i = 0; i < results.Count; i++)
     {
         AddResult(string.Format("{0}[{1}].Score", parent, i), results[i].Score);
         AddResult(string.Format("{0}[{1}].TranslationX", parent, i), results[i].GetPose().TranslationX);
         AddResult(string.Format("{0}[{1}].TranslationY", parent, i), results[i].GetPose().TranslationY);
         AddResult(string.Format("{0}[{1}].Rotation", parent, i), results[i].GetPose().Rotation);
     }
 }
Example #4
0
        public bool RunTool_Pattern(int cameraModule) //no need
        {
            bool   runStatus = true;
            String colResult;
            CogToolResultConstants toolColResult;
            int i;

            if (this.cogToolRun[cameraModule] != null)
            {
                //configure the vision tool
                this.cogToolRun[cameraModule].Inputs["InputImage"].Value         = grabImage[cameraModule] as CogImage8Grey;
                this.cogToolRun[cameraModule].Inputs["SaveImage"].Value          = true;
                this.cogToolRun[cameraModule].Inputs["ImageDirectoryPath"].Value = "c:\\Seagate\\vision\\";

                this.cogToolRun[cameraModule].Run();

                ICogRunStatus cogStatus = this.cogToolRun[cameraModule].RunStatus;

                if (cogStatus.Result == CogToolResultConstants.Error)
                {
                    runStatus = false;
                    // error default all to invalid
                    for (i = 0; i < MAX_RESULT; i++)
                    {
                        this.inspectResult[cameraModule, i] = HGAStatus.Unknown;
                    }
                }
                else
                {
                    double[] score          = { 0, 0, 0, 0, 0, 0 };
                    double[] centerofmass_X = { 0, 0, 0, 0, 0, 0 };

                    cogpmaligntool = (CogPMAlignTool)this.cogToolRun[cameraModule].Tools["PatternTool"];
                    CogPMAlignResults resultcollection = cogpmaligntool.Results;
                    for (int x = 0; x < resultcollection.Count; x++)
                    {
                        score[x]          = resultcollection[x].Score;
                        centerofmass_X[x] = resultcollection[x].GetPose().TranslationX;
                    }
                }
            }
            else
            {
                runStatus = false;
            }

            return(runStatus);
        }
 private void GetResult()
 {
     PatternResults = new CogPMAlignResults();
     PatternResults = PatternProc.Results;
 }
Example #6
0
 public InspectionMultiPattern()
 {
     PatternProc = new CogPMAlignTool();
     PatternResults = new CogPMAlignResults();
     AnglePointProc = new CogAnglePointPointTool();
 }
Example #7
0
        private void btnFindSearchArea_Click(object sender, EventArgs e)
        {
            double _OffsetX = MapDataParam.Unit.PatternAreaOriginX - MapDataParam.Unit.PatternAreaCenterX;
            double _OffsetY = MapDataParam.Unit.PatternAreaOriginY - MapDataParam.Unit.PatternAreaCenterY;

            InspPattern.SetPatternReference(MapDataParam.Info.UnitPattern);
            InspPattern.SetMatchingParameter(MapDataParam.Info.FindCount, MapDataParam.Info.FindScore);

            if (false == InspPattern.Run(OriginImage))
            {
                MessageBox.Show("Pattern Not Found"); return;
            }

            CogPMAlignResults _PatternResult = InspPattern.GetResults();

            if (null == _PatternResult)
            {
                return;
            }

            MapDataParam.Info.UnitListCenterX.Clear();
            MapDataParam.Info.UnitListCenterY.Clear();
            MapDataParam.Info.UnitListWidth.Clear();
            MapDataParam.Info.UnitListHeight.Clear();
            if (_PatternResult.Count > 0)
            {
                double[] _OriginX, _OriginY, _CenterX, _CenterY, _Width, _Height;
                _OriginX = new double[_PatternResult.Count];
                _OriginY = new double[_PatternResult.Count];
                _CenterX = new double[_PatternResult.Count];
                _CenterY = new double[_PatternResult.Count];
                _Width   = new double[_PatternResult.Count];
                _Height  = new double[_PatternResult.Count];

                uint          _RowCount         = Convert.ToUInt32(numUpDownUnitRowCount.Value);
                uint          _ColCount         = Convert.ToUInt32(numUpDownUnitColumnCount.Value);
                CenterPoint[] _CenterPointArray = new CenterPoint[_RowCount * _ColCount];
                for (int iLoopCount = 0; iLoopCount < _PatternResult.Count; ++iLoopCount)
                {
                    _OriginX[iLoopCount] = _PatternResult[iLoopCount].GetPose().TranslationX;
                    _OriginY[iLoopCount] = _PatternResult[iLoopCount].GetPose().TranslationY;
                    _CenterX[iLoopCount] = _PatternResult[iLoopCount].GetPose().TranslationX - _OffsetX;
                    _CenterY[iLoopCount] = _PatternResult[iLoopCount].GetPose().TranslationY - _OffsetY;
                    _Width[iLoopCount]   = _PatternResult.GetTrainArea().Width;
                    _Height[iLoopCount]  = _PatternResult.GetTrainArea().Height;

                    CogRectangle _FindPattern = new CogRectangle();
                    _FindPattern.SetCenterWidthHeight(_CenterX[iLoopCount], _CenterY[iLoopCount], MapDataParam.Unit.SearchAreaWidth, MapDataParam.Unit.SearchAreaHeight);
                    kpTeachDisplay.DrawStaticShape(_FindPattern, "SearchArea" + (iLoopCount + 1));

                    CogPointMarker _OriginPoint = new CogPointMarker();
                    _OriginPoint.SetCenterRotationSize(_OriginX[iLoopCount], _OriginY[iLoopCount], 0, 2);
                    kpTeachDisplay.DrawStaticShape(_OriginPoint, "PatternOrigin" + (iLoopCount + 1), CogColorConstants.Green, 12);

                    MapDataParam.Info.UnitListCenterX.Add(_CenterX[iLoopCount]);
                    MapDataParam.Info.UnitListCenterY.Add(_CenterY[iLoopCount]);
                    MapDataParam.Info.UnitListWidth.Add(_Width[iLoopCount]);
                    MapDataParam.Info.UnitListHeight.Add(_Height[iLoopCount]);

                    if (_CenterPointArray.Length > iLoopCount)
                    {
                        _CenterPointArray[iLoopCount]   = new CenterPoint();
                        _CenterPointArray[iLoopCount].X = _CenterX[iLoopCount];
                        _CenterPointArray[iLoopCount].Y = _CenterY[iLoopCount];
                    }
                }

                if ((_RowCount * _ColCount) != _PatternResult.Count)
                {
                    MessageBox.Show("Unit 개수와 Find Result 개수가 맞지 않습니다.");
                    return;
                }

                else
                {
                    CenterPoint[,] _SortedCenterPoint = CenterPointSort(_RowCount, _ColCount, _CenterPointArray);

                    MapDataParam.Info.UnitListCenterX.Clear();
                    MapDataParam.Info.UnitListCenterY.Clear();
                    for (int iLoopCount = 0; iLoopCount < _RowCount; ++iLoopCount)
                    {
                        for (int jLoopCount = 0; jLoopCount < _ColCount; ++jLoopCount)
                        {
                            MapDataParam.Info.UnitListCenterX.Add(_SortedCenterPoint[iLoopCount, jLoopCount].X);
                            MapDataParam.Info.UnitListCenterY.Add(_SortedCenterPoint[iLoopCount, jLoopCount].Y);
                        }
                    }

                    SelectingRectName = SelectedRectName = "";
                    IsDrawPatterns    = true;
                    MessageBox.Show("Pattern Find Complete");
                }
            }
        }
Example #8
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);
    }