代码功能:识别车牌字符,C#开发界面,结果显示在界面中
关键算子:smallest_rectangle2 ()获取最小外接矩形
select_shape()按照形状筛选区域
read_ocr_class_mlp()读取软件自带的训练模板
do_ocr_multi_class_mlp ()按照模板识别字符,**注意!**必须和模板的类型一致,比如这里都是_mlp类型
///手动分隔符
dev_close_window ()
dev_open_file_dialog ('read_image', 'default', 'default', Selection)
read_image (Image, Selection)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_part(WindowHandle,0,0,Height-1,Width-1)
dev_display (Image)
*转换为r/g/b/h/s/v通道图,选择对比度最大的通道
decompose3 (Image, r, g, b)
trans_from_rgb (r, g, b, h, s, v, 'hsv')
*阈值处理
threshold (s, Region, 120, 255)
fill_up (Region, RegionFillUp)
*找出区域所在的最小外接矩形
smallest_rectangle2 (RegionFillUp, Row, Column, Phi, Length1, Length2)
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
*区域所在的中心坐标及角度
vector_angle_to_rigid (Row, Column, Phi, Row, Column, -0.01, HomMat2D)
*将区域和图片旋转到水平位置
affine_trans_region (Rectangle, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
affine_trans_image (s, ImageAffineTrans, HomMat2D, 'constant', 'false')
*区域做差,得到目标区域
reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced)
threshold (ImageReduced, Region1, 1, 50)
closing_circle (Region1, RegionClosing, 1.8)
*构建连通域
connection (Region1, ConnectedRegions)
*按照面积大小筛选出目标区域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 1000, 99999)
count_obj (SelectedRegions, Number)
if (Number!=6)
stop()
endif
*按照列排序
sort_region (SelectedRegions, SortedRegions, 'first_point', 'true', 'column')
*按照自带的训练模板训练字符
read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)
*invert_image (ImageReduced, ImageInvert)
*识别字符,结果存放到变量Class中
do_ocr_multi_class_mlp (SortedRegions, ImageReduced, OCRHandle, Class, Confidence)
*创建文档,存放字符
open_file ('fff.txt', 'output', FileHandle)
fwrite_string (FileHandle, Class)
close_file (FileHandle)`
//
// File generated by HDevelop for HALCON/.NET (C#) Version 18.05
//
// This file is intended to be used with the HDevelopTemplate or
// HDevelopTemplateWPF projects located under %HALCONEXAMPLES%\c#
using System;
using System.Windows.Forms;
using HalconDotNet;
using System.Text;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using identification;
public partial class HDevelopExport
{
// Form1 new_form = new Form1();
public HTuple hv_ExpDefaultWinHandle;
//public Form form2 = new Form();
public void HDevelopStop()
{
MessageBox.Show("Press button to continue", "Program stop");
}
// Procedures
// Chapter: Graphics / Text
// Short Description: This procedure writes a text message.
public void disp_message (HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
{
// Local iconic variables
// Local control variables
HTuple hv_GenParamName = new HTuple(), hv_GenParamValue = new HTuple();
HTuple hv_Color_COPY_INP_TMP = new HTuple(hv_Color);
HTuple hv_Column_COPY_INP_TMP = new HTuple(hv_Column);
HTuple hv_CoordSystem_COPY_INP_TMP = new HTuple(hv_CoordSystem);
HTuple hv_Row_COPY_INP_TMP = new HTuple(hv_Row);
// Initialize local and output iconic variables
//This procedure displays text in a graphics window.
//
//Input parameters:
//WindowHandle: The WindowHandle of the graphics window, where
// the message should be displayed
//String: A tuple of strings containing the text message to be displayed
//CoordSystem: If set to 'window', the text position is given
// with respect to the window coordinate system.
// If set to 'image', image coordinates are used.
// (This may be useful in zoomed images.)
//Row: The row coordinate of the desired text position
// A tuple of values is allowed to display text at different
// positions.
//Column: The column coordinate of the desired text position
// A tuple of values is allowed to display text at different
// positions.
//Color: defines the color of the text as string.
// If set to [], '' or 'auto' the currently set color is used.
// If a tuple of strings is passed, the colors are used cyclically...
// - if |Row| == |Column| == 1: for each new textline
// = else for each text position.
//Box: If Box[0] is set to 'true', the text is written within an orange box.
// If set to' false', no box is displayed.
// If set to a color string (e.g. 'white', '#FF00CC', etc.),
// the text is written in a box of that color.
// An optional second value for Box (Box[1]) controls if a shadow is displayed:
// 'true' -> display a shadow in a default color
// 'false' -> display no shadow
// otherwise -> use given string as color string for the shadow color
//
//It is possible to display multiple text strings in a single call.
//In this case, some restrictions apply:
//- Multiple text positions can be defined by specifying a tuple
// with multiple Row and/or Column coordinates, i.e.:
// - |Row| == n, |Column| == n
// - |Row| == n, |Column| == 1
// - |Row| == 1, |Column| == n
//- If |Row| == |Column| == 1,
// each element of String is display in a new textline.
//- If multiple positions or specified, the number of Strings
// must match the number of positions, i.e.:
// - Either |String| == n (each string is displayed at the
// corresponding position),
// - or |String| == 1 (The string is displayed n times).
//
//
//Convert the parameters for disp_text.
if ((int)((new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(new HTuple())))) != 0)
{
hv_Color_COPY_INP_TMP.Dispose();
hv_Column_COPY_INP_TMP.Dispose();
hv_CoordSystem_COPY_INP_TMP.Dispose();
hv_Row_COPY_INP_TMP.Dispose();
hv_GenParamName.Dispose();
hv_GenParamValue.Dispose();
return;
}
if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Row_COPY_INP_TMP.Dispose();
hv_Row_COPY_INP_TMP = 12;
}
if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Column_COPY_INP_TMP.Dispose();
hv_Column_COPY_INP_TMP = 12;
}
//
//Convert the parameter Box to generic parameters.
hv_GenParamName.Dispose();
hv_GenParamName = new HTuple();
hv_GenParamValue.Dispose();
hv_GenParamValue = new HTuple();
if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(0))) != 0)
{
if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleEqual("false"))) != 0)
{
//Display no box
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"box");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
"false");
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
else if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleNotEqual("true"))) != 0)
{
//Set a color other than the default.
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"box_color");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
hv_Box.TupleSelect(0));
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
}
if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(1))) != 0)
{
if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleEqual("false"))) != 0)
{
//Display no shadow.
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"shadow");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
"false");
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
else if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleNotEqual("true"))) != 0)
{
//Set a shadow color other than the default.
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
"shadow_color");
hv_GenParamName.Dispose();
hv_GenParamName = ExpTmpLocalVar_GenParamName;
}
}
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
{
HTuple
ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
hv_Box.TupleSelect(1));
hv_GenParamValue.Dispose();
hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
}
}
}
}
//Restore default CoordSystem behavior.
if ((int)(new HTuple(hv_CoordSystem_COPY_INP_TMP.TupleNotEqual("window"))) != 0)
{
hv_CoordSystem_COPY_INP_TMP.Dispose();
hv_CoordSystem_COPY_INP_TMP = "image";
}
//
if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(""))) != 0)
{
//disp_text does not accept an empty string for Color.
hv_Color_COPY_INP_TMP.Dispose();
hv_Color_COPY_INP_TMP = new HTuple();
}
//
HOperatorSet.DispText(hv_ExpDefaultWinHandle, hv_String, hv_CoordSystem_COPY_INP_TMP,
hv_Row_COPY_INP_TMP, hv_Column_COPY_INP_TMP, hv_Color_COPY_INP_TMP, hv_GenParamName,
hv_GenParamValue);
hv_Color_COPY_INP_TMP.Dispose();
hv_Column_COPY_INP_TMP.Dispose();
hv_CoordSystem_COPY_INP_TMP.Dispose();
hv_Row_COPY_INP_TMP.Dispose();
hv_GenParamName.Dispose();
hv_GenParamValue.Dispose();
return;
}
// Main procedure
private string action(string name,Form1 my_form)
{
// Local iconic variables
HObject ho_Image, ho_R, ho_G, ho_B, ho_H, ho_S;
HObject ho_V, ho_Region, ho_RegionFillUp, ho_Rectangle;
HObject ho_RegionAffineTrans, ho_ImageAffineTrans, ho_ImageReduced;
HObject ho_Region1, ho_ConnectedRegions, ho_SelectedRegions;
HObject ho_SortedRegions, ho_ImageInvert;
// Local control variables
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
HTuple hv_WindowHandle = new HTuple(), hv_Row = new HTuple();
HTuple hv_Column = new HTuple(), hv_Phi = new HTuple();
HTuple hv_Length1 = new HTuple(), hv_Length2 = new HTuple();
HTuple hv_HomMat2D = new HTuple(), hv_Number = new HTuple();
HTuple hv_OCRHandle = new HTuple(), hv_Class = new HTuple();
HTuple hv_Confidence = new HTuple(), hv_FileHandle = new HTuple();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.GenEmptyObj(out ho_R);
HOperatorSet.GenEmptyObj(out ho_G);
HOperatorSet.GenEmptyObj(out ho_B);
HOperatorSet.GenEmptyObj(out ho_H);
HOperatorSet.GenEmptyObj(out ho_S);
HOperatorSet.GenEmptyObj(out ho_V);
HOperatorSet.GenEmptyObj(out ho_Region);
HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
HOperatorSet.GenEmptyObj(out ho_Rectangle);
HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans);
HOperatorSet.GenEmptyObj(out ho_ImageAffineTrans);
HOperatorSet.GenEmptyObj(out ho_ImageReduced);
HOperatorSet.GenEmptyObj(out ho_Region1);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
HOperatorSet.GenEmptyObj(out ho_SortedRegions);
HOperatorSet.GenEmptyObj(out ho_ImageInvert);
//dev_close_window(...);
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, name);
hv_Width.Dispose();hv_Height.Dispose();
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
my_form.textBox4.Text = hv_Width.ToString();
my_form.textBox5.Text = hv_Height.ToString();
//TextBox textbox5 = new TextBox();
//textbox5.Text = hv_Width;
//dev_open_window(...);
HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_Height - 1,hv_Width -1);
HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
ho_R.Dispose();ho_G.Dispose();ho_B.Dispose();
HOperatorSet.Decompose3(ho_Image, out ho_R, out ho_G, out ho_B);
ho_H.Dispose();ho_S.Dispose();ho_V.Dispose();
HOperatorSet.TransFromRgb(ho_R, ho_G, ho_B, out ho_H, out ho_S, out ho_V, "hsv");
ho_Region.Dispose();
HOperatorSet.Threshold(ho_S, out ho_Region, 128, 255);
ho_RegionFillUp.Dispose();
HOperatorSet.FillUp(ho_Region, out ho_RegionFillUp);
hv_Row.Dispose();hv_Column.Dispose();hv_Phi.Dispose();hv_Length1.Dispose();hv_Length2.Dispose();
HOperatorSet.SmallestRectangle2(ho_RegionFillUp, out hv_Row, out hv_Column, out hv_Phi,
out hv_Length1, out hv_Length2);
ho_Rectangle.Dispose();
HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row, hv_Column, hv_Phi, hv_Length1,
hv_Length2);
hv_HomMat2D.Dispose();
HOperatorSet.VectorAngleToRigid(hv_Row, hv_Column, hv_Phi, hv_Row, hv_Column,
0, out hv_HomMat2D);
ho_RegionAffineTrans.Dispose();
HOperatorSet.AffineTransRegion(ho_Rectangle, out ho_RegionAffineTrans, hv_HomMat2D,
"nearest_neighbor");
ho_ImageAffineTrans.Dispose();
HOperatorSet.AffineTransImage(ho_R, out ho_ImageAffineTrans, hv_HomMat2D, "constant",
"false");
//图像裁剪
ho_ImageReduced.Dispose();
HOperatorSet.ReduceDomain(ho_ImageAffineTrans, ho_RegionAffineTrans, out ho_ImageReduced
);
//提取白色区域
ho_Region1.Dispose();
HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 100, 255);
ho_ConnectedRegions.Dispose();
HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions);
ho_SelectedRegions.Dispose();
HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, (new HTuple("rect2_len1")).TupleConcat(
"rect2_len2"), "and", (new HTuple(45)).TupleConcat(20), (new HTuple(55)).TupleConcat(
30));
hv_Number.Dispose();
HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number);
if ((int)(new HTuple(hv_Number.TupleNotEqual(6))) != 0)
{
HDevelopStop();
}
//
ho_SortedRegions.Dispose();
HOperatorSet.SortRegion(ho_SelectedRegions, out ho_SortedRegions, "first_point",
"true", "column");
//读取ocr
hv_OCRHandle.Dispose();
HOperatorSet.ReadOcrClassMlp("Industrial_0-9A-Z_NoRej.omc", out hv_OCRHandle);
//图像反转
ho_ImageInvert.Dispose();
HOperatorSet.InvertImage(ho_ImageReduced, out ho_ImageInvert);
hv_Class.Dispose();hv_Confidence.Dispose();
HOperatorSet.DoOcrMultiClassMlp(ho_SortedRegions, ho_ImageInvert, hv_OCRHandle,
out hv_Class, out hv_Confidence);
string str = "";string str2 = "";
for (int i = 0; i < hv_Class.Length; i++)
{
str += hv_Class.TupleSelect(i);
str2 += hv_Confidence.TupleSelect(i);
}
my_form.textBox2.Text = str2;
//disp_message(hv_ExpDefaultWinHandle, hv_Class, "window", 0, 0, "black", "true");
ho_Image.Dispose();
ho_R.Dispose();
ho_G.Dispose();
ho_B.Dispose();
ho_H.Dispose();
ho_S.Dispose();
ho_V.Dispose();
ho_Region.Dispose();
ho_RegionFillUp.Dispose();
ho_Rectangle.Dispose();
ho_RegionAffineTrans.Dispose();
ho_ImageAffineTrans.Dispose();
ho_ImageReduced.Dispose();
ho_Region1.Dispose();
ho_ConnectedRegions.Dispose();
ho_SelectedRegions.Dispose();
ho_SortedRegions.Dispose();
ho_ImageInvert.Dispose();
hv_Width.Dispose();
hv_Height.Dispose();
hv_WindowHandle.Dispose();
hv_Row.Dispose();
hv_Column.Dispose();
hv_Phi.Dispose();
hv_Length1.Dispose();
hv_Length2.Dispose();
hv_HomMat2D.Dispose();
hv_Number.Dispose();
hv_OCRHandle.Dispose();
hv_Class.Dispose();
hv_Confidence.Dispose();
hv_FileHandle.Dispose();
return (str);
}
public void InitHalcon()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("width", 512);
HOperatorSet.SetSystem("height", 512);
}
//public static void change(TextBox a, TextBox b)
//{
// a.Text = he_width;
//}
public string RunHalcon(HTuple Window,Form1 form,string name)
{
hv_ExpDefaultWinHandle = Window;
action(name,form);
return (action(name,form));
}
}
Form类代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using System.IO;
namespace identification
{
public partial class Form1 : Form
{
HDevelopExport HD = new HDevelopExport();
public static Form1 my_form;
//HObject ho_Image; HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
my_form = this;
}
private void Read_image_Click(object sender, EventArgs e)
{
string fileName = string.Empty; //文件名
OpenFileDialog dlg = new OpenFileDialog();//打开文件
dlg.DefaultExt = "png";
dlg.Filter = "*.jpg|*.png";
if (dlg.ShowDialog() == DialogResult.OK)
fileName = dlg.FileName;
if (fileName == null)
return;
string lisence=HD.RunHalcon(hWindowControl1.HalconWindow,my_form,fileName);
textBox1.Text = lisence;
}
}
}