Exemple #1
0
        public String ImageAnalysis(IplImage image, Int32 method)
        {
            String      plateData = null;
            Double      plateWHratio;
            Plate       plate = null;
            List <Char> chars = null;


            try
            {
                plate = new Plate(image);
                // JIVAN
                //plate.SavePlateCopyImage(@"C:\temp\newImages\newImageAdaptativeThreshold1.png");
                plate.Normalize(method);

                plateWHratio = (Double)((Double)plate.GetWidth / (Double)plate.GetHeight);
                if (plateWHratio < Constants.MIN_PLATE_WIDTH_HEIGHT_RATIO ||
                    plateWHratio > Constants.MAX_PLATE_WIDTH_HEIGHT_RATIO)
                {
                    return(plateData);
                }

                // JIVAN
                //plate.SavePlateCopyImage(@"C:\temp\newImages\newImageAdaptativeThreshold2.png");
                chars = plate.GetChars();

                //  JIVAN

                /*Int32 count2 =0;
                 * foreach(Char ch in chars)
                 * {
                 *  ch.GetImage.SaveImage(String.Format(@"C:\temp\newImages\newBeforeNormImageCh{0}.png", count2));
                 *  ch.GetThresholdedImage.SaveImage(String.Format(@"C:\temp\newImages\newImageThresholdedCh{0}.png", count2++));
                 * }
                 */

                if (chars.Count < Constants.INTELLIGENCE_MIN_CHARS ||
                    chars.Count > Constants.INTELLIGENCE_MAX_CHARS)
                {
                    return(plateData);
                }

                if (plate.GetCharsWidthDispertion(chars) > Constants.INTELLIGENCE_MAX_CHAR_WIDTH_DISP)
                {
                    return(plateData);
                }

                RecognizedPlate recognizedPlate = new RecognizedPlate();
                //Int32 count =0;
                foreach (Char ch in chars)
                {
                    ch.Normalize();
                    // JIVAN
                    //ch.GetImage.SaveImage(String.Format(@"C:\temp\newImages\newNormImageCh{0}.png",count++));
                }

                Double averageHeight     = plate.GetAveragePieceHeight(chars);
                Double averageContrast   = plate.GetAveragePieceContrast(chars);
                Double averageBrightness = plate.GetAveragePieceBrightness(chars);
                Double averageHue        = plate.GetAveragePieceHue(chars);
                Double averageSaturation = plate.GetAveragePieceSaturation(chars);

                foreach (Char chr in chars)
                {
                    Double widthHeightRatio = (Double)(chr.PieceWidth);
                    widthHeightRatio /= (Double)(chr.PieceHeight);

                    if (widthHeightRatio < Constants.INTELLIGENCE_MIN_CHAR_WIDTH_HEIGHT_RATIO ||
                        widthHeightRatio > Constants.INTELLIGENCE_MAX_CHAR_WIDTH_HEIGHT_RATIO)
                    {
                        continue;
                    }


                    if ((chr.PositionInPlate.X1 < 2 ||
                         chr.PositionInPlate.X2 > plate.GetWidth - 1) &&
                        widthHeightRatio < 0.12)
                    {
                        continue;
                    }

                    Double contrastCost   = Math.Abs(chr.StatisticContrast - averageContrast);
                    Double brightnessCost = Math.Abs(chr.StatisticAverageBrightness - averageBrightness);
                    Double hueCost        = Math.Abs(chr.StatisticAverageHue - averageHue);
                    Double saturationCost = Math.Abs(chr.StatisticAverageSaturation - averageSaturation);
                    Double heightCost     = (chr.PieceHeight - averageHeight) / averageHeight;

                    if (brightnessCost > Constants.INTELLIGENCE_MAX_BRIGHTNESS_COST_DISPERSION)
                    {
                        continue;
                    }

                    if (contrastCost > Constants.INTELLIGENCE_MAX_CONTRAST_COST_DISPERSION)
                    {
                        continue;
                    }

                    if (hueCost > Constants.INTELLIGENCE_MAX_HUE_COST_DISPERSION)
                    {
                        continue;
                    }

                    if (saturationCost > Constants.INTELLIGENCE_MAX_SATURATION_COST_DISPERSION)
                    {
                        continue;
                    }

                    if (heightCost < -(Constants.INTELLIGENCE_MAX_HIGHT_COST_DISPERSION))
                    {
                        continue;
                    }

                    Double similarityCost = 0.0;
                    CharacterRecognizer.RecognizedChar rc = null;

                    rc             = chrRecog.Recognize(chr);
                    similarityCost = rc.GetPatterns()[0].GetCost;
                    if (similarityCost <= Constants.INTELLIGENCE_MAX_SIMILARITY_COST_DISPERSION)
                    {
                        recognizedPlate.AddChar(rc);
                    }
                }

                if (recognizedPlate.GetChars.Count < Constants.INTELLIGENCE_MIN_CHARS)
                {
                    return(plateData);
                }

                plateData = parser.Parse(recognizedPlate, Constants.INTELLIGENCE_SYNTAX_ANALYSIS_MODE);
            }
            finally
            {
                if (null != chars)
                {
                    foreach (Char ch in chars)
                    {
                        ch.ClearImage();
                    }
                }

                if (null != plate)
                {
                    plate.ClearImage();
                }
            }

            return(plateData);
        }
        public String Parse(RecognizedPlate recognizedPlate, Int32 syntaxAnalysisMode)
        {
            Int32 length = recognizedPlate.GetChars.Count;

            UnFlagAll();
            FlagEqualOrShorterLength(length);

            List <FinalPlate> finalPlates = new List <FinalPlate>();

            foreach (PlateForm form in plateForms)
            {
                if (!form.flagged)
                {
                    continue;
                }

                for (Int32 i = 0; i <= length - form.Length; i++)
                {
                    FinalPlate finalPlate = new FinalPlate();
                    for (Int32 ii = 0; ii < form.Length; ii++)
                    {
                        CharacterRecognizer.RecognizedChar rc = recognizedPlate.GetChar(ii + i);

                        if (form.GetPosition(ii).IsAllowed(rc.GetPattern(0).GetChar))
                        {
                            finalPlate.AddChar(rc.GetPattern(0).GetChar);
                        }
                        else
                        {
                            finalPlate.IncrementRequiredChanges();
                            for (Int32 x = 0; x < rc.GetPatterns().Count; x++)
                            {
                                if (form.GetPosition(ii).IsAllowed(rc.GetPattern(x).GetChar))
                                {
                                    CharacterRecognizer.RecognizedChar.RecognizedPattern rp = rc.GetPattern(x);
                                    finalPlate.AddRequiredChanges(rp.GetCost / 100.0);
                                    finalPlate.AddChar(rp.GetChar);
                                    break;
                                }
                            }
                        }
                    }

                    finalPlates.Add(finalPlate);
                }
            }

            if (finalPlates.Count == 0)
            {
                return(recognizedPlate.GetString());
            }

            Double minimalChanges = Double.PositiveInfinity;
            Int32  minimalIndex   = 0;

            for (Int32 i = 0; i < finalPlates.Count; i++)
            {
                if (finalPlates[i].RequiredChanges <= minimalChanges)
                {
                    minimalChanges = finalPlates[i].RequiredChanges;
                    minimalIndex   = i;
                }
            }

            String toReturn = recognizedPlate.GetString();

            if (finalPlates[minimalIndex].RequiredChanges <= 2)
            {
                toReturn = finalPlates[minimalIndex].GetPlate();
            }

            return(toReturn);
        }