/*
         *  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;
            }
        }
        /*
         *  Description:
         *      Match a single image given the filename
         *  Input:
         *      //Filename of the image
         *      String Filename
         *      //Add the results to  the listboxes?
         *      bool add
         *      //If true, the VisionLab scripts will be called by LicensePlateMatcher. If false, the wrapped functions are called by LicensePlateMatcherScript
         *      bool useScripts
         *  Output:
         */
        private void MatchImage(String Filename, bool add, bool useScripts)
        {
            ErrorTB.Text     = "";
            lblExpected.Text = Filename.Substring(0, 6);

            //*************************************//
            //** load and display original image **//
            //*************************************//
            Bitmap bm;

            bm = new Bitmap(Filename);
            RGB888Image plateImage = VisionLabEx.BitmapToJL(bm);

            VisionLabEx.DisplayImage(plateImage, imgOrig);

            //****************//
            //** Find plate **//
            //****************//
            Int32Image binaryPlateImage = new Int32Image();
            bool       success;

            if (useScripts)
            {
                success = LicensePlateMatcherScript.FindPlate(plateImage, ref binaryPlateImage);
            }
            else
            {
                success = LicensePlateMatcher.FindPlate(plateImage, ref binaryPlateImage);
            }
            if (!success)
            {
                lblLexiconResult.Text = "";
                if (add)
                {
                    lstFindPlateErr.Items.Add(Filename);
                    lblFindPlateErrCount.Text = lstFindPlateErr.Items.Count.ToString();
                }
                ClearResultLabels();
                ClearPictureBox(imgPlateBin);
                ClearPictureBox(imgRectifiedPlate);
                return;
            }
            VisionLabEx.DisplayImage(binaryPlateImage, imgPlateBin, true, true);

            //**
            //** Enable this to display blob measurements to debug output
            //**
            DisplayBlobs(binaryPlateImage);

            //*******************//
            //** Rectify plate **//
            //*******************//
            Int32Image binaryCharacterImage = new Int32Image();

            if (useScripts)
            {
                success = LicensePlateMatcherScript.FindCharacters(plateImage, binaryPlateImage, ref binaryCharacterImage);
            }
            else
            {
                success = LicensePlateMatcher.FindCharacters(plateImage, binaryPlateImage, ref binaryCharacterImage);
            }
            if (!success)
            {
                if (imgRectifiedPlate.Image != null)
                {
                    imgRectifiedPlate.Image.Dispose();
                }
                imgRectifiedPlate.Image = null;
                lblLexiconResult.Text   = "";
                if (add)
                {
                    lstRectifyPlateErr.Items.Add(Filename);
                    lblRectfyPlateErrCount.Text = lstRectifyPlateErr.Items.Count.ToString();
                }
                ClearResultLabels();
                ClearPictureBox(imgRectifiedPlate);
                return;
            }
            VisionLabEx.DisplayImage(binaryCharacterImage, imgRectifiedPlate, true, true);
            //**
            //** Enable this to display blob measurements to debug output
            //**
            DisplayBlobs(binaryCharacterImage);

            //*****************//
            //** Match Plate **//
            //*****************//
            LicensePlate result        = new LicensePlate();
            LicensePlate lexiconResult = new LicensePlate();

            if (useScripts)
            {
                success = LicensePlateMatcherScript.MatchPlate(binaryCharacterImage, ref result);
            }
            else
            {
                success = LicensePlateMatcher.MatchPlate(binaryCharacterImage, blobMatcher, lexicon, ref result, ref lexiconResult);
            }
            if (!success)
            {
                lblLexiconResult.Text = "";
                if (add)
                {
                    lstMatchPlateErr.Items.Add(Filename);
                    lblMatchPlateErrCount.Text = lstMatchPlateErr.Items.Count.ToString();
                }
                ClearResultLabels();
                return;
            }

            //*********************//
            //** Process results **//
            //*********************//
            ProcessResults(result, lexiconResult, Filename, (double)nupConfidence.Value / 100, add);

            bm.Dispose();
            plateImage.Dispose();
            binaryPlateImage.Dispose();
            binaryCharacterImage.Dispose();

            //Force a garbage collect to prevens malloc errors from unmanaged code.
            GC.Collect();
        }