`一直以来halcon都是工业界高效稳定准确的视觉算法库的代表,其定位,检测和识别算法都比较好,有很多学员不太了解其ocr字符识别算法如何编写,今天和大家一起探讨利用c#+halcon如何实现ocr。
1.加载字库Industrial_0-9_NoRej.omc
HOperatorSet.CreateTextModelReader("auto","Industrial_0-9_NoRej.omc", out hv_TextModel);
2.定位字符位置并输出识别结果
HObject hv_Characters;
HOperatorSet.SetColor(hWindowControl1.HalconWindow, "green");
hWindowControl1.Focus();
HTuple r, c, phi, len1, len2,hv_rel_class;
HObject rectangle;
hWindowControl1.Focus();
HOperatorSet.DrawRectangle2(hWindowControl1.HalconWindow, out r, out c, out phi, out len1, out len2);
HOperatorSet.GenRectangle2(out rectangle, r, c, phi, len1, len2);
HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
HOperatorSet.DispObj(himage, hWindowControl1.HalconWindow);
HOperatorSet.DispObj(rectangle, hWindowControl1.HalconWindow);
HOperatorSet.ReduceDomain(himage, rectangle, out temp);
HOperatorSet.FindText(temp, hv_TextModel, out hv_TextReslut);
HOperatorSet.GetTextObject(out hv_Characters, hv_TextReslut, "all_lines");
HOperatorSet.SmallestRectangle1(hv_Characters, out hv_Row1, out hv_Column1,
out hv_Row2, out hv_Column2);
HOperatorSet.SetColored(hWindowControl1.HalconWindow, 12);
HOperatorSet.DispObj(hv_Characters, hWindowControl1.HalconWindow);
HOperatorSet.GetTextResult(hv_TextReslut, "class", out hv_rel_class);
HTuple I;
for (I = 0; (int)I <= (int)((new HTuple(hv_rel_class.TupleLength())) - 1); I = (int)I + 1)
{
HOperatorSet.SetTposition(hWindowControl1.HalconWindow, (hv_Row2.TupleSelect(I)) + 10,
hv_Column1.TupleSelect(I));
HOperatorSet.WriteString(hWindowControl1.HalconWindow, hv_rel_class.TupleSelect(
I));
}
-----------------------------------------
总结:
CreateTextModelReader ------用于读取ocr字库
FindText--------------------------用于获取字符分割结果
GetTextObject-------------------用于获取字符分割后的目标
GetTextResult--------------------用于获取字符识别结果
以下动图:
|