private void btn_extract_Click(object sender, EventArgs e) { if (hDisplay1.GetSearchRegions().Count == 0) { MessageBox.Show("请先添加搜索区域"); return; } HTuple hv_Number; HObject _roi = hDisplay1.GetSearchRegions().ElementAt(0); HObject _region = new HObject(); HObject _imagereduced = new HObject(); HObject ho_ObjectSelected = new HObject(); HOperatorSet.ReduceDomain(m_image, _roi, out _imagereduced); HOperatorSet.Threshold(_imagereduced, out _region, m_OcrParam.GrayMin, m_OcrParam.GrayMax); HOperatorSet.Connection(_region, out _region); HOperatorSet.SelectShape(_region, out _region, "area", "and", m_OcrParam.AreaMin, m_OcrParam.AreaMax); HOperatorSet.CountObj(_region, out hv_Number); HTuple hv_Index = new HTuple(); List <RegionX> _listregionx = new List <RegionX>(); for (hv_Index = 1; hv_Index.Continue(hv_Number, 1); hv_Index = hv_Index.TupleAdd(1)) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(_region, out ho_ObjectSelected, hv_Index); RegionX _hregion = new RegionX(ho_ObjectSelected.CopyObj(1, -1), "green"); _listregionx.Add(_hregion); } DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, _listregionx, null); }
OcrTrainParam m_TrainParam; //训练ocr的参数类 public OcrControl() { InitializeComponent(); AddEvent(); m_OcrParam = new RegionParam(); m_TrainParam = new OcrTrainParam(); DelegateUIControl.GetInstance().FormSetHDisplay = this.hDisplay1; DelegateUIControl.GetInstance().txt_CharsTrainf = this.txt_CharsTrainf; }
///手动加载图片 private void btn_LoadImage_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "|*.bmp;*.jpeg"; if (ofd.ShowDialog() == DialogResult.OK) { string path = ofd.FileName; HOperatorSet.ReadImage(out m_image, path); // HOperatorSet.InvertImage(m_image, out m_image); DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, null, null); } }
/// <summary> /// 手动添加ocr模板文件 /// </summary> /// <param name="path">保存有ocr模板文件的文件夹</param> public void LoadCharacterLibrary(string path) { if (!Directory.Exists(path)) { throw new Exception(path + "路径不存在"); } string[] filenames = Directory.GetFiles(path); if (filenames.Length == 0) { throw new Exception(path + "文件夹为空"); } string omcfile = string.Empty; string trffile = string.Empty; foreach (string member in filenames) { if (member.EndsWith(".omc")) { omcfile = member; } else if (member.EndsWith(".trf")) { trffile = member; } } if (omcfile == null) { throw new Exception(path + "路径下缺少.omc文件"); } if (trffile == null) { throw new Exception(path + "路径下缺少.trf文件"); } try { HOperatorSet.ReadOcrClassMlp(omcfile, out m_ocrhandle); HOperatorSet.ReadOcrTrainfNames(trffile, out HTuple characterNames, out HTuple characterCount); string character = string.Empty; foreach (var mem in characterNames.SArr) { character += mem + ", "; } DelegateUIControl.GetInstance().UpdateTextBox("txt_CharsTrainf", character, false); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btn_TrainOcr_Click(object sender, EventArgs e) { if (hDisplay1.GetSearchRegions().Count == 0) { MessageBox.Show("请先添加搜索区域"); return; } if (string.IsNullOrEmpty(txt_CharacterTrained.Text)) { MessageBox.Show("请先填写要训练的字符"); return; } HTuple hv_Number; HObject _roi = hDisplay1.GetSearchRegions().ElementAt(0); HObject _region = new HObject(); HObject _imagereduced = new HObject(); HObject ho_ObjectSelected = new HObject(); HOperatorSet.ReduceDomain(m_image, _roi, out _imagereduced); HOperatorSet.Threshold(_imagereduced, out _region, m_OcrParam.GrayMin, m_OcrParam.GrayMax); HOperatorSet.Connection(_region, out _region); HOperatorSet.SelectShape(_region, out _region, "area", "and", m_OcrParam.AreaMin, m_OcrParam.AreaMax); if (cklb_SortRegionModle.GetItemChecked(0)) { HOperatorSet.SortRegion(_region, out _region, "character", "true", "row"); } else { HOperatorSet.SortRegion(_region, out _region, "character", "true", "column"); } HOperatorSet.CountObj(_region, out hv_Number); if (hv_Number.I == 0) { MessageBox.Show("分割字符为空"); return; } string[] _characterTrained = new string[] { }; _characterTrained = txt_CharacterTrained.Text.Split(','); if (_characterTrained.Length != hv_Number.I) { MessageBox.Show("字符区域与字符个数不相等"); return; } HTuple hv_Index = new HTuple(); List <RegionX> _listregionx = new List <RegionX>(); for (hv_Index = 1; hv_Index.Continue(hv_Number, 1); hv_Index = hv_Index.TupleAdd(1)) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(_region, out ho_ObjectSelected, hv_Index); RegionX _hregion = new RegionX(ho_ObjectSelected.CopyObj(1, -1), "green"); _listregionx.Add(_hregion); } string path = string.Empty; string Foldername = string.Empty; string trffilepath = string.Empty; FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.ShowNewFolderButton = true; if (fbd.ShowDialog() == DialogResult.OK) { path = fbd.SelectedPath; if (!Directory.Exists(path)) { MessageBox.Show($"不存在 '{path}' 路径!"); return; } string[] _f = path.Split('\\'); Foldername = _f[_f.Length - 1]; trffilepath = path + "\\" + Foldername + ".trf"; } else { return; } HObject _characterRegion = new HObject(); HOperatorSet.GenEmptyObj(out _characterRegion); for (int i = 0; i < hv_Number.I; i++) { HOperatorSet.AppendOcrTrainf(_region[i + 1], m_image, _characterTrained[i], trffilepath); } HTuple _ocrhandle = new HTuple(); HTuple error = new HTuple(); HTuple errorlog = new HTuple(); HOperatorSet.CreateOcrClassMlp(m_TrainParam.CharacterWidth, m_TrainParam.CharacterHeiht, m_TrainParam.ZoomModle, "default", _characterTrained, 80, "none", 10, 42, out _ocrhandle); HOperatorSet.TrainfOcrClassMlp(_ocrhandle, trffilepath, 200, 1, 0.01, out error, out errorlog); HOperatorSet.WriteOcrTrainf(_region, m_image, _characterTrained, trffilepath); HOperatorSet.WriteOcrClassMlp(_ocrhandle, path + "\\" + Foldername); DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, _listregionx, null); }
private void btn_ReadCharacter_Click(object sender, EventArgs e) { if (m_ocrhandle == null) { MessageBox.Show("请先读取字符库"); return; } if (hDisplay1.GetSearchRegions().Count == 0) { MessageBox.Show("请先添加搜索区域"); return; } HTuple hv_Number; HObject _roi = hDisplay1.GetSearchRegions().ElementAt(0); HObject _region = new HObject(); HObject _imagereduced = new HObject(); HObject ho_ObjectSelected = new HObject(); HOperatorSet.ReduceDomain(m_image, _roi, out _imagereduced); HOperatorSet.Threshold(_imagereduced, out _region, m_OcrParam.GrayMin, m_OcrParam.GrayMax); HOperatorSet.Connection(_region, out _region); HOperatorSet.SelectShape(_region, out _region, "area", "and", m_OcrParam.AreaMin, m_OcrParam.AreaMax); HOperatorSet.CountObj(_region, out hv_Number); HTuple hv_Index = new HTuple(); List <RegionX> _listregionx = new List <RegionX>(); List <ViewROI.StringX> _liststringx = new List <ViewROI.StringX>(); for (hv_Index = 1; hv_Index.Continue(hv_Number, 1); hv_Index = hv_Index.TupleAdd(1)) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(_region, out ho_ObjectSelected, hv_Index); RegionX _hregion = new RegionX(ho_ObjectSelected.CopyObj(1, -1), "green"); _listregionx.Add(_hregion); } HTuple _characters = new HTuple(); HTuple _confidence = new HTuple(); // DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, _listregionx, null); HOperatorSet.DoOcrMultiClassMlp(_region, m_image, m_ocrhandle, out _characters, out _confidence); HTuple _row = new HTuple(); HTuple _column = new HTuple(); HTuple _area = new HTuple(); for (hv_Index = 1; hv_Index.Continue(hv_Number, 1); hv_Index = hv_Index.TupleAdd(1)) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(_region, out ho_ObjectSelected, hv_Index); HOperatorSet.AreaCenter(ho_ObjectSelected, out _area, out _row, out _column); ViewROI.StringX sx = new ViewROI.StringX(19, true, false); sx.SetString(_characters[hv_Index - 1], (int)_row.D + 50, (int)_column.D, Color.Green); _liststringx.Add(sx); } DelegateUIControl.GetInstance().UpdateHDisplay("FormSetHDisplay", m_image, _listregionx, _liststringx); List <OcrResult> ocrresultlist = new List <OcrResult>(); for (int i = 0; i < _characters.TupleLength(); i++) { OcrResult _ocrresult = new OcrResult(); _ocrresult.character = _characters[i]; _ocrresult.scale = _confidence[i].D.ToString("F4"); ocrresultlist.Add(_ocrresult); } dGV_CharacterResult.DataSource = ocrresultlist; }