《车牌识别emgucv》由会员分享,可在线阅读,更多相关《车牌识别emgucv(5页珍藏版)》请在金锄头文库上搜索。
1、using System;using System.Collections.Generic;using System.Text;using System.Drawing;using Emgu.Util;using Emgu.CV;using Emgu.CV.Structure;using tessnet2;using System.Diagnostics; namespace LicensePlateRecognition / / A license plate detector / public class LicensePlateDetector : DisposableObject pr
2、ivate Tesseract _ocr; / / Create a license plate detector / public LicensePlateDetector() /create OCR _ocr = new Tesseract(); /You can download more language definition data from /Languages supported includes: /Dutch, Spanish, German, Italian, French and English _ocr.Init(eng, false); / / Detect lic
3、ense plate from the given image / / The image to search license plate from / A list of images where the detected license plate region is stored / A list of images where the detected license plate region with noise removed is stored / A list where the region of license plate, defined by an MCvBox2D i
4、s stored / The list of words for each license plate public ListList DetectLicensePlate(Image img, ListImage licensePlateList, ListImage filteredLicensePlateList, List boxList) /Stopwatch w = Stopwatch.StartNew(); ListList licenses = new ListList(); using (Image gray = img.Convert() using (Image cann
5、y = new Image(gray.Size) using (MemStorage stor = new MemStorage() CvInvoke.cvCanny(gray, canny, 100, 50, 3); Contour contours = canny.FindContours( Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, stor); FindLicensePlate(contours, gray, canny, licens
6、ePlateList, filteredLicensePlateList, boxList, licenses); /w.Stop(); return licenses; private void FindLicensePlate( Contour contours, Image gray, Image canny, ListImage licensePlateList, ListImage filteredLicensePlateList, List boxList, ListList licenses) for (; contours != null; contours = contour
7、s.HNext) Contour approxContour = contours.ApproxPoly(contours.Perimeter * 0.05, contours.Storage); if (approxContour.Area 100 & approxContour.Total = 4) /img.Draw(contours, new Bgr(Color.Red), 1); if (!IsParallelogram(approxContour.ToArray() Contour child = contours.VNext; if (child != null) FindLic
8、ensePlate(child, gray, canny, licensePlateList, filteredLicensePlateList, boxList, licenses); continue; MCvBox2D box = approxContour.GetMinAreaRect(); double whRatio = (double)box.size.Width / box.size.Height; if (!(3.0 whRatio & whRatio 8.0) Contour child = contours.VNext; if (child != null) FindLi
9、censePlate(child, gray, canny, licensePlateList, filteredLicensePlateList, boxList, licenses); continue; Image plate = gray.Copy(box); Image filteredPlate = FilterPlate(plate); List words; using (Bitmap bmp = filteredPlate.Bitmap) words = _ocr.DoOCR(bmp, filteredPlate.ROI); licenses.Add(words); lice
10、nsePlateList.Add(plate); filteredLicensePlateList.Add(filteredPlate); boxList.Add(box); / / Check if the four points forms a parallelogram / / The four points that defines a polygon / True if the four points defines a parallelogram private static bool IsParallelogram(Point pts) LineSegment2D edges = PointCollection.PolyLine(pts, true); double diff1 = Math.Abs(edges0.Length - edges2.Length); double diff2 = Math.Abs(edges1.Length - edges3.Length); if (diff1 / edges0.Length = 0.05 & diff1 / edges2.Length = 0.05