private void frmMain_Load(object sender, EventArgs e)
 {
     VisionLab.InitVisionLib();
     lexicon     = null;
     blobMatcher = null;
     LoadFiles();
 }
        /*
         *  Description:
         *      Load the pattern matcher and scripts
         */
        private void LoadFiles()
        {
            try
            {
                ErrorTB.Text = "";
                if (chkUseScripts.Checked)
                {
                    chkUseLexicon.Checked = false;
                    chkUseLexicon.Enabled = false;
                }
                else
                {
                    chkUseLexicon.Enabled = true;
                }
                if (chkUseScripts.Checked)
                {
                    LicensePlateMatcherScript.Init();
                }
                String exePath = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                if (blobMatcher == null)
                {
                    blobMatcher = new BlobMatcher_Int32();
                }
                blobMatcher.ReadFromFile(exePath + "\\..\\..\\..\\..\\..\\..\\VL\\lic_fonts.pm");

                if (lexicon == null)
                {
                    lexicon = new ClassLexicon(blobMatcher.GetPatternNamesTab());
                }
            }
            catch (System.Exception ex)
            {
                ErrorTB.Text = ex.Message;
            }
        }
 internal static HandleRef getCPtr(BlobMatcher_Int32 obj)
 {
     return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr);
 }
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BlobMatcher_Int32 obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
        /*
         *  Description:
         *      Read the license plate
         *  Input:
         *      //Rectified license plate image containing six characters
         *      Int32Image labeledRectifiedPlateImage
         *  Output:
         *      //Result by the blob matcher
         *      ref LicensePlate result
         *  Return:
         *      //six characters found
         *      bool
         */
        public static bool MatchPlate(Int32Image binaryCharacterImage, BlobMatcher_Int32 matcher, ClassLexicon lexicon, ref LicensePlate result, ref LicensePlate lexiconResult)
        {
            try
            {
                //Check if 6 characters/blobs have been found and label image.
                if (VisionLab.LabelBlobs(binaryCharacterImage, Connected.EightConnected) != 6)
                {
                    return(false);
                }

                //Calculate dimensions and locations of all characters/blobs.
                vector_BlobAnalyse ba_vec = new vector_BlobAnalyse();
                ba_vec.Add(BlobAnalyse.BA_TopLeft);
                ba_vec.Add(BlobAnalyse.BA_Height);
                ba_vec.Add(BlobAnalyse.BA_Width);
                vector_Blob returnBlobs = new vector_Blob();
                VisionLab.BlobAnalysis(binaryCharacterImage, VisionLab.VectorToSet_BlobAnalyse(ba_vec), VisionLab.MaxPixel(binaryCharacterImage), returnBlobs, SortOrder.SortDown, BlobAnalyse.BA_TopLeft, UseXOrY.UseX);
                ba_vec.Dispose();
                Int32Image binaryCharacter = new Int32Image();

                //Create data structure for lexicon.
                vector_vector_LetterMatch match = new vector_vector_LetterMatch();

                //Process each character/blob.
                foreach (Blob b in returnBlobs)
                {
                    //Cut out character
                    VisionLab.ROI(binaryCharacterImage, binaryCharacter, b.TopLeft(), new HeightWidth(b.Height(), b.Width()));
                    //Convert ROI result to binary
                    VisionLab.ClipPixelValue(binaryCharacter, 0, 1);

                    //Calculate character's classification for all classes.
                    vector_PatternMatchResult returnMatches = new vector_PatternMatchResult();
                    float  conf = matcher.AllMatches(binaryCharacter, (float)-0.5, (float)0.5, returnMatches);
                    float  err  = returnMatches[0].error;
                    int    id   = returnMatches[0].id;
                    string chr  = matcher.PatternName(id);

                    //Fill datastructure for lexicon.
                    match.Add(VisionLabEx.PatternMatchResultToLetterMatch(returnMatches));

                    //Store best match in result
                    result.characters.Add(new LicenseCharacter(chr, err, conf));
                }

                //Validate match with lexicon.
                vector_int bestWord = new vector_int();
                lexiconResult.confidence = lexicon.FindBestWord(match, bestWord, Optimize.OptimizeForMinimum);
                for (int i = 0; i < bestWord.Count; i++)
                {
                    string character = matcher.PatternName(bestWord[i]);
                    //Store lexicon result
                    lexiconResult.characters.Add(new LicenseCharacter(character));
                }

                binaryCharacter.Dispose();
                returnBlobs.Dispose();
                match.Dispose();
                bestWord.Dispose();

                bool[] types = new bool[6];
                for (int i = 0; i < 6; i++)
                {
                    types[i] = '0' <= result.characters[i].character[0] && result.characters[i].character[0] <= '9';
                }
                if (types[0] && types[1] && !types[2] && !types[3] && !types[4] && !types[5])
                {
                    return(true);
                }
                if (!types[0] && !types[1] && types[2] && types[3] && !types[4] && !types[5])
                {
                    return(true);
                }
                if (!types[0] && !types[1] && !types[2] && !types[3] && types[4] && types[5])
                {
                    return(true);
                }
                if (types[0] && !types[1] && !types[2] && !types[3] && types[4] && types[5])
                {
                    return(true);
                }
                if (types[0] && types[1] && !types[2] && !types[3] && !types[4] && types[5])
                {
                    return(true);
                }
                return(false);
            }
            catch (System.Exception ex)
            {
                throw new Exception("MatchPlate: " + ex.Message);
            }
        }
 internal static HandleRef getCPtr(BlobMatcher_Int32 obj) {
   return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }