实现一个车牌识别的项目
Halcon部分的代码
***** 读取图片
dev_close_window ()
read_image (Image, '../素材/chepai18.jpg')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_display (Image)
***** 处理图片-定位车牌
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Region, 80, 100)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 8870.97, 10000)
***** 处理图片- 转正图片
shape_trans (SelectedRegions, RegionTrans, 'rectangle2')
area_center (RegionTrans, Area, Row, Column)
orientation_region (RegionTrans, Phi)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(0), HomMat2D)
hom_mat2d_slant (HomMat2D, rad(15), 'x', Column, Row, HomMat2DSlant)
affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2DSlant, 'nearest_neighbor')
affine_trans_image (Image, ImageAffineTrans, HomMat2DSlant, 'constant', 'false')
reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced)
*************** 开始识别之车牌图片预处理,带中文字符分割
rgb1_to_gray (ImageReduced, GrayImage1)
threshold (GrayImage1, Regions, 176, 242)
closing_circle (Regions, RegionClosing, 3.5)
opening_circle (Regions, RegionOpening, 1.7)
connection (RegionOpening, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 20, 600)
sort_region (SelectedRegions1, SortedRegions, 'character', 'true', 'column')
*************** 组装苏字区域
select_obj (SortedRegions, ObjectSelected1, 1)
select_obj (SortedRegions, ObjectSelected2, 2)
select_obj (SortedRegions, ObjectSelected3, 3)
union2 (ObjectSelected1, ObjectSelected2, RegionUnion)
union2 (RegionUnion, ObjectSelected3, RegionUnion1)
select_obj (SortedRegions, ObjectSelected4, 4)
select_obj (SortedRegions, ObjectSelected5, 5)
select_obj (SortedRegions, ObjectSelected6, 6)
select_obj (SortedRegions, ObjectSelected7, 7)
select_obj (SortedRegions, ObjectSelected8, 8)
select_obj (SortedRegions, ObjectSelected9, 9)
***************** 把所有区域组成一个对象
gen_empty_obj (NumberObject)
concat_obj (NumberObject, RegionUnion1, NumberObject)
concat_obj (NumberObject, ObjectSelected4, NumberObject)
concat_obj (NumberObject, ObjectSelected5, NumberObject)
concat_obj (NumberObject, ObjectSelected6, NumberObject)
concat_obj (NumberObject, ObjectSelected7, NumberObject)
concat_obj (NumberObject, ObjectSelected8, NumberObject)
concat_obj (NumberObject, ObjectSelected9, NumberObject)
**************** 创建训练文件
TrainFile:='./Charactor.trf'
Words:=['苏','E','C','6','2','N','8']
** 完成图像与字符训练对应关系
write_ocr_trainf (NumberObject, GrayImage1, Words, TrainFile)
** 读取训练文件
read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)
** 创建一个分类识别器
create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)
** 训练分类识别器
trainf_ocr_class_mlp (OCRHandle, TrainFile, 200, 1, 0.01, Error, ErrorLog)
** 保存分类识别文件
write_ocr_class_mlp (OCRHandle, './Charactor.omc')
*********************** 基于训练omc文件开始识别带中文车牌**************
read_ocr_class_mlp ('./Charactor.omc', OCRHandle1)
do_ocr_multi_class_mlp (NumberObject, GrayImage1, OCRHandle1, Class, Confidence)
dev_clear_window ()
dev_display (Image)
dev_set_color ('red')
set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
for Index := 0 to |Class|-1 by 1
set_tposition (WindowHandle,30, 120+36*Index)
write_string (WindowHandle, Class[Index])
endfor
Winform部分的代码
using System;
using System.Windows.Forms;
using VLPR;
using HalconDotNet;
namespace halcon
{
public partial class Form1 : Form
{
private HDevelopExport HD = new HDevelopExport();
string ImagePath; // 图片路径
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnLoadImage_Click(object sender, EventArgs e)
{
// 设置过滤文件条件
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "JPG文件|*.jpg";
// 设置关闭对话回到当前目录
openFileDialog1.RestoreDirectory = true;
// 打开对话框
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
ImagePath = openFileDialog1.FileName;
// 读取图片并显示到该窗口上面
HD.ReadImage(hWindowControl2.HalconWindow, hWindowControl2.Width, hWindowControl2.Height, ImagePath);
}
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
var cardNumber = HD.RecognitionImage();
this.textBox1.Text = this.textBox1.Text + cardNumber + "\r\n";
}
}
}
winform中添加2个button,1个textbox,1个HWindowControl
实现效果