2021年5月5日 星期三

License_Plate_Recognition_in_CSharp

 REF:

1)https://www.emgu.com/wiki/index.php/License_Plate_Recognition_in_CSharp

2)https://github.com/fasetto/license-plate-recognition/blob/master/src/LPlateRecognition.UI/ViewModels/MainViewModel.cs

Windows 10 x64 @ ASUS X450

Visual Studio 2019

建置程序

1) New FormApp

2) Nuget 

emgu.cv 3.4.3.3016

Tesseract.Net SDK 1.18.342

3) Add class (Ref:1)

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.OCR;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Emgu.Util;
using Patagames.Ocr;
using Patagames.Ocr.Enums;
//---###
using (UMat tmp = filteredPlate.Clone())
       {
          string plainText;
          using (var api = OcrApi.Create())
          {
            api.Init(Languages.English);
            api.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890");
            plainText = api.GetTextFromImage(tmp.Bitmap);
           }
           //_ocr.Recognize(tmp);
          //words = _ocr.GetCharacters();
                                //if (words.Length == 0) continue;
                                if (plainText.Length == 0) continue;
                                //for (int i = 0; i < words.Length; i++)
                                for (int i = 0; i < plainText.Length; i++)
                                {
                                    strBuilder.Append(plainText[i]);
                                }
                            }
//---###
4) Form1.cs
public Form1()
        {
            InitializeComponent();
            imageBox1.Image = new Image<Bgr, byte>(@"C:\Users\csjou\Downloads\license-plate2.jpg");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LicensePlateDetector LPD = new LicensePlateDetector("test");
            Mat m = new Image<Bgr, byte>(imageBox1.Image.Bitmap).Mat;
            List<IInputOutputArray> licensePlateImagesList = new List<IInputOutputArray>();
            List<IInputOutputArray> filteredLicensePlateImagesList = new List<IInputOutputArray>();
            List<RotatedRect> licenseBoxList = new List<RotatedRect>();
            List<string> words = LPD.DetectLicensePlate(
            m,
            licensePlateImagesList,
            filteredLicensePlateImagesList,
            licenseBoxList
            );
            this.Text = "";
            for (int i = 0; i < words.Count; i++)
                this.Text += words[i];
        }



emgu 轉換

 REF: http://ferris2014.blogspot.com/2016/06/opencv-emgu-mat-to-image-image-to-mat.html

Mat to image, image to Mat, image to imagebox, imagebox to image, Mat to imagebox, imagebox to Mat

1. Mat to image

_img = _mat.ToImage<bgr, Byte>();

2. image to Mat

_mat = _img.Mat;

3. image to imagebox

imagebox1.Image=_img;

4. imagebox to image

_img = new Image<bgr, byte>(imageBox1.Image.Bitmap)

5. Mat to imagebox

imagebox1.Image=_mat;

6. imagebox to Mat

(Step4 + Step2)

_img= new Image<bgr, byte>(imageBox1.Image.Bitmap).Mat;