Professional Documents
Culture Documents
Lampiran Kode Program PDF
Lampiran Kode Program PDF
namespace LicensePlateRecognition
{
public partial class LicensePlateRecognitionForm : Form
{
private LicensePlateDetector _licensePlateDetector;
public LicensePlateRecognitionForm()
{
InitializeComponent();
_licensePlateDetector = new LicensePlateDetector("", 170,
255);
panel1.Controls.Clear();
75
Point startPoint = new Point(10, 10);
for (int i = 0; i < words.Count; i++)
{
if (words[i].Length >= 5)
{
int nilai = words[i].Length - 1;
if (nilai >= 11) nilai = 10;
words[i] = words[i].Substring(0, nilai);
AddLabelAndImage(
ref startPoint,
String.Format("{0}", words[i]),
licensePlateImagesList[i].ConcateVertical(filteredLicensePlateImag
esList[i]));
image.Draw(licenseBoxList[i], new
Bgr(Color.Blue), 2);
}
}
imageBox1.Image = image;
}
if (result == DialogResult.OK)
{
Image<Bgr, Byte> img;
try
{
76
img = new Image<Bgr,
byte>(openFileDialog1.FileName);
}
catch
{
MessageBox.Show(String.Format("File Tidak Valid:
{0}", openFileDialog1.FileName));
return;
}
img_global = img;
}
}
77
Lampiran 2. Kode Program Class LicensePlateDetector
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using Emgu.CV;
using Emgu.CV.OCR;
using Emgu.CV.Structure;
using Emgu.Util;
namespace LicensePlateRecognition
{
public class LicensePlateDetector : DisposableObject
{
private Tesseract _ocr;
private float gray_thresh;
private float gray_max;
public LicensePlateDetector(String dataPath, float th1, float
th2){
// membuat OCR engine
_ocr = new Tesseract("tessdata", "eng",
Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);
_ocr.SetVariable("tessedit_char_whitelist",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
this.gray_thresh = th1;
this.gray_max = th2;
}
try
{
// channel 1 untuk saturasi kurang dari 40. mask
untuk hitam ataupun putih
78
channels[1]._ThresholdBinaryInv(new Gray(40), new
Gray(255));
// channels 2 untuk piksel terang.
channels[2]._ThresholdBinary(new Gray(150), new
Gray(255));
CvInvoke.cvAnd(channels[1], channels[2],
channels[0], IntPtr.Zero);
}
finally
{
channels[1].Dispose();
channels[2].Dispose();
}
return channels[0];
}
}
Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE,
stor);
FindLicensePlate(kontur, gray, canny,
licensePlateImagesList, filteredLicensePlateImagesList,
detectedLicensePlateRegionList, licenses);
}
return licenses;
}
// mendapatkan karakter dalam area plat nomor
// membaca karakter dari kiri ke kanan
private static int GetNumberOfChildren(Contour<Point>
contours)
79
{
Contour<Point> child = contours.VNext;
if (child == null) return 0;
int count = 0;
while (child != null)
{
count++;
child = child.HNext;
}
return count;
}
80
float tmp = box.size.Width;
box.size.Width = box.size.Height;
box.size.Height = tmp;
box.angle -= 90.0f;
}
// Menentukan ukuran rasio plat nomor
double whRatio = (double)box.size.Width /
box.size.Height;
if (!(2.0 < whRatio && whRatio < 4.0))
{
Contour<Point> child = contours.VNext;
if (child != null)
FindLicensePlate(child, gray, canny,
licensePlateImagesList, filteredLicensePlateImagesList,
detectedLicensePlateRegionList, licenses);
continue;
}
if (words.Length == 0) continue;
81
filteredLicensePlateImagesList.Add(filteredPlate);
detectedLicensePlateRegionList.Add(box);
}
}
}
}
//penyaring / filter plat nomor
private static Image<Gray, Byte> FilterPlate(Image<Gray,
Byte> plate, float gray_threshx, float gray_maxx)
{
/*>>>>*/
float nilai1, nilai2;
nilai1 = gray_threshx;
nilai2 = gray_maxx;
nilai1 = (float)Convert.ToInt32(nilai1);
nilai2 = (float)Convert.ToInt32(nilai2);
Image<Gray, Byte> thresh = plate.ThresholdBinaryInv(new
Gray(nilai1), new Gray(nilai2));
using (Image<Gray, Byte> plateMask = new Image<Gray,
byte>(plate.Size))
using (Image<Gray, Byte> plateCanny = plate.Canny(120,
50))
using (MemStorage stor = new MemStorage())
{
plateMask.SetValue(255.0);
for (
Contour<Point> contours = plateCanny.FindContours(
Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL,
stor);
contours != null;
contours = contours.HNext)
{
Rectangle rect = contours.BoundingRectangle;
if (rect.Height > (plate.Height >> 1))
{
rect.X -= 1; rect.Y -= 1; rect.Width += 2;
rect.Height += 2;
rect.Intersect(plate.ROI);
plateMask.Draw(rect, new Gray(0.0), -1);
}
}
thresh.SetValue(0, plateMask);
}
// erosi dan dilasi menggunakan struktur elemen persegi 3x3
thresh._Erode(1);
thresh._Dilate(1);
return thresh;
82
}
protected override void DisposeObject()
{
_ocr.Dispose();
}
}
}
83