Exemple #1
0
        /// <summary>
        /// With each execution step the shape-based model is searched in
        /// the current test image. The detection result is then compared
        /// with the previous results and the overall statistics is adjusted.
        /// </summary>
        public override bool ExecuteStep()
        {
            string fileKey, imgNumStr;
            int    val, i;
            string matchFormatStr;
            int    actualMatchNum;
            int    specifiedMatchNum;
            int    maxMatchNum;
            double score, time, row, col, angleA, angleB, scaleR, scaleC;

            //----------------------第一步:检测是否有测试图像-------------------------//
            if (!Iterator.MoveNext()) //未有测试图像,停止优化
            {
                return(false);
            }

            fileKey = (string)Iterator.Current;
            MatchAssistant.SetTestImage(fileKey);

            if (!(OptSuccess = MatchAssistant.DetectShapeModel())) ////查找匹配模板的实例失败,停止统计
            {
                return(false);
            }



            Result         = MatchAssistant.Result;
            actualMatchNum = Result.MatchCount;

            // --------------determine recognition rate ----------------
            specifiedMatchNum = Parameter.RecogSpecifiedMatchNum;
            maxMatchNum       = Parameter.NumToMatch;

            _specifiedMatchNum += specifiedMatchNum;
            _maxMatchNum       += maxMatchNum;
            _foundMatchNum     += actualMatchNum;

            if (actualMatchNum > 0) //若找到匹配模板的实例数大于零,则'至少匹配1个数量的实例'的标记数量更新
            {
                _imageWithOneMatchNum++;
            }

            if (actualMatchNum >= specifiedMatchNum) //若找到匹配模板的实例数大于预期(指定)的数量,则'匹配指定数量的实例'的标记数量更新
            {
                _imageWithSpecifiedMatchNum++;
            }

            if (actualMatchNum == maxMatchNum)  //若找到匹配模板的实例数等于'可能的(最大)数量',则'匹配最大数量的实例'的标记数量更新
            {
                _imageWithMaxMatchNum++;
            }

            CurrentImageIndex++;          //图像索引递增
            InspectTabRecogRateData[2] = "-";
            InspectTabRecogRateData[3] = "-";
            InspectTabRecogRateData[4] = "-";

            imgNumStr = " of " + CurrentImageIndex + "图像)";

            val = _imageWithOneMatchNum; //'识别率'% ('至少匹配一个模板模式计数' of '当前图像索引' 图像 )
            InspectTabRecogRateData[0] = System.Math.Round(100.0 * ((double)val / CurrentImageIndex), 2) + " % (" + val + imgNumStr;

            val = _imageWithSpecifiedMatchNum; //'识别率'% ('指定匹配数个模板模式计数' of '当前图像索引' 图像 )
            InspectTabRecogRateData[1] = System.Math.Round(100.0 * ((double)val / CurrentImageIndex), 2) + " % (" + val + imgNumStr;

            if (_maxMatchNum > 0)
            {
                matchFormatStr = " of " + _maxMatchNum + "模板)";
                val            = _imageWithMaxMatchNum; //'识别率'% ('最大匹配数个模板模式计数' of '当前图像索引' 图像 )
                this.InspectTabRecogRateData[2] = System.Math.Round(100.0 * ((double)val / CurrentImageIndex), 2) + " % (" + val + imgNumStr;

                val = _foundMatchNum;        //'识别率'% ('实际匹配模板个数' of '可能匹配个数' 模板 )
                InspectTabRecogRateData[3] = System.Math.Round(100.0 * ((double)val / _maxMatchNum), 2) + " % (" + val + matchFormatStr;
            }

            if (_specifiedMatchNum > 0)
            {
                matchFormatStr             = " of " + _specifiedMatchNum + "模板)";
                val                        = _foundMatchNum; //'识别率'% ('实际匹配模板个数' of '指定匹配个数' 模板 )
                InspectTabRecogRateData[4] = System.Math.Round(100.0 * ((double)val / _specifiedMatchNum), 2) + " % (" + val + matchFormatStr;
            }

            RecoginzedAndStatisticedDel(ShapeModelOpt.UPDATE_INSP_RECOGRATE);

            // ---------------- determine statistics data ------------
            if (actualMatchNum > 0) //实际匹配数大于零
            {
                i = 0;
                if (!_modelFound) //初始赋值
                {
                    _scoreMin      = _scoreMax = Result.Score[0].D;
                    _elapseTimeMin = _elapseTimeMax = Result.ElapseTime;
                    _rowMin        = _rowMax = Result.Row[0].D;
                    _colMin        = _colMax = Result.Col[0].D;
                    _scaleRowMin   = _scaleRowMax = Result.ScaleRow[0].D;
                    _scaleColMin   = _scaleColMax = Result.ScaleCol[0].D;
                    _angleMin      = _angleMax = Result.Angle[0].D;
                    _modelFound    = true;
                    i++;
                }

                for (; i < actualMatchNum; i++)
                {
                    score = Result.Score[i].D;
                    if (score < _scoreMin)
                    {
                        _scoreMin = score;
                    }
                    else if (score > _scoreMax)
                    {
                        _scoreMax = score;
                    }

                    row = Result.Row[0].D;
                    if (row < _rowMin)
                    {
                        _rowMin = row;
                    }
                    else if (row > _rowMax)
                    {
                        _rowMax = row;
                    }

                    col = Result.Col[0].D;
                    if (col < _colMin)
                    {
                        _colMin = col;
                    }
                    else if (col > _colMax)
                    {
                        _colMax = col;
                    }

                    angleA = Result.Angle[0].D;
                    if (angleA < _angleMin)
                    {
                        _angleMin = angleA;
                    }
                    else if (angleA > _angleMax)
                    {
                        _angleMax = angleA;
                    }

                    scaleR = Result.ScaleRow[0].D;
                    if (scaleR < _scaleRowMin)
                    {
                        _scaleRowMin = scaleR;
                    }
                    else if (scaleR > _scaleRowMax)
                    {
                        _scaleRowMax = scaleR;
                    }

                    scaleC = Result.ScaleCol[0].D;
                    if (scaleC < _scaleColMin)
                    {
                        _scaleColMin = scaleC;
                    }
                    else if (scaleC > _scaleColMax)
                    {
                        _scaleColMax = scaleC;
                    }
                }

                time = Result.ElapseTime;
                if (time < _elapseTimeMin)
                {
                    _elapseTimeMin = time;
                }
                else if (time > _elapseTimeMax)
                {
                    _elapseTimeMax = time;
                }
            }

            if (_modelFound)
            {
                InspectTabResultsData[0] = "" + System.Math.Round(_scoreMin, 2);
                InspectTabResultsData[1] = "" + System.Math.Round(_scoreMax, 2);
                InspectTabResultsData[2] = "" + System.Math.Round((_scoreMax - _scoreMin), 2);

                InspectTabResultsData[3] = "" + System.Math.Round(_elapseTimeMin, 2);
                InspectTabResultsData[4] = "" + System.Math.Round(_elapseTimeMax, 2);
                InspectTabResultsData[5] = "" + System.Math.Round((_elapseTimeMax - _elapseTimeMin), 2);

                InspectTabResultsData[6] = "" + System.Math.Round(_rowMin, 2);
                InspectTabResultsData[7] = "" + System.Math.Round(_rowMax, 2);
                InspectTabResultsData[8] = "" + System.Math.Round((_rowMax - _rowMin), 2);

                InspectTabResultsData[9]  = "" + System.Math.Round(_colMin, 2);
                InspectTabResultsData[10] = "" + System.Math.Round(_colMax, 2);
                InspectTabResultsData[11] = "" + System.Math.Round((_colMax - _colMin), 2);

                angleA = (double)_angleMin * 180.0 / System.Math.PI;
                angleB = (double)_angleMax * 180.0 / System.Math.PI;
                InspectTabResultsData[12] = "" + System.Math.Round(angleA, 2) + "°";
                InspectTabResultsData[13] = "" + System.Math.Round(angleB, 2) + "°";
                InspectTabResultsData[14] = "" + System.Math.Round((angleB - angleA), 2) + "°";

                InspectTabResultsData[15] = "" + System.Math.Round(_scaleRowMin, 2);
                InspectTabResultsData[16] = "" + System.Math.Round(_scaleRowMax, 2);
                InspectTabResultsData[17] = "" + System.Math.Round((_scaleRowMax - _scaleRowMin), 2);

                InspectTabResultsData[18] = "" + System.Math.Round(_scaleColMin, 2);
                InspectTabResultsData[19] = "" + System.Math.Round(_scaleColMax, 2);
                InspectTabResultsData[20] = "" + System.Math.Round((_scaleColMax - _scaleColMin), 2);

                RecoginzedAndStatisticedDel(ShapeModelOpt.UPDATE_INSP_RESULTS);
            }
            return(CurrentImageIndex < ImageCount);
        }
Exemple #2
0
        /// <summary>
        /// In each execution step a certain parameter set is applied
        /// to the whole set of test images and the performance is then
        /// evaluated.
        /// </summary>
        public override bool ExecuteStep()
        {
            double cScoreMin, cGreediness, cRecogRate;
            string fileKey;
            int    actualMatchNum, specifiedMatchNum;
            bool   satisfiedRate;

            //----------------------第一步:检测是否有测试图像-------------------------//

            if (!Iterator.MoveNext()) //未有测试图像,停止优化
            {
                return(false);
            }

            //----------------------第二步:当前检测参数下图像处理---------------------//
            cScoreMin   = _currentScoreMin / 100.0;
            cGreediness = _currentGreediness / 100.0;

            OptimizationStatus = "测试图像 " + (CurrentImageIndex + 1).ToString() +
                                 "-最小得分:" + cScoreMin +
                                 "-贪婪度:" + cGreediness;
            RecoginzedAndStatisticedDel(ShapeModelOpt.UPDATE_RECOG_STATISTICS_STATUS);

            fileKey = (string)Iterator.Current;
            MatchAssistant.SetTestImage(fileKey);
            MatchAssistant.SetMinScore(cScoreMin);
            MatchAssistant.SetGreediness(cGreediness);

            if (!MatchAssistant.DetectShapeModel()) //查找匹配模板的实例失败,停止优化
            {
                return(false);
            }


            Result            = MatchAssistant.Result;
            actualMatchNum    = Result.MatchCount;
            specifiedMatchNum = 0;                             //每张图像中的指定匹配实例数

            switch (Parameter.RecogAccuracyMode)
            {
            case Communal.ShapeModelParameter.RECOG_MODE_SPECIFIEDNUM:
                specifiedMatchNum = Parameter.RecogSpecifiedMatchNum;
                break;

            case Communal.ShapeModelParameter.RECOG_MODE_ATLEASTONE:
                specifiedMatchNum = 1;
                if (actualMatchNum > 1)
                {
                    actualMatchNum = 1;
                }
                break;

            case Communal.ShapeModelParameter.RECOG_MODE_MAXIMUMNUM:
                specifiedMatchNum = Parameter.NumToMatch;
                break;

            default: break;
            }

            _matchNum          += actualMatchNum;    //实际匹配数
            _specifiedMathcNum += specifiedMatchNum; //指定匹配数总数(所有图像中指定匹配实例数之和)

            cRecogRate          = (_specifiedMathcNum > 0) ? (100.0 * _matchNum / _specifiedMathcNum) : 0.0;
            _currentElapseTime  = _currentElapseTime * CurrentImageIndex + Result.ElapseTime;
            _currentElapseTime /= ++CurrentImageIndex;

            //Write data into strings and call for update
            RecogTabOpimizationData[0] = "" + System.Math.Round(cScoreMin, 2);
            RecogTabOpimizationData[1] = "" + System.Math.Round(cGreediness, 2);
            RecogTabOpimizationData[2] = "" + System.Math.Round(cRecogRate, 2) + "%";

            if (_currentElapseTime < 1000)
            {
                RecogTabOpimizationData[3] = "" + System.Math.Round(_currentElapseTime, 2) + " ms";
            }
            else
            {
                RecogTabOpimizationData[3] = "" + System.Math.Round(_currentElapseTime / 1000.0, 2) + " s";
            }

            RecoginzedAndStatisticedDel(ShapeModelOpt.UPDATE_RECOG_UPDATED_VALS); //更新:识别更新后的参数值

            if (CurrentImageIndex < ImageCount)                                   //未达到最后图像,返回True(继续按当前参数处理图像:识别并显示)
            {
                return(true);
            }

            //----------------------第三步:当前参数条件下,判断识别率-------------------------//
            Iterator.Reset();
            CurrentImageIndex  = 0;
            _matchNum          = 0;
            _specifiedMathcNum = 0;

            satisfiedRate = (Parameter.RecogRateOpt == 0) ?  //评估搜索的启发式选项:==识别率,>=识别率
                            (System.Math.Abs((double)cRecogRate - Parameter.RecogRate) < 0.001) : (cRecogRate >= Parameter.RecogRate);

            //----------------------第三步A:识别率满足设定,递增贪婪度-------------------------//
            if (satisfiedRate) //当前参数条件下,满足识别率条件
            {
                OptSuccess = true;
                if (_currentElapseTime < _optElapseTime)//识别平均时间是否比上次短,短则更新优化后的参数
                {
                    _optScoreMin   = _currentScoreMin;
                    _optGreediness = _currentGreediness;
                    _optElapseTime = _currentElapseTime;

                    RecogTabOpimizationData[4] = "" + System.Math.Round(_optScoreMin / 100.0, 2);
                    RecogTabOpimizationData[5] = "" + System.Math.Round(_optGreediness / 100.0, 2);
                    RecogTabOpimizationData[6] = "" + System.Math.Round(cRecogRate, 2) + " %";

                    RecogTabOpimizationData[7] = RecogTabOpimizationData[3];
                    RecoginzedAndStatisticedDel(ShapeModelOpt.UPDATE_RECOG_OPTIMAL_VALS); //更新:最优化参数值
                }

                _currentGreediness += _greedinessStep;
                return(_currentGreediness <= 100);        //贪婪度是否超出限定值:未超出--返回True(继续优化贪婪度),超出--返回False(停止优化)
            }


            //----------------------第三步B:识别率不满足设定,递减匹配得分-------------------------//
            _currentScoreMin += _scoreMinStep;           //未满足识别率条件,递减最小匹配得分,准备再优化

            if (OptSuccess)                              //[优化参数成功],若当前最小匹配得分:大于10,则返回True(继续优化);否则返回False(停止优化)
            {
                return(_currentScoreMin >= 10);          //即优化贪婪度到极限前,出现某个贪婪度下的识别率不满足条件,则再递减匹配得分,若可以满足识别率,则因贪婪度到底极限而停止优化;否则只能到最低匹配得分而停止优化
            }
            //----------------------第四步B:判断匹配得分是否超出底线-------------------------//
            return(_currentScoreMin > 0);               //[优化参数未成功]若当前最小匹配得分:大于0,则返回True(继续优化);否则返回False(停止优化)
        }