Example #1
0
        public static PatternMatchingTransformationResult Execute(
            EImageBW8 eImage,
            float max_AngleOffset,
            float max_XTranslateOffset,
            float max_YTranslateOffset,
            int maxNumberOfTrial,
            EuresysDoublePatternMatcher eMatcher,
            PatternMatcherParameters patternMatcherParameters,
            EROIBW8 matcherROI,
            ELineGauge gauge1,
            ELineGauge gauge2,
            ELineGauge gauge3,
            ELineGauge gauge4,
            double filterTolerance,
            double fiducialOffset,
            WaferOrientation fiducialOrientation,
            bool isIncludeFiducialTolerance,
            Func <ELineGauge,
                  ELineGauge,
                  ELineGauge,
                  ELineGauge,
                  EImageBW8,
                  double,
                  double,
                  WaferOrientation,
                  bool,
                  PointF> FindCenter,
            PointF TeacherMidPoint)
        {
            List <string> messages    = new List <string>();
            float         thetaOffset = -1;
            float         xOffset     = -1;
            float         yOffset     = -1;

            bool isPass = false;
            bool isThetaOffsetWithinTolerance = false;
            bool isXOffsetWithinTolerance     = false;
            bool isYOffsetWithinTolerance     = false;

            int xTranslationCount     = -1;
            int yTranslationCount     = -1;
            int thetaTranslationCount = -1;

            PointF calibratedCenterPoint = new PointF(
                patternMatcherParameters.WaferCenterXPos,
                patternMatcherParameters.WaferCenterYPos);
            PointF currentWaferCenterPoint            = new PointF(-1, -1);
            EuresysDoubleMatcherResults matchedResult = null;

            messages.Add("Maximum NumberOfTrial is " + maxNumberOfTrial);
            messages.Add("Accepted AngleOffset is below " + max_AngleOffset);
            messages.Add("Accepted X Offset is below " + max_XTranslateOffset);
            messages.Add("Accepted Y Offset is below " + max_YTranslateOffset);

            #region X Offset

            for (int i = 0; i < maxNumberOfTrial; i++)
            {
                // Find X Offset
                currentWaferCenterPoint = FindCenter.Invoke(
                    gauge1,
                    gauge2,
                    gauge3,
                    gauge4,
                    eImage,
                    filterTolerance,
                    fiducialOffset,
                    fiducialOrientation,
                    isIncludeFiducialTolerance);

                xOffset = calibratedCenterPoint.X - currentWaferCenterPoint.X;

                if (Math.Abs(xOffset) < max_XTranslateOffset)
                {
                    isXOffsetWithinTolerance = true;
                    xTranslationCount        = i;
                    messages.Add("XOffset within tolerance");
                    messages.Add("Number of X tranlation performed = " + xTranslationCount);
                    break;
                }

                else
                {
                    isXOffsetWithinTolerance = false;
                    messages.Add(string.Format("XOffset: {0} out of tolerance", xOffset));
                    eImage = ImageTransformer.TranslateImage_X(eImage, xOffset);
                    messages.Add("Image X Translated by " + xOffset);
                }

                if (i == maxNumberOfTrial)
                {
                    xTranslationCount = i;
                    messages.Add("Maximum number of trials for XOffset reached");
                }
            }

            #endregion XOffset

            #region Y Offset

            for (int i = 0; i <= maxNumberOfTrial; i++)
            {
                // Find Y Offset
                currentWaferCenterPoint = FindCenter.Invoke(
                    gauge1,
                    gauge2,
                    gauge3,
                    gauge4,
                    eImage,
                    filterTolerance,
                    fiducialOffset,
                    fiducialOrientation,
                    isIncludeFiducialTolerance);
                yOffset = calibratedCenterPoint.Y - currentWaferCenterPoint.Y;

                if (Math.Abs(yOffset) < max_YTranslateOffset)
                {
                    isYOffsetWithinTolerance = true;
                    yTranslationCount        = i;
                    messages.Add("YOffset within tolerance");
                    messages.Add("Number of Y tranlation performed = " + yTranslationCount);
                    break;
                }

                else
                {
                    isYOffsetWithinTolerance = false;
                    messages.Add(string.Format("YOffset: {0} out of tolerance", yOffset));
                    eImage = ImageTransformer.TranslateImage_Y(eImage, yOffset);
                    messages.Add("Image Y Translated by " + yOffset);
                }

                if (i == maxNumberOfTrial)
                {
                    yTranslationCount = i;
                    messages.Add("Maximum number of trials for YOffset reached");
                }
            }

            #endregion Y offset

            #region Theta Offset

            if (isXOffsetWithinTolerance && isYOffsetWithinTolerance)
            {
                for (int i = 0; i <= maxNumberOfTrial; i++)
                {
                    currentWaferCenterPoint = FindCenter.Invoke(
                        gauge1,
                        gauge2,
                        gauge3,
                        gauge4,
                        eImage,
                        filterTolerance,
                        fiducialOffset,
                        fiducialOrientation,
                        isIncludeFiducialTolerance);

                    EuresysEROIHelper.AttachROI(eImage, matcherROI);
                    eMatcher.MatchPatterns(matcherROI);


                    WaferOrientation patternOrientation = FindWaferOrientation(currentWaferCenterPoint, TeacherMidPoint);


                    matchedResult = PatternMatcherParameterHelper.CreateDoublePatternMatcherResult(
                        eMatcher,
                        matcherROI,
                        patternMatcherParameters.OriginalXPos_pattern1,
                        patternMatcherParameters.OriginalYPos_pattern1,
                        currentWaferCenterPoint,
                        patternMatcherParameters.DefaultAngleOffset,
                        patternOrientation);


                    thetaOffset = matchedResult.AngleBetweenResult;
                    if (thetaOffset > 180)
                    {
                        throw new ArgumentOutOfRangeException("theta Offset must not be higher than 180");
                    }
                    else if (thetaOffset < -180)
                    {
                        throw new ArgumentOutOfRangeException("theta Offset must not be lesser than 180");
                    }

                    if (Math.Abs(thetaOffset) < max_AngleOffset)
                    {
                        isThetaOffsetWithinTolerance = true;
                        thetaTranslationCount        = i;
                        messages.Add("Theta Offset within tolerance");
                        messages.Add("Number of Theta tranlation performed = " + thetaTranslationCount);
                        break;
                    }
                    else
                    {
                        isThetaOffsetWithinTolerance = false;
                        messages.Add(string.Format("Theta Offset: {0} out of tolerance", thetaOffset));
                        eImage = ImageTransformer.RotateImage(eImage, thetaOffset, calibratedCenterPoint);
                        messages.Add("Image Theta Rotated by " + thetaOffset + " around calibrated Center Point");
                    }

                    if (i == maxNumberOfTrial)
                    {
                        thetaTranslationCount = i;
                        messages.Add("Maximum number of trials for Theta Offset reached");
                    }
                }
            }

            else
            {
                if (!isXOffsetWithinTolerance)
                {
                    messages.Add("Theta offset correction skipped due to X Offset out of tolerance");
                }

                if (!isYOffsetWithinTolerance)
                {
                    messages.Add("Theta offset correction skipped due to X Offset out of tolerance");
                }
            }

            #endregion Theta Offset

            #region Final Result

            messages.Add("Final Angle is " + Math.Round(thetaOffset, 4));
            messages.Add("Final X Offset is " + Math.Round(xOffset, 4));
            messages.Add("Final Y Offset is " + Math.Round(yOffset, 4));

            if (isThetaOffsetWithinTolerance &&
                isXOffsetWithinTolerance &&
                isYOffsetWithinTolerance)
            {
                isPass = true;
                messages.Add("Result is Pass");
            }

            else
            {
                isPass = false;
                messages.Add("Result is False");
            }

            return(new PatternMatchingTransformationResult()
            {
                MatchedResult = matchedResult,
                eImageAfterTransformation = eImage,
                FinalAngleOffset = thetaOffset,
                FinalXOffset = xOffset,
                FinalYOffset = yOffset,
                IsPass = isPass,
                IsThetaOffsetWithinTolerance = isThetaOffsetWithinTolerance,
                IsXOffsetWithinTolerance = isXOffsetWithinTolerance,
                IsYOffsetWithinTolerance = isXOffsetWithinTolerance,
                XTranlastionCount = xTranslationCount,
                YTranslationCount = yTranslationCount,
                ThetaTranslationCount = thetaTranslationCount,
                FinalWaferCenter = currentWaferCenterPoint,
                Messages = messages,
            });

            #endregion Final Result
        }
        public static bool TeachTeacherROI(
            string RecipeName,
            EuresysDoublePatternMatcher PatternMatcher_,
            PatternMatcherParameters PatternMatcherParameters_,
            EROIBW8 eROIForPatternTeaching1_,
            EROIBW8 eROIForPatternTeaching2_,
            iEuresysROI MatcherEROI,
            PointF WaferCenterPoint,
            WaferOrientation patternOrientation)
        {
            string PatternFilePath_One      = PatternMatcherParameterHelper.GetLeftPatternFilePath(RecipeName);
            string PatternFilePath_Two      = PatternMatcherParameterHelper.GetRightPatternFilePath(RecipeName);
            string PatternImageFilePath_One = PatternMatcherParameterHelper.GetLeftPatternImageFilePath(RecipeName);
            string PatternImageFilePath_Two = PatternMatcherParameterHelper.GetRightPatternImageFilePath(RecipeName);

            PatternMatcher_.TeachAndSaveEMatcher(
                PatternMatcherParameters_,
                eROIForPatternTeaching1_,
                eROIForPatternTeaching2_,
                PatternFilePath_One,
                PatternFilePath_Two,
                PatternImageFilePath_One,
                PatternImageFilePath_Two);

            if (PatternMatcher_.Pattern1.IsVoid)
            {
                goto Fail;
            }
            if (PatternMatcher_.Pattern2.IsVoid)
            {
                goto Fail;
            }

            // Match
            EROIBW8 matcherROI = MatcherEROI.GetROI(0);

            PatternMatcher_.MatchPatterns(matcherROI);

            EMatcher eMatcher1 = PatternMatcher_.EMatcher1;
            EMatcher eMatcher2 = PatternMatcher_.EMatcher2;

            if (eMatcher1.NumPositions != 1)
            {
                string errorMessage = "Pattern 1: Number of patterns matched is not equal to one";
                VisionLogger.Log(LogType.Exception, "PatternMatcherManager", errorMessage);
                VisionNotifier.AddNotification(errorMessage);
                goto Fail;
            }

            if (eMatcher2.NumPositions != 1)
            {
                string errorMessage = "Pattern 2: Number of patterns matched is not equal to one";
                VisionLogger.Log(LogType.Exception, "PatternMatcherManager", errorMessage);
                VisionNotifier.AddNotification(errorMessage);
                goto Fail;
            }

            EROIBW8 matcherEROI_1 = MatcherEROI.GetROI(0);

            float OriginalXPos_pattern1 = eMatcher1.GetPosition(0).CenterX + (matcherEROI_1.OrgX);
            float OriginalYPos_pattern1 = eMatcher1.GetPosition(0).CenterY + (matcherEROI_1.OrgY);
            float OriginalXPos_pattern2 = eMatcher2.GetPosition(0).CenterX + (matcherEROI_1.OrgX);
            float OriginalYPos_pattern2 = eMatcher2.GetPosition(0).CenterY + (matcherEROI_1.OrgY);
            float WaferCenterXPos       = WaferCenterPoint.X;
            float WaferCenterYPos       = WaferCenterPoint.Y;

            PointF p1 = new PointF(OriginalXPos_pattern1, OriginalYPos_pattern1);
            PointF p2 = new PointF(OriginalXPos_pattern2, OriginalYPos_pattern2);
            float  PatternDefaultAngleOffset = (float)CalculateAngleBetween3Points.Execute(
                p1,
                p2,
                WaferCenterPoint,
                0,
                true,
                patternOrientation);

            // Replace value
            PatternMatcherParameters_.OriginalXPos_pattern1 = OriginalXPos_pattern1;
            PatternMatcherParameters_.OriginalYPos_pattern1 = OriginalYPos_pattern1;
            PatternMatcherParameters_.OriginalXPos_pattern2 = OriginalXPos_pattern2;
            PatternMatcherParameters_.OriginalYPos_pattern2 = OriginalYPos_pattern2;
            PatternMatcherParameters_.WaferCenterXPos       = WaferCenterXPos;
            PatternMatcherParameters_.WaferCenterYPos       = WaferCenterYPos;
            PatternMatcherParameters_.DefaultAngleOffset    = PatternDefaultAngleOffset;
            return(true);

Fail:
            return(false);
        }